Java 多线程工具类

一、CountDownLatch

CountDownLatch 可用于倒计数

getCount():获取当前计数器剩余计数

countDown():倒计数器释放一次

await():用在多线程执行的后面,只有当 CountDownLatch 计数器全部释放,及 getCount() == 0 时,才会唤醒,继续执行

public class Test {

	public static void main(String[] args) throws InterruptedException {

		ExecutorService es = Executors.newFixedThreadPool(10);
		CountDownLatch latch = new CountDownLatch(5);
		while(latch.getCount() > 0) {
		//for(int i = 0; i < 2; i++) {
			es.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName());
				}
			});
			latch.countDown();
		}
		latch.await();
		es.shutdown();
	}

}

二、CyclicBarrier

用于循环执行,只有达到指定个数的线程 执行await() 之后,才会唤醒

await():run 中 await 达到指定数之后,才唤醒

await(long timeout, TimeUnit unit):可设置超时时间,超时后如果还没有达到指定的 await() 数量,抛出超时异常

有一点:如果await() 未达到指定数量,则会导致线程一直等待。此时不会释放线程占用

public class Test08 {

	public static void main(String[] args) throws InterruptedException {

		ExecutorService es = Executors.newFixedThreadPool(3);
		CyclicBarrier cb = new CyclicBarrier(3);
		es.execute(new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("wait 【1】");
					System.out.println(Thread.currentThread().getName());
					cb.await();
					System.out.println(1);
				} catch (InterruptedException | BrokenBarrierException e) {
					e.printStackTrace();
				}
			}
		});
		es.execute(new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("wait 【2】");
					System.out.println(Thread.currentThread().getName());
					cb.await();
					System.out.println(2);
				} catch (InterruptedException | BrokenBarrierException e) {
					e.printStackTrace();
				}
			}
		});
		es.execute(new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("wait 【3】");
					System.out.println(Thread.currentThread().getName());
					cb.await();
					System.out.println(3);
				} catch (InterruptedException | BrokenBarrierException e) {
					e.printStackTrace();
				}
			}
		});
		es.execute(new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("wait 【4】");
					System.out.println(Thread.currentThread().getName());
					cb.await();
					System.out.println(4);
				} catch (InterruptedException | BrokenBarrierException e) {
					e.printStackTrace();
				}
			}
		});
		es.execute(new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("wait 【5】");
					System.out.println(Thread.currentThread().getName());
					cb.await();
					System.out.println(5);
				} catch (InterruptedException | BrokenBarrierException e) {
					e.printStackTrace();
				}
			}
		});

		for (int i = 0; i < 10; i++) {
			es.execute(new Runnable() {
				@Override
				public void run() {
					// 上面 4 5 占用了两个线程,所以线程池中只剩下一个线程可用
					System.out.println(Thread.currentThread().getName());
				}
			});
		}
		System.out.println("43412432432432");
		es.shutdown();
	}
}

三、Semaphore

Semaphore(int permits):permits 许可数目

Semaphore(int permits, boolean fair):fair 是否公平

acquire():获取许可证 -- 如果当前没有许可可以获取,则阻塞等待

acquire(int permits):permits 许可数量

release():释放许可证 -- 释放前需要先有获取的许可证,阻塞

release(int permits):permits 许可数量

有一点:如果指定数量的许可证都被占用,则会阻塞,若要不阻塞,可使用 tryAcquire() 方法

public class Test08 {

	public static void main(String[] args) throws InterruptedException {

		ExecutorService es = Executors.newFixedThreadPool(5);
		Semaphore sh = new Semaphore(4);
		for (int i = 0; i < 10; i++) {
			es.execute(new Work(i, sh));
		}

		es.shutdown();
	}
}

class Work implements Runnable {
	private int worker;
	private Semaphore semaphore;

	public Work(int worker, Semaphore semaphore) {
		this.worker = worker;
		this.semaphore = semaphore;
	}

	@Override
	public void run() {
		try {
			semaphore.acquire(1);
			System.out.println("当前工作人员 【" + worker + "】" + " 获取许可");
			semaphore.release(1);
			System.out.println("当前工作人员 【" + worker + "】" + " 释放");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

四、Exchanger

进行两个线程之间数据交换

exchange():如果调用交换方法的线程为单数,则会有一个线程进行等待,阻塞线程

public class Test08 {

	public static void main(String[] args) throws InterruptedException {

		ExecutorService es = Executors.newFixedThreadPool(5);
		Exchanger change = new Exchanger<>();
		es.execute(new Runnable() {
			@Override
			public void run() {
				try {
					String s1 = "1";
					System.out.println(s1);
					String s2 = change.exchange(s1);
					System.out.println(s1 + " - " +s2);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		es.execute(new Runnable() {
			@Override
			public void run() {
				try {
					String s1 = "2";
					System.out.println(s1);
					String s2 = change.exchange(s1);
					System.out.println(s1 + " - " +s2);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		es.execute(new Runnable() {
			@Override
			public void run() {
				try {
					String s1 = "3";
					System.out.println(s1);
					String s2 = change.exchange(s1);
					System.out.println(s1 + " - " +s2);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		es.execute(new Runnable() {
			@Override
			public void run() {
				try {
					String s1 = "4";
					System.out.println(s1);
					String s2 = change.exchange(s1);
					System.out.println(s1 + " - " +s2);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		
		es.shutdown();
	}
}

 

你可能感兴趣的:(Java,线程)