![](https://i-blog.csdnimg.cn/blog_migrate/ba763dee4feb1223a54391ac45f0fa2f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b2c8f72101aa61ef9d2013ebc576d618.png)
public class CallableTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//如何启动Callable
MyThread thread=new MyThread();
//需要和FutureTask类配套使用
FutureTask task = new FutureTask(thread);
new Thread(task,"A").start();
new Thread(task,"B").start();
Integer o = (Integer) task.get();//可能会产生阻塞,一般放在最后一行或者异步通信来处理
System.out.println(o);
}
}
class MyThread implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("123456");//两个线程只会打印一次,因为结果会被缓存,效率高
return 1024;
}
}
减法计数器CountDownLatch
/**
* @author wuyimin
* @create 2021-07-08-19:40
* @description 减法计数器 每次有线程调用countDown方法数量就会减一,假设变为0,await就会被唤醒,继续执行
*/
public class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch=new CountDownLatch(6);
for (int i = 0; i < 6; i++) {
new Thread(()->{
System.out.println(Thread.currentThread().getName() + "Go");
countDownLatch.countDown();//数量减一
},String.valueOf(i)).start();
}
countDownLatch.await();//等待计数器归零,然后再向下执行
System.out.println("结束");
}
}
加法计数器CyclicBarrier
/**
* @author wuyimin
* @create 2021-07-08-19:47
* @description 加法计数器
* */
public class addDemo {
public static void main(String[] args) {
//只有线程数量达到了八个的时候才会执行该代码
CyclicBarrier cyclicBarrier=new CyclicBarrier(8,()->{
System.out.println("成功");
});
for (int i = 0; i <=7; i++) {
//lambda如何拿到上面的i,加上final变量会储存在方法区里,子线程共享堆,所以能读到,
// 如果就是i的话,是存在于另一个线程的栈中,不同的线程读不到
final int temp=i;
new Thread(()->{
System.out.println("当前计数"+temp);
try {
cyclicBarrier.await();//使用await来计数,await后这次线程会被阻塞
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}).start();
}
}
}
限流Semaphore
public class SemaphoreDemo {
public static void main(String[] args) {
//线程数量
Semaphore semaphore = new Semaphore(3);//限流 每次只能有三个线程进来操作
for (int i = 0; i <=6 ; i++) {
new Thread(()->{
//acquire()得到
//release()释放
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName()+"拿到");
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName()+"释放");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
semaphore.release();
}
}).start();
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/1e1cfea6a1f7f903660d98a88d6d8069.png)