JUC之Callable接口回顾和JUC辅助类
Callable接口和JUC辅助类
Callable接口:
回顾:
创建线程的四种方式:
- 继承Thread
- 实现runnable接口
- 实现callable接口
- 使用线程池
之前的文章:多线程编程1-定义理解与三种实现方式
Runnable和Callable接口的差异:
- Runnable无返回值,Callable有返回值
- Runnable不抛异常,Callable抛异常
- 实现名称不同,Runnable是run方法,Callable是call方法
class MyThread1 implements Runnable{
@Override
public void run() {
}
}
class MyThread2 implements Callable{
@Override
public Integer call() throws Exception {
return 200;
}
}
Runnable 接口实现类FutureTask
FutureTask构造可以传递callable
这是类的继承结构:

别名:可取消的异步,简单的理解是当主线程中存在耗时高的任务时,可以单开一个子线程处理,主线程处理耗时少的任务,最终汇合在一起。
需要注意的是,使用FutureTask当得到第一次结果后,第二次获取时直接返回结果,也可以说所有的任务只汇总一次。
JUC辅助类:
CountDownLatch(减少计数)
定义:一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。用给定的计数 初始化 CountDownLatch。由于调用了 countDown() 方法,所以在当前计数到达零之前,await 方法会一直受阻塞。之后,会释放所有等待的线程,await 的所有后续调用都将立即返回。这种现象只出现一次——计数无法被重置。如果需要重置计数,请考虑使用 CyclicBarrier。
CountDownLatch是一个通用同步工具,它有很多用途。将计数 1 初始化的CountDownLatch用作一个简单的开/关锁存器,或入口:在通过调用countDown()的线程打开入口前,所有调用await的线程都一直在入口处等待。用 N 初始化的CountDownLatch可以使一个线程在 N 个线程完成某项操作之前一直等待,或者使其在某项操作完成 N 次之前一直等待
/**
* 问题,当六个人走出教室,则班长锁门
*/
public class CountDownLatch07 {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(6);
for(int i = 1; i <= 6; i++){
new Thread(() ->{
System.out.println(Thread.currentThread().getName()+" 号同学走出教室");
latch.countDown();
},String.valueOf(i)).start();
}
latch.await();
System.out.println(Thread.currentThread().getName()+"班长锁门");
}
}
其使用的方法,CountDownLatch latch = new CountDownLatch(6)、latch.countDown()、latch.await();
CyclicBarrier(循环栅栏)也可以实现CountDownLatch效果,CyclicBarrier在所有线程执行完毕之后是可以重用的。
CyclicBarrier(循环栅栏)
源码定义:
一种同步辅助工具,它允许一组线程全部等待彼此到达公共屏障点。 CyclicBarriers 在涉及固定大小的线程组的程序中很有用,这些线程必须偶尔相互等待。 屏障被称为循环的,因为它可以在等待线程被释放后重新使用。
简单的理解,当达到设置的要求后,执行特定的内容,相当于监听器;
实例代码:
public class JucUtils {
private static final Integer NUMBER = 7;
public static void main(String[] args) {
//设置资源要求,达到后所需要执行的任务
CyclicBarrier cyclicBarrier = new CyclicBarrier(NUMBER, () -> {
System.out.println("资源达到要求!");
});
//对设置的目标前进
for (int i = 1; i <= 7; i++) {
int finalI = i;
new Thread(()->{
try {
System.out.println("资源正在收集:"+ Thread.currentThread().getName());
//等待,当等待的线程数量到达设置的值,调用执行任务并释放这些线程。
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
},String.valueOf(i)).start();
}
}
}
感兴趣的可以看看源码:
-------------------------CyclicBarrier--------------------
public CyclicBarrier(int parties, Runnable barrierAction) {
if (parties <= 0) throw new IllegalArgumentException();
this.parties = parties;
this.count = parties; //设置临界值
this.barrierCommand = barrierAction;
}
----------------------------await-----------------------
public int await() throws InterruptedException, BrokenBarrierException {
try {
return dowait(false, 0L);
} catch (TimeoutException toe) {
throw new Error(toe); // cannot happen
}
}
//------------------dowait部分代码-------------------
final Generation g = generation;
if (g.broken)
throw new BrokenBarrierException();
if (Thread.interrupted()) {
breakBarrier();
throw new InterruptedException();
}
int index = --count; //每次等待,所需资源-1;
if (index == 0) { // tripped
boolean ranAction = false;
try {
final Runnable command = barrierCommand;
if (command != null)
command.run();
ranAction = true;
nextGeneration();
return 0;
} finally {
if (!ranAction)
breakBarrier();
}
}
//------nextGeneration----------
private void nextGeneration() {
// signal completion of last generation
trip.signalAll(); //唤醒线程
// set up next generation
count = parties;
generation = new Generation();
}
Semaphore(信号灯)
如果对操作系统有所了解的话,该工具类就是信号量+pv操作的集合,对信号量的操作只有三种,初始化、p操作、v操作,其中信号量就是Semaphore初始化的(某种资源的数量),p操作对应的是semaphore.acquire(),信号量--,v操作对应的semaphore.release(),信号量++,当Semaphore初始化唯1时,则为互斥资源。
package com.JUC;
import java.util.Random;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* 类比操作系统的中信号量PV操作
*/
public class Semaphore07 {
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(3);
for(int i = 1; i <= 6; i++){
new Thread(()->{
try {
semaphore.acquire(); //加锁
System.out.println(Thread.currentThread().getName()+" 抢到了车位");
TimeUnit.SECONDS.sleep(new Random().nextInt(5));//随机时间停车
System.out.println(Thread.currentThread().getName()+"-------离开了车位");
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
semaphore.release(); //解锁
}
},String.valueOf(i)).start();
}
}
}
JUC之Callable接口回顾和JUC辅助类的更多相关文章
- 基于接口回调详解JUC中Callable和FutureTask实现原理
Callable接口和FutureTask实现类,是JUC(Java Util Concurrent)包中很重要的两个技术实现,它们使获取多线程运行结果成为可能.它们底层的实现,就是基于接口回调技术. ...
- JUC之Lock接口以及Synchronized回顾
Lock接口 Synchronized关键字回顾: 多线程编程步骤(上): 创建资源类,在资源类创建属性和操作方法 创建多个线程,调用资源类的操作方法 创建线程的四种方式: 继承Thread 实现Ru ...
- JUC—Callable接口
一.callable接口是什么? 面试题: 获得多线程的方法几种? 正确答案如下: 传统的 是继承thread类和实现runnable接口, java5以后又有实现 callable接口 和 java ...
- 实现多线程的方式之实现Callable接口
package com.hls.juc; import java.util.concurrent.Callable;import java.util.concurrent.ExecutionExcep ...
- 实现Callable接口创建线程
创建执行线程有四种方式: 实现implements接口创建线程 继承Thread类创建线程 实现Callable接口,通过FutureTask包装器来创建线程 使用线程池创建线程 下面介绍通过实现Ca ...
- 创建执行线程方式三:实现Callable接口
Callable接口 ① Java 5.0 在 java.util.concurrent 提供了一个新的创建执行 线程的方式:Callable 接口② Callable 接口类似于 Runnable, ...
- Callable接口、Runable接口、Future接口
1. Callable与Runable区别 Java从发布的第一个版本开始就可以很方便地编写多线程的应用程序,并在设计中引入异步处理.Thread类.Runnable接口和Java内存管理模型使得多线 ...
- 线程池的应用及Callable接口的使用
public interface Executor { /** * Executes the given command at some time in the future. The comman ...
- [改善Java代码]异步运算考虑使用Callable接口
多线程有两种实现方式: 一种是实现Runnable接口,另一种是继承Thread类,这两种方式都有缺点,run方法没有返回值,不能抛出异常(这两个缺点归根到底是Runable接口的缺陷,Thread也 ...
随机推荐
- Maven错误收集
Eclipse 创建项目时报错 Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:1 ...
- AOP切入点的配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- ubuntu 使用mysql
一: 安装: sudo apt-get install mysql-serversudo apt-get install mysql-clientsudo apt-get install libmys ...
- JSP页面的基本结构
一:一个JSP页面由以下基本元素组成. (1)HTML标签 (2)CSS (3)变量和方法 (4)Java代码段 (5)JSP动作和指令 (6)其他脚本元素(如Javascript) 二:JSP的基本 ...
- python中的虚拟环境(在jupyter和pycharm中的使用)
1.通过anaconda新建虚拟环境 创建虚拟环境:conda create -n your_env_name python=3.6 激活虚拟环境:activate your_env_name(虚拟环 ...
- 05 - Vue3 UI Framework - Button 组件
官网基本做好了,接下来开始做核心组件 返回阅读列表点击 这里 目录准备 在项目 src 目录下创建 lib 文件夹,用来存放所有的核心组件吧.然后再在 lib 文件夹下创建 Button.vue 文件 ...
- 尚硅谷SSM-CRUD实战Demo
SSM-CRUD实战项目 1. 项目总览 SpringMVC + Spring + MyBatis CRUD:增删改查 功能: 分页 数据校验 jquery前端校验+JSR303后端校验 ajax R ...
- SpringBoot使用 MyBatis Plus 实现物理分页查询
一.分页配置在MyBatis Plus 可以直接使用selectPage这样的分页,但返回的数据确实是分页后的数据,但在控制台打印的SQL语句其实并没有真正的物理分页,而是通过缓存来获得全部数据中再进 ...
- Intellij IDEA添加插件
1. Lombok插件 IDEA增加对Lombok的支持 <!--lombok插件--> <dependency> <groupId>org.projectlomb ...
- C语言之字符串替换库函数replace
头文件 #include <algorithm> 例子 下面的代码, 将字符串中的 /替换为\ std::string str("C:/demo/log/head/send&qu ...