(2人评价)
JUC线程框架深度讲解

java.util.concurrent线程框架全面分析

价格 ¥ 198.00
public class JUCDemo {
    public static void main(String[] args) throws Exception {
//        test1();

//        testnewCachedThreadPool();


//        testnewFixedThreadPool();


//        testnewsinglethreadExecutor();


//        testnewScheduledThreadPool();


//        testFixedThreadPool();

//        testExecutorCompletionService();

    }
private static void testThreadPoolExecutor() {
    BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(5);
    ThreadPoolExecutor executor = new ThreadPoolExecutor(
            2, 4, 1, TimeUnit.SECONDS,
            queue, Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
    for(int x=0;x<10;x++){
        executor.submit(()->{
            System.out.println("【"+Thread.currentThread().getName()+"】before:执行任务开始。");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("【"+Thread.currentThread().getName()+"】after:执行任务结束。");
        });
    }
}

    private static void testExecutorCompletionService() throws InterruptedException, ExecutionException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        ExecutorCompletionService<String> service = new ExecutorCompletionService<String>(executorService);
        for(int x=0;x<10;x++){
            service.submit(new TaskItem());
        }
        for(int x=0;x<10;x++){
            System.out.println(service.take().get());
        }
        executorService.shutdown();
    }

    private static void testFixedThreadPool() throws InterruptedException, ExecutionException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        Set<Callable<String>> allThreads=new HashSet<>();
        for(int x=0;x<10;x++){
            int temp=x;
            allThreads.add(()->{
                return "【"+Thread.currentThread().getName()+"】执行任务,num="+temp;
            });
        }
        List<Future<String>> futures = executorService.invokeAll(allThreads);
        for(Future<String>  future:futures){
            System.out.println(future.get());
        }
    }

    private static void testnewScheduledThreadPool() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
        for(int x=0;x<3;x++){
            scheduledExecutorService.scheduleAtFixedRate(()->{
                System.out.println("【"+Thread.currentThread().getName()+"】执行调度任务!!");
            },3,3, TimeUnit.SECONDS);
        }
    }

    private static void testnewsinglethreadExecutor() throws InterruptedException {
        CountDownLatch cdl=new CountDownLatch(10);
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for(int x=0;x<10;x++){
                executorService.submit(()->{
                    System.out.println("【"+Thread.currentThread().getName()+"】执行任务!!");
                })  ;
                cdl.countDown();
        }
        cdl.await();
    }

    private static void testnewFixedThreadPool() throws InterruptedException {
        CountDownLatch cdl=new CountDownLatch(10);
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for(int x=0;x<10;x++){
            executorService.submit(()->{
                System.out.println("【"+Thread.currentThread().getName()+"】执行任务!");
            });
            cdl.countDown();
        }
        cdl.await();
    }

    private static void testnewCachedThreadPool() throws InterruptedException {
        CountDownLatch cdl=new CountDownLatch(10);
        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int x=0;x<10;x++){
            executorService.submit(()->{
                System.out.println("【"+Thread.currentThread().getName()+"】"+"执行任务!");
            });
            cdl.countDown();
        }
        cdl.await();
    }

    private static void test1() throws InterruptedException {
        for (int x = 0; x < 10; x++){
            ThreadBody.getThread().start();
            ThreadBody.getThread().sleep(1000) ;
        }
    }
}


class DefaultThreadFactory implements ThreadFactory {
    private static int count=0;
    @Override
    public Thread newThread(@NotNull Runnable r) {
        Thread newThread=new Thread(r,"MLDNThread-"+count++);
        newThread.setPriority(Thread.MIN_PRIORITY);
        return newThread;
    }
}


class TaskItem implements Callable<String>{

    @Override
    public String call() throws Exception {
        long startTime=System.currentTimeMillis();
        TimeUnit.SECONDS.sleep(1);
        long endTime=System.currentTimeMillis();
        return "【"+Thread.currentThread().getName()+"】执行任务花费时间:"+(endTime-startTime);
    }
}

class ThreadBody {
    private static final ThreadLocal<Thread>  LOCAL=new ThreadLocal<>();
    public static Thread getThread(){
        Thread thread = LOCAL.get();
        if(thread==null){
            ThreadFactory factory =   new DefaultThreadFactory();
            thread=factory.newThread(()->{
                for(int x=0;x<5;x++){
                    System.out.printf("子线程运行,当前线程的ID:%s、线程名称:%s\n",
                            Thread.currentThread().getId(),
                            Thread.currentThread().getName());
                }
                LOCAL.remove();
            });
        }
        return thread;
    }
}
[展开全文]

授课教师

讲师

课程特色

视频(68)
文档(22)
图文(3)
下载资料(1)