java.util.concurrent概览

2019-02-18  本文已影响0人  传葱

前言

核心类

Executor

public class InvokeExecutor implements Executor {
    @Override
    public void execute(Runnable command) {
        command.run();
    }

    public static void main(String[] args) {
        executor();
    }

    public static void executor() {
        //替代专门create一个线程
        Executor executor = new InvokeExecutor();
        executor.execute(() -> {
            System.out.println("hello,world!");
        });

        executor.execute(() -> {
            System.out.println("lalal");
        });
    }
}

ExecutorService

public class ExecutorServiceTest {
    ExecutorService executor = Executors.newFixedThreadPool(10);

    public void executor() {
        executor.submit(() -> {
            new Task();
        });

        executor.submit(() -> {
            new Task1();
        });

    }

    public static void main(String[] args) {
        ExecutorServiceTest executorServiceTest = new ExecutorServiceTest();
        executorServiceTest.executor();
    }

}

class Task implements Runnable {

    @Override
    public void run() {

    }
}

class Task1 implements Runnable {

    @Override
    public void run() {

    }
}

ScheduledExecutorService

public class ScheduleTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ScheduleTest scheduleTest = new ScheduleTest();
        scheduleTest.executor();
    }

    public void executor() throws ExecutionException, InterruptedException {
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        Future<String> future = scheduledExecutorService.schedule(() -> {
            return "hello, world!";
        }, 1,  TimeUnit.SECONDS);

        System.out.println(future.get());

        //上一个任务执行完成后delay delay长的时间开始执行下个任务
        scheduledExecutorService.scheduleWithFixedDelay(() -> {
            System.out.println("lalal");
        }, 1, 2 ,TimeUnit.SECONDS);

        //间隔perid开始执行新的任务
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println("lalal");
        }, 1, 2 ,TimeUnit.SECONDS);

//      scheduledExecutorService.shutdown();
    }
}

Future

public void invoke() {
    ExecutorService executorService = Executors.newFixedThreadPool(10);
 
    Future<String> future = executorService.submit(() -> {
        // ...
        Thread.sleep(10000l);
        return "Hello world";
    });
}


if (future.isDone() && !future.isCancelled()) {
    try {
        str = future.get();
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}
try {
    future.get(10, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
    e.printStackTrace();
}

CountDownLatch

CyclicBarrier

public class Task implements Runnable {
 
    private CyclicBarrier barrier;
 
    public Task(CyclicBarrier barrier) {
        this.barrier = barrier;
    }
 
    @Override
    public void run() {
        try {
            LOG.info(Thread.currentThread().getName() + 
              " is waiting");
            barrier.await();
            LOG.info(Thread.currentThread().getName() + 
              " is released");
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
 
}


public void start() {
 
    CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> {
        // ...
        LOG.info("All previous tasks are completed");
    });
 
    Thread t1 = new Thread(new Task(cyclicBarrier), "T1"); 
    Thread t2 = new Thread(new Task(cyclicBarrier), "T2"); 
    Thread t3 = new Thread(new Task(cyclicBarrier), "T3"); 
 
    if (!cyclicBarrier.isBroken()) { 
        t1.start(); 
        t2.start(); 
        t3.start(); 
    }
}
上一篇下一篇

猜你喜欢

热点阅读