JUC 线程工具类基本使用

CountDownLatch(发令枪)

  • public void await() throws InterruptedException { };
调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行
  • public void countDown() { };
将count值减1
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class CountDownLatchDemo {

	static CountDownLatch downLatch = new CountDownLatch(10);

	public static void main(String[] args) {
		ScheduledExecutorService executorService = Executors.newScheduledThreadPool(200);
		for (int i = 10; i > 0; i--) {
			executorService.submit(new Task());
		}

		try {
			System.out.println("等待子线程执行完毕");
			downLatch.await();//线程走到这里均挂起
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println("子线程执行完毕,继续执行其他");

		executorService.shutdown();
	}

	static class Task implements Runnable{
		@Override
		public void run() {
			System.out.println(Thread.currentThread().getName() + "task is start");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "task is end");
			downLatch.countDown();
		}
	}
}
pool-1-thread-1task is start
pool-1-thread-3task is start
pool-1-thread-2task is start
pool-1-thread-4task is start
等待子线程执行完毕
pool-1-thread-5task is start
pool-1-thread-1task is end
pool-1-thread-3task is end
pool-1-thread-2task is end
pool-1-thread-4task is end
pool-1-thread-5task is end
子线程执行完毕,继续执行其他

CyclicBarrier(回环栅栏)

实现让一组线程等待至某个状态之后再全部同时执行
  • public CyclicBarrier(int parties, Runnable barrierAction) {}
parties:指让多少个线程或者任务等待至barrier状态
barrierAction:当这些线程都达到barrier状态时会执行的内容
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;


public class CylicBarrierDemo {

	static CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() {

		@Override
		public void run() {
			System.out.println(Thread.currentThread().getName() + " i am cyclicBarrier last run");
		}
	});

	public static void main(String[] args) {

		ScheduledExecutorService executorService = Executors.newScheduledThreadPool(200);

		for (int i = 10; i > 0; i--) {
			executorService.submit(new Task());
		}

		executorService.shutdown();
	}

	static class Task implements Runnable {

		@Override
		public void run() {
			System.out.println(Thread.currentThread().getName() + "task is start");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			try {
				System.out.println(Thread.currentThread().getName() + "task is end");
				cyclicBarrier.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (BrokenBarrierException e) {
				e.printStackTrace();
			}
		}
	}

}import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;


public class CylicBarrierDemo {

	static CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() {

		@Override
		public void run() {
			System.out.println(Thread.currentThread().getName() + " i am cyclicBarrier last run");
		}
	});

	public static void main(String[] args) {

		ScheduledExecutorService executorService = Executors.newScheduledThreadPool(200);

		for (int i = 10; i > 0; i--) {
			executorService.submit(new Task());
		}

		executorService.shutdown();
	}

	static class Task implements Runnable {

		@Override
		public void run() {
			System.out.println(Thread.currentThread().getName() + "task is start");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			try {
				System.out.println(Thread.currentThread().getName() + "task is end");
				cyclicBarrier.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (BrokenBarrierException e) {
				e.printStackTrace();
			}
		}
	}

}
pool-1-thread-2task is start
pool-1-thread-1task is start
pool-1-thread-3task is start
pool-1-thread-4task is start
pool-1-thread-5task is start
pool-1-thread-6task is start
pool-1-thread-7task is start
pool-1-thread-8task is start
pool-1-thread-9task is start
pool-1-thread-10task is start
pool-1-thread-2task is end
pool-1-thread-1task is end
pool-1-thread-4task is end
pool-1-thread-3task is end
pool-1-thread-5task is end
pool-1-thread-5 i am cyclicBarrier last run
pool-1-thread-6task is end
pool-1-thread-7task is end
pool-1-thread-8task is end
pool-1-thread-9task is end
pool-1-thread-10task is end
pool-1-thread-10 i am cyclicBarrier last run

Semaphore

  • acquire() 获取一个许可
  • release() 释放一个许可
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Semaphore;

public class SeamphoreDemo {

	static Semaphore seamphore = new Semaphore(5);

	public static void main(String[] args) {

		ScheduledExecutorService executorService = Executors.newScheduledThreadPool(200);

		for (int i = 10; i > 0; i--) {
			executorService.submit(new Task());
		}

		executorService.shutdown();
	}

	static class Task implements Runnable {

		@Override
		public void run() {
			try {
				seamphore.acquire();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "task is start");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "task is end");
			seamphore.release();
		}
	}
}
pool-1-thread-1task is start
pool-1-thread-2task is start
pool-1-thread-3task is start
pool-1-thread-4task is start
pool-1-thread-5task is start
pool-1-thread-1task is end
pool-1-thread-6task is start
pool-1-thread-2task is end
pool-1-thread-3task is end
pool-1-thread-4task is end
pool-1-thread-8task is start
pool-1-thread-9task is start
pool-1-thread-5task is end
pool-1-thread-7task is start
pool-1-thread-10task is start
pool-1-thread-6task is end
pool-1-thread-8task is end
pool-1-thread-9task is end
pool-1-thread-7task is end
pool-1-thread-10task is end
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值