JAVA 多线程同步工具类总结
来源:互联网 发布:linux查看电池电量 编辑:程序博客网 时间:2024/06/15 07:04
关于同步常用的几个工具类总结如下:
- CountDownLatch 闭锁:可以延迟线程的进度,直到锁到达终止状态。闭锁的作用相当于一扇门,在锁到达终止状态之前这扇门一直是关闭的。当锁到达终止状态时,允许所有线程通过。CountDownLatch 有一个初始值,通过调用 countDown 可以减少该值,一直到 0 时到达终止状态。
- FutureTask 用于执行一个可返回结果的长任务,任务在单独的线程中执行,其他线程可以用 get 方法取任务结果,如果任务尚未完成,线程在 get 上阻塞。
- Semaphore 用于控制同时访问某资源,或同时执行某操作的线程数目。信号量有一个初始值即可以分配的信号量总数目。线程任务开始前先调用 acquire 取得信号量,任务结束后调用 release 释放信号量。在 acquire 是如果没有可用信号量,线程将阻塞在 acquire 上,直到其他线程释放一个信号量。
- CyclicBarrier 栅栏用于多个线程多次迭代时进行同步,在一轮任务中,任何线程完成任务后都在 barrier 上等待,直到所有其他线程也完成任务,然后一起释放,同时进入下一轮迭代。
CountDownLatch 的例子:
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.atomic.AtomicInteger;
-
-
- public class DemoOfLatch {
-
-
- public static void main(String[] args) {
-
- int numberOfThread = 5;
- final CountDownLatch startLatch = new CountDownLatch(1);
- final CountDownLatch stopLatch = new CountDownLatch(numberOfThread);
- final AtomicInteger count = new AtomicInteger(0);
-
- System.out.println("Main thread start...");
-
- for ( int i=0; i<numberOfThread; i++ ) {
- Thread thread = new Thread( new Runnable() {
-
- @Override
- public void run() {
- int tid = count.getAndIncrement();
- try {
-
- startLatch.await();
- System.out.printf("Thread %d started...%n", tid);
- int duration = (int)(Math.random() * 5000);
- Thread.sleep(duration);
- } catch (InterruptedException e) {
- e.printStackTrace();
- Thread.currentThread().interrupt();
- } finally {
- System.out.printf("Thread %d stoped...%n", tid);
-
- stopLatch.countDown();
- }
- }
-
- });
- thread.start();
- }
-
-
- System.out.println("Main thread do preparation work for child threads...");
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
-
- System.out.println("Main thread let child threads go...");
- startLatch.countDown();
-
- try {
-
- System.out.println("Main thread wait for all child threads...");
- stopLatch.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- System.out.println("Main thread exit...");
- }
-
- }
FutureTask 的例子:
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.FutureTask;
-
-
- public class DemoOfFutureTask {
-
- public static void main(String[] args) {
-
-
- final FutureTask<Movie> future = new FutureTask<Movie>( new Callable<Movie>() {
-
- @Override
- public Movie call() throws Exception {
- System.out.println("Future task started...");
- Thread.sleep(5000);
- System.out.println("Future task finished...");
- return new Movie("2012","Unknown");
- }
-
- });
-
-
- Thread thread = new Thread(future);
- thread.start();
-
-
- System.out.println("Now let's do sth eles...");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- }
-
-
- System.out.println("Now wait for result of future task...");
- try {
- Movie res = future.get();
- System.out.printf("Result from task is name=%s, actor=%s", res.name, res.actor);
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
-
- }
-
- public static class Movie {
- final public String name;
- final public String actor;
- public Movie(String name, String actor) {
- this.name = name;
- this.actor = actor;
- }
- }
-
- }
Semaphore 的例子:
- import java.util.concurrent.Semaphore;
- import java.util.concurrent.atomic.AtomicInteger;
-
- public class DemoOfSemaphore {
-
-
-
-
- public static void main(String[] args) {
-
- final int numOfThread = 5;
- final AtomicInteger count = new AtomicInteger(0);
- final Semaphore semaphore = new Semaphore(numOfThread);
-
- for (int i = 0; i < 10; i++) {
- Thread thread = new Thread(new Runnable() {
-
- @Override
- public void run() {
- int tid = count.getAndIncrement();
- try {
-
- System.out.printf("Thread %d wait on semaphore...%n", tid);
- semaphore.acquire();
-
- System.out.printf("Thread %d get semaphore...%n", tid);
- int duration = (int)(Math.random() * 5000);
- Thread.sleep(duration);
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
-
- System.out.printf("Thread %d release semaphore...%n", tid);
- semaphore.release();
- }
-
- }
-
- });
- thread.start();
- }
-
- }
-
- }
CyclicBarrier 的例子:
- import java.util.concurrent.BrokenBarrierException;
- import java.util.concurrent.CyclicBarrier;
-
- public class DemoOfBarrier {
-
- public static void main(String[] args) {
-
- final int numOfThread = 2;
- final int numOfIteration = 2;
-
-
- final CyclicBarrier barrier = new CyclicBarrier(numOfThread,
- new Runnable() {
-
-
-
- @Override
- public void run() {
- long tid = Thread.currentThread().getId();
-
- System.out.printf("[%d] - All threads arrived barrier...%n", tid);
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.printf("[%d] - Clear work done...%n", tid);
- }
-
- });
-
-
- for (int i = 0; i < numOfThread; i++) {
- Thread thread = new Thread(new Runnable() {
-
- @Override
- public void run() {
- long tid = Thread.currentThread().getId();
- for ( int k=0; k<numOfIteration; k++ ) {
- try {
-
- System.out.printf("Thread %d start its work...%n", tid);
- long duration = (int)(Math.random()*5000);
- Thread.sleep(duration);
-
- System.out.printf("Thread %d wait on barrier...%n", tid);
- int num = barrier.await();
-
- System.out.printf("Thread %d pass barrier with order=%d...%n", tid, num);
- } catch (InterruptedException e) {
- e.printStackTrace();
- Thread.currentThread().interrupt();
- } catch (BrokenBarrierException e) {
- e.printStackTrace();
- }
- }
- }
-
- });
- thread.start();
- }
-
- }
-
- }