多线程

This commit is contained in:
xiongraorao 2018-08-02 15:58:10 +08:00
parent e90aa5acb3
commit a7c6568e6e
9 changed files with 977 additions and 5 deletions

View File

@ -0,0 +1,46 @@
package com.raorao.java.thread;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
/**
* 线程类.
*
* @author Xiong Raorao
* @since 2018-08-01-15:40
*/
public class MyThread extends Thread {
public static void main(String[] args) {
Thread t1 = new MyThread();
t1.start();
Thread t2 = new Thread(new MyThread2());
t2.start();
Callable call = new MyCallable();
FutureTask<String> task = new FutureTask<String>(call);
Thread t3 = new Thread(task);
t3.start();
}
@Override
public void run() {
System.out.println("线程运行中---------extends");
}
static class MyThread2 implements Runnable{
@Override
public void run() {
System.out.println("线程运行中------runnable");
}
}
static class MyCallable implements Callable{
@Override
public String call() throws Exception {
System.out.println("线程运行中------callable");
return "call over!";
}
}
}

View File

@ -0,0 +1,53 @@
package com.raorao.java.thread.excutor;
import afu.org.checkerframework.checker.igj.qual.I;
import com.raorao.java.thread.excutor.MyExecutor.MyRunnable;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import javax.sound.midi.Soundbank;
import org.omg.PortableServer.THREAD_POLICY_ID;
/**
* futureTask测试.
*
* @author Xiong Raorao
* @since 2018-08-02-11:16
*/
public class FutureTaskTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Integer result = 0;
FutureTask<Integer> task1 = new FutureTask<>(new MyRunnable("future thread"), result);
// task1.run();
// System.out.println(task1.get());
// FutureTask<Integer> task2 = new FutureTask<>(new MyCallable(5));
// task2.run();
// System.out.println(task2.get());
ExecutorService service = Executors.newFixedThreadPool(1);
Future<Integer> res = service.submit(new MyCallable(5));
System.out.println(res.get());
System.out.println("ok");
Thread.sleep(2000);
service.shutdown();
}
static class MyCallable implements Callable<Integer>{
private Integer res = 0;
public MyCallable(Integer a) {
res = a;
}
@Override
public Integer call() throws Exception {
//Thread.sleep(2000);
wait(2000);
return res * res;
}
}
}

View File

@ -0,0 +1,40 @@
package com.raorao.java.thread.excutor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;
/**
* 可重入锁测试.
*
* @author Xiong Raorao
* @since 2018-08-02-11:14
*/
public class LockTest {
private Lock lock = new ReentrantLock();
public static void main(String[] args) {
LockTest test = new LockTest();
ExecutorService service = Executors.newFixedThreadPool(3);
for(int i = 0 ; i < 3 ; i ++){
service.submit(() -> test.func());
}
}
public void func() {
lock.lock();
try {
for (int i = 0; i < 10; i++) {
System.out.print(i + " ");
}
} finally {
lock.unlock(); // 确保释放锁从而避免发生死锁
}
}
}

View File

@ -0,0 +1,47 @@
package com.raorao.java.thread.excutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import javax.sound.midi.Soundbank;
import javax.sound.midi.Track;
/**
* Executor框架.
*
* @author Xiong Raorao
* @since 2018-08-01-16:32
*/
public class MyExecutor {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(3);
for(int i = 0; i < 3; i++){
service.submit(new MyRunnable("thread-" + i));
}
service.shutdown();
}
static class MyRunnable implements Runnable{
private String name;
public MyRunnable(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(name + " start ...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + " end ...");
}
}
}

View File

@ -29,6 +29,9 @@ Thoutworks | 内推 | | [内推链接](https://jinshuju.net/f/CcO2JA)
多益网络 | 内推 | <li>内推笔试第一批8.11 10:00 <li> 内推笔试第二批: 9.06 10:00 | [招聘官网](https://xz.duoyi.com/jobs/index.html?t=0) <br>7月20日投了内推
好未来 | 提前批 | 8.19日截止 | [招聘官网](http://job.100tal.com/jobxq?jobId=510212759) <li> 7月24日投了
腾讯 | 提前批/网申 | <li> 提前批7.25-9.12 <li> 提前批7.25-9.14 <li> 在线笔试9.16-9.17 <li> 面试9.26开始| [招聘官网](https://join.qq.com/)
抖音、头条 | 内推 | 8.1 - 12.31| [招聘官网](https://job.bytedance.com/campus/) <li> 8.2 投简历
携程 | 内推 | <li> 内推8.2 - 8.12 <li> 网申 8.2 - 9.4 | [招聘官网](http://campus.ctrip.com)
## 2. 面试记录

View File

@ -1,6 +1,26 @@
# java基础
<!-- TOC -->
## HashMap和ConcurrentHashMap
- [HashMap ConcurrentHashMap](#hashmap-concurrenthashmap)
- [java this 和 super的用法](#java-this-和-super的用法)
- [this](#this)
- [super](#super)
- [super和this的异同](#super和this的异同)
- [抽象类和接口](#抽象类和接口)
- [Synchronized 和 volitate区别](#synchronized-和-volitate区别)
- [异常](#异常)
- [String StringBuffer StringBuilder的区别](#string-stringbuffer-stringbuilder的区别)
- [运行速度:](#运行速度)
- [线程安全](#线程安全)
- [总结](#总结)
- [java 如何实现序列化](#java-如何实现序列化)
- [什么是cookieSession和cookie有什么区别](#什么是cookiesession和cookie有什么区别)
- [如何避免死锁](#如何避免死锁)
- [进程和线程区别](#进程和线程区别)
- [参考文档](#参考文档)
<!-- /TOC -->
# HashMap ConcurrentHashMap
HashMap和ConcurrentHashMap的最主要的区别就是前者是线程不安全后者是线程安全的。在不同的JDK版本中区别也不一样
@ -16,8 +36,291 @@ ConcurrentHashMap 不用segment,改成CAS+synchronized方法实现。
CAS 的含义是“我认为原有的值应该是什么,如果是,则将原有的值更新为新值,否则不做修改,并告诉我原来的值是多少”
参考文档:
# java this 和 super的用法
## this
this是自身的一个对象代表对象本身可以理解为指向对象本身的一个指针。
this 的用法主要有三种:
- 普通的直接引用this 相当于当前对象本身
- 形参与成员名字重名用this来区分
- 引用构造函数 **必须放在构造函数的第一行**
``` java
class Person {
private int age = 10;
public Person(){
System.out.println("初始化年龄:"+age);
}
public int GetAge(int age){
this.age = age;
return this.age;
}
}
```
## super
super 指的是自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类。
super 也有三种用法:
- 普通的直接引用
- 子类中的成员变量或方法与父类中的成员变量或方法同名
- 引用构造函数
``` java
class Country {
String name;
void value() {
name = "China";
}
}
class City extends Country {
String name;
void value() {
name = "Shanghai";
super.value(); //调用父类的方法
System.out.println(name);
System.out.println(super.name);
}
public static void main(String[] args) {
City c=new City();
c.value();
}
}
```
**super 作为引用构造函数的时候,必须放在第一行**
``` java
class Person {
public static void prt(String s) {
System.out.println(s);
}
Person() {
prt("父类·无参数构造方法: "+"A Person.");
}//构造方法(1)
Person(String name) {
prt("父类·含一个参数的构造方法: "+"A person's name is " + name);
}//构造方法(2)
}
public class Chinese extends Person {
Chinese() {
super(); // 调用父类构造方法1
prt("子类·调用父类”无参数构造方法“: "+"A chinese coder.");
}
Chinese(String name) {
super(name);// 调用父类具有相同形参的构造方法2
prt("子类·调用父类”含一个参数的构造方法“: "+"his name is " + name);
}
Chinese(String name, int age) {
this(name);// 调用具有相同形参的构造方法3
prt("子类调用子类具有相同形参的构造方法his age is " + age);
}
public static void main(String[] args) {
Chinese cn = new Chinese();
cn = new Chinese("codersai");
cn = new Chinese("codersai", 18);
}
}
```
## super和this的异同
super参数调用基类中的某一个构造函数应该为构造函数中的第一条语句
this参数调用本类中另一种形成的构造函数应该为构造函数中的第一条语句
super: 它引用当前对象的直接父类中的成员用来访问直接父类中被隐藏的父类中成员数据或函数基类与派生类中有相同成员定义时如super.变量名 super.成员函数据名(实参)
this它代表当前对象名在程序中易产生二义性之处应使用this来指明当前对象如果函数的形参与类中的成员数据同名这时需用this来指明成员变量名
调用super()必须写在子类构造方法的第一行否则编译不通过。每个子类构造方法的第一条语句都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错。
super()和this()类似,区别是super()从子类中调用父类的构造方法this()在同一类内调用其它方法。
super()和this()均需放在构造方法内第一行。
尽管可以用this调用一个构造器但却不能调用两个。
this和super不能同时出现在一个构造函数里面因为this必然会调用其它的构造函数其它的构造函数必然也会有super语句的存在所以在同一个构造函数里面有相同的语句就失去了语句的意义编译器也不会通过。
this()和super()都指的是对象所以均不可以在static环境中使用。包括static变量,static方法static语句块。
从本质上讲this是一个指向本对象的指针, 然而super是一个Java关键字。
# 抽象类和接口
抽象类与接口:
抽象类和抽象方法都使用 abstract 进行声明。抽象类一般会包含抽象方法,抽象方法一定位于抽象类中。
抽象类和普通类最大的区别是,抽象类不能被实例化,需要继承抽象类才能实例化其子类。
接口是抽象类的延伸,在 Java 8 之前,它可以看成是一个完全抽象的类,也就是说它不能有任何的方法实现。
从 Java 8 开始,接口也可以拥有默认的方法实现,这是因为不支持默认方法的接口的维护成本太高了。在 Java 8
之前,如果一个接口想要添加新的方法,那么要修改所有实现了该接口的类。
接口的成员(字段 + 方法)默认都是 public 的,并且不允许定义为 private 或者 protected。
接口的字段默认都是 static 和 final 的。
抽象类和接口的比较
| | 抽象类 | 接口
--- | ---| ---
设计| IS-A, 满足里式替换原则| LIKE-A, 提供一个方法实现契约
使用| 单继承| 多实现
字段| 无限制| static or final
方法| 无限制| public
如何使用:
使用抽象类:
需要在几个相关的类中共享代码。
需要能控制继承来的方法和域的访问权限,而不是都为 public。
需要继承非静态non-static和非常量non-final字段。
使用接口:
需要让不相关的类都实现一个方法,例如不相关的类都可以实现 Compareable 接口中的 compareTo() 方法;
需要使用多重继承例如Runnable接口实现线程类
# Synchronized 和 volitate区别
[volatile与synchronized的区别](https://www.cnblogs.com/tf-Y/p/5266710.html)
1) volatile本质是在告诉jvm当前变量在寄存器中的值是不确定的,需要从主存中读取,synchronized则是锁定当前变量,只有当前线程可以访问该变量,其他线程被阻塞住.
2) volatile仅能使用在变量级别,synchronized则可以使用在变量,方法.
3) volatile仅能实现变量的修改可见性,而synchronized则可以保证变量的修改可见性和原子性.(**只能保证单个变量的读写原子性不能保证volatile++这种复合操作的原子性**)
4) volatile不会造成线程的阻塞,而synchronized可能会造成线程的阻塞.
5) 当一个域的值依赖于它之前的值时volatile就无法工作了如n=n+1,n++等。如果某个域的值受到其他域的值的限制那么volatile也无法工作如Range类的lower和upper边界必须遵循lower<=upper的限制。
6) 使用volatile而不是synchronized的唯一安全的情况是类中只有一个可变的域。
# 异常
Throwable 可以用来表示任何可以作为异常抛出的类,分为两种: Error 和 Exception。其中 Error 用来表示 JVM 无法处理的错误Exception 分为两种:
受检异常 :需要用 try...catch... 语句捕获并进行处理,并且可以从异常中恢复;
非受检异常 :是程序运行时错误,例如除 0 会引发 Arithmetic Exception此时程序奔溃并且无法恢复。
![throwable](img/Throwable.png)
[java入门之异常处理](https://www.tianmaying.com/tutorial/Java-Exception)
# String StringBuffer StringBuilder的区别
## 运行速度:
StingBuilder > StringBuffer > String
String为字符串常量而StringBuilder和StringBuffer均为字符串变量即String对象一旦创建之后该对象是不可更改的但后两者的对象是变量是可以更改的
``` java
String str = "abc";
str = str + "de";// 原来的"abc"没了下次GC会被回收掉创建了一个新的str变量
```
## 线程安全
在线程安全上StringBuilder是线程不安全的而StringBuffer是线程安全的
如果一个StringBuffer对象在字符串缓冲区被多个线程使用时StringBuffer中很多方法可以带有synchronized关键字所以可以保证线程是安全的但StringBuilder的方法则没有该关键字所以不能保证线程安全有可能会出现一些错误的操作。所以如果要进行的操作是多线程的那么就要使用StringBuffer但是在单线程的情况下还是建议使用速度比较快的StringBuilder。
## 总结
String适用于少量的字符串操作的情况
StringBuilder适用于单线程下在字符缓冲区进行大量操作的情况
StringBuffer适用多线程下在字符缓冲区进行大量操作的情况
# java 如何实现序列化
OutputStream.writeObject方法可以实现java对象的序列化如果想要java自动实现则需要实现Serializable接口
# 什么是cookieSession和cookie有什么区别
Cookie是会话技术,将用户的信息保存到浏览器的对象.
区别:
(1)cookie数据存放在客户的浏览器上session数据放在服务器上
(2)cookie不是很安全别人可以分析存放在本地的COOKIE并进行COOKIE欺骗,如果主要考虑到安全应当使用session
(3)session会在一定时间内保存在服务器上。当访问增多会比较占用你服务器的性能如果主要考虑到减轻服务器性能方面应当使用COOKIE
(4)单个cookie在客户端的限制是3K就是说一个站点在客户端存放的COOKIE不能3K。
结论:
将登陆信息等重要信息存放为SESSION;其他信息如果需要保留可以放在COOKIE中
# 如何避免死锁
死锁的发生必须满足以下四个条件:
互斥条件:一个资源每次只能被一个进程使用。
请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。
不剥夺条件:进程已获得的资源,在末使用完之前,不能强行剥夺。
循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系。
三种用于避免死锁的技术:
加锁顺序(线程按照一定的顺序加锁)
加锁时限(线程尝试获取锁的时候加上一定的时限,超过时限则放弃对该锁的请求,并释放自己占有的锁)
死锁检测
死锁原因及如何避免更深理解移步http://blog.csdn.net/ls5718/article/details/51896159
# 进程和线程区别
**定义:**
进程:具有一定的独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立的单位
线程进程的一个实体是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源。
- 关系:
一个线程可以创建和撤销另一个线程;同一个进程中的多个线程之间可以并发执行.
相对进程而言,线程是一个更加接近于执行体的概念,它可以与同进程中的其他线程共享数据,但拥有自己的栈空间,拥有独立的执行序列。
- 区别:
进程和线程的主要差别在于它们是不同的操作系统资源管理方式。进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其它进程产生影响,而线程只是一个进程中的不同执行路径。线程有自己的堆栈和局部变量,但线程之间没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率要差一些。但对于一些要求同时进行并且又要共享某些变量的并发操作,只能用线程,不能用进程。
1) 简而言之,一个程序至少有一个进程,一个进程至少有一个线程.
2) 线程的划分尺度小于进程,使得多线程程序的并发性高。
3) 另外,进程在执行过程中拥有独立的内存单元,而多个线程共享内存,从而极大地提高了程序的运行效率。
4) 线程在执行过程中与进程还是有区别的。每个独立的线程有一个程序运行的入口、顺序执行序列和程序的出口。但是线程不能够独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。
5) 从逻辑角度来看,多线程的意义在于一个应用程序中,有多个执行部分可以同时执行。但操作系统并没有将多个线程看做多个独立的应用,来实现进程的调度和管理以及资源分配。这就是进程和线程的重要区别。
- 优缺点:
线程和进程在使用上各有优缺点:
线程执行开销小,但不利于资源的管理和保护;
而进程正相反。
同时线程适合于在SMP机器上运行而进程则可以跨机器迁移。
# 参考文档
- [HashMap? ConcurrentHashMap? 相信看完这篇没人能难住你!](https://crossoverjie.top/2018/07/23/java-senior/ConcurrentHashMap/)
- [Java多线程之CAS](https://blog.csdn.net/u010412719/article/details/52053390)
- [Java中this和super的用法总结](https://www.cnblogs.com/hasse/p/5023392.html)

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

View File

@ -0,0 +1,480 @@
<!-- TOC -->
- [CountDownLatch的用法](#countdownlatch的用法)
- [线程创建](#线程创建)
- [线程间协作](#线程间协作)
- [join](#join)
- [wait](#wait)
- [interrupt](#interrupt)
- [Condition](#condition)
- [Executor 框架](#executor-框架)
- [Executor 主要的类和接口](#executor-主要的类和接口)
- [ThreadPoolExecutor](#threadpoolexecutor)
- [newFixedThreadPool](#newfixedthreadpool)
- [newSingleThreadExecutor](#newsinglethreadexecutor)
- [newScheduledThreadPool](#newscheduledthreadpool)
- [newCachedThreadPool](#newcachedthreadpool)
- [Future 接口](#future-接口)
- [ScheduledThreadPoolExecutor](#scheduledthreadpoolexecutor)
- [参考文档](#参考文档)
<!-- /TOC -->
# CountDownLatch的用法
CountDownLatch 是一个同步工具类,用来协调多个线程之间的同步,或者说起到线程之间的通信。
如果想当前线程在别的线程执行完毕后执行可以使用CountDownLatch。
``` java
/**<p><b>Sample usage:</b> Here is a pair of classes in which a group
* of worker threads use two countdown latches:
* <ul>
* <li>The first is a start signal that prevents any worker from proceeding
* until the driver is ready for them to proceed;
* <li>The second is a completion signal that allows the driver to wait
* until all workers have completed.
* </ul>
*
* <pre> {@code
* class Driver { // ...
* void main() throws InterruptedException {
* CountDownLatch startSignal = new CountDownLatch(1);
* CountDownLatch doneSignal = new CountDownLatch(N);
*
* for (int i = 0; i < N; ++i) // create and start threads
* new Thread(new Worker(startSignal, doneSignal)).start();
*
* doSomethingElse(); // don't let run yet
* startSignal.countDown(); // let all threads proceed
* doSomethingElse();
* doneSignal.await(); // wait for all to finish
* }
* }
*
* class Worker implements Runnable {
* private final CountDownLatch startSignal;
* private final CountDownLatch doneSignal;
* Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
* this.startSignal = startSignal;
* this.doneSignal = doneSignal;
* }
* public void run() {
* try {
* startSignal.await();
* doWork();
* doneSignal.countDown();
* } catch (InterruptedException ex) {} // return;
* }
*
* void doWork() { ... }
* }}</pre>
```
# 线程创建
线程创建通常有3中方法
- 继承 Thread 类
- 实现 Runnable 接口
- 实现 Callable 接口
实现 Runnable 接口来创建一个线程:
``` java
public class MyThread extends Thread {
public static void main(String[] args) {
Thread t1 = new MyThread();
t1.start();
Thread t2 = new Thread(new MyThread2());
t2.start();
Callable call = new MyCallable();
FutureTask<String> task = new FutureTask<String>(call);
Thread t3 = new Thread(task);
t3.start();
}
@Override
public void run() {
System.out.println("线程运行中---------extends");
}
static class MyThread2 implements Runnable{
@Override
public void run() {
System.out.println("线程运行中------runnable");
}
}
static class MyCallable implements Callable{
@Override
public String call() throws Exception {
System.out.println("线程运行中------callable");
return "call over!";
}
}
}
```
# 线程间协作
## join
在线程中调用另一个线程的 join() 方法,会将当前线程挂起,而不是忙等待,直到目标线程结束。
对于以下代码,虽然 b 线程先启动,但是因为在 b 线程中调用了 a 线程的 join() 方法b 线程会等待 a 线程结束才继续执行,因此最后能够保证 a 线程的输出先于 b 线程的输出。
``` java
public class JoinExample {
public static void main(String[] args) {
JoinExample example = new JoinExample();
example.test();
}
private class A extends Thread {
@Override
public void run() {
System.out.println("A");
}
}
private class B extends Thread {
private A a;
B(A a) {
this.a = a;
}
@Override
public void run() {
try {
a.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("B");
}
}
public void test() {
A a = new A();
B b = new B(a);
b.start();
a.start();
}
}
```
## wait
wait 方法会使得当前线程进入等待状态,使用 wait() 挂起期间,线程会释放锁。这是因为,如果没有释放锁,那么其它线程就无法进入对象的同步方法或者同步控制块中,那么就无法执行 notify() 或者 notifyAll() 来唤醒挂起的线程,造成死锁。
wait() 和 sleep() 的区别
- wait() 是 Object 的方法,而 sleep() 是 Thread 的静态方法;
- wait() 会释放锁sleep() 不会。
## interrupt
线程中断的方法有两种:
1. 使用 interrupted()来作为线程循环执行的判断条件
``` java
public class InterruptExample {
private static class MyThread2 extends Thread {
@Override
public void run() {
while (!interrupted()) {
// ..
}
System.out.println("Thread end");
}
}
}
```
2. 使用InterruptedException
通过调用一个线程的 interrupt() 来中断该线程,如果该线程处于阻塞、限期等待或者无限期等待状态,那么就会抛出 InterruptedException从而提前结束该线程。但是不能中断 I/O 阻塞和 synchronized 锁阻塞
对于以下代码,在 main() 中启动一个线程之后再中断它,由于线程中调用了 Thread.sleep() 方法,因此会抛出一个 InterruptedException从而提前结束线程不执行之后的语句。
``` java
public class InterruptExample {
private static class MyThread1 extends Thread {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println("Thread run");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new MyThread1();
thread1.start();
thread1.interrupt();
System.out.println("Main run");
}
```
## Condition
java.util.concurrent 类库中提供了 Condition 类来实现线程之间的协调,可以在 Condition 上调用 await() 方法使线程等待,其它线程调用 signal() 或 signalAll() 方法唤醒等待的线程。相比于 wait() 这种等待方式await() 可以指定等待的条件,因此更加灵活。
``` java
public class AwaitSignalExample {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void before() {
lock.lock();
try {
System.out.println("before");
condition.signalAll();
} finally {
lock.unlock();
}
}
public void after() {
lock.lock();
try {
condition.await();
System.out.println("after");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
```
# Executor 框架
Executor 框架主要由3大部分组成如下
![](img/TIM截图20180801171519.jpg)
- 任务
被执行的任务需要实现 Runnable 或者 Callable接口
- 任务的执行
任务执行的核心接口 Executor, 以及继承自 Executor 的 ExecutorService 接口。 Executor 框架有两个关键类实现了 ExecutorService 接口(ThreadPoolExecutor 和 ScheduledThreadPoolExecutor)
- 异步计算的结果
![](img/TIM截图20180801172104.jpg)
Future 接口和 FutureTask 类
## Executor 主要的类和接口
类和接口 | 说明
--- | ---
Executor | 一个接口其定义了一个接收Runnable对象的方法executor其方法签名为executor(Runnable command), Executor 框架的基础,它将任务的提交和任务的执行分离开来
ExecutorService | 是一个比Executor使用更广泛的子类接口其提供了生命周期管理的方法以及可跟踪一个或多个异步任务执行状况返回Future的方法
AbstractExecutorService | ExecutorService执行方法的默认实现
ThreadPoolExecutor | 线程池的核心实现类用来执行被提交的任务。通过调用Executors以下静态工厂方法来创建线程池并返回一个ExecutorService对象
ScheduledExecutorService | 一个可定时调度任务的接口
ScheduledThreadPoolExecutor | ScheduledExecutorService 的实现类, 可以在给定的延迟后运行命令,或者定期执行命令, 比 Timer更加灵活功能更加强大。
### ThreadPoolExecutor
ThreadPoolExecutor 使用 Executors来创建提供了4种类型的ThreadPoolExectuor, 分别是 newFixedThreadPool 、 newSingleThreadExecutor 、newScheduledThreadPool、 newCachedThreadPool.
- 构造函数
``` java
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
```
**参数说明:**
corePoolSize核心线程数如果运行的线程少于corePoolSize则创建新线程来执行新任务即使线程池中的其他线程是空闲的
maximumPoolSize:最大线程数可允许创建的线程数corePoolSize和maximumPoolSize设置的边界自动调整池大小
corePoolSize <运行的线程数< maximumPoolSize:仅当队列满时才创建新线程
corePoolSize=运行的线程数= maximumPoolSize创建固定大小的线程池
keepAliveTime:如果线程数多于corePoolSize,则这些多余的线程的空闲时间超过keepAliveTime时将被终止
unit:keepAliveTime参数的时间单位
workQueue:保存任务的阻塞队列,与线程池的大小有关:
当运行的线程数少于corePoolSize时在有新任务时直接创建新线程来执行任务而无需再进队列
当运行的线程数等于或多于corePoolSize在有新任务添加时则选加入队列不直接创建线程
当队列满时,在有新任务时就创建新线程
threadFactory:使用ThreadFactory创建新线程默认使用defaultThreadFactory创建线程
handle:定义处理被拒绝任务的策略默认使用ThreadPoolExecutor.AbortPolicy,任务被拒绝时将抛出RejectExecutorException
### newFixedThreadPool
创建可重用且固定线程数的线程池,如果线程池中的所有线程都处于活动状态,此时再提交任务就在队列中等待,直到有可用线程;如果线程池中的某个线程由于异常而结束时,线程池就会再补充一条新线程。
``` java
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
//使用一个基于FIFO排序的阻塞队列在所有corePoolSize线程都忙时新任务将在队列中等待
new LinkedBlockingQueue<Runnable>());
}
```
### newSingleThreadExecutor
创建一个单线程的Executor如果该线程因为异常而结束就新建一条线程来继续执行后续的任务
``` java
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
//corePoolSize和maximumPoolSize都等于表示固定线程池大小为1
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
```
### newScheduledThreadPool
创建一个可延迟执行或定期执行的线程池
``` java
/**
* Creates a new {@code ScheduledThreadPoolExecutor} with the
* given core pool size.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @throws IllegalArgumentException if {@code corePoolSize < 0}
*/
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE,
DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
new DelayedWorkQueue());
}
```
使用 scheduledThread 来模拟心跳:
``` java
public class HeartBeat {
public static void main(String[] args) {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
Runnable task = new Runnable() {
public void run() {
System.out.println("HeartBeat.........................");
}
};
executor.scheduleAtFixedRate(task,5,3, TimeUnit.SECONDS); //5秒后第一次执行之后每隔3秒执行一次
}
}
```
### newCachedThreadPool
创建可缓存的线程池如果线程池中的线程在60秒未被使用就将被移除在执行新的任务时当线程池中有之前创建的可用线程就重用可用线程否则就新建一条线程
``` java
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
//使用同步队列,将任务直接提交给线程
new SynchronousQueue<Runnable>());
}
```
例子:
``` java
public class ThreadPoolTest {
public static void main(String[] args) throws InterruptedException {
ExecutorService threadPool = Executors.newCachedThreadPool();//线程池里面的线程数会动态变化,并可在线程线被移除前重用
for (int i = 1; i <= 3; i ++) {
final int task = i; //10个任务
TimeUnit.SECONDS.sleep(1);
threadPool.execute(new Runnable() { //接受一个Runnable实例
public void run() {
System.out.println("线程名字: " + Thread.currentThread().getName() + " 任务名为: "+task);
}
});
}
}
}
```
输出为每个任务新建一条线程共创建了3条线程
线程名字: pool-1-thread-1 任务名为: 1
线程名字: pool-1-thread-2 任务名为: 2
线程名字: pool-1-thread-3 任务名为: 3
去掉第6行的注释其输出如下始终重复利用一条线程因为newCachedThreadPool能重用可用线程
线程名字: pool-1-thread-1 任务名为: 1
线程名字: pool-1-thread-1 任务名为: 2
线程名字: pool-1-thread-1 任务名为: 3
### Future 接口
Future 接口和实现了Future接口的FutureTask类用来表示异步计算的结果。当我们把Runnable 或者 Callable 接口的实现类提交给ThreadPoolExecutor时Exector会返回给FutureTask对象
### ScheduledThreadPoolExecutor
ScheduledThreadPoolExecutor 继承自 ThreadPoolExecutor, 主要用来在给定的延迟后运行任务。 主要包含3个成员变量
- long: time ; 表示这个任务将要被执行的具体时间
- long: sequenceNumber; 表示这个任务被添加到SheduledThreadPoolExecutor 中的序号
- longperiod; 表示任务执行的间隔周期
该类采用了DelyQueue封装了一个优先队列该队列会对队列中的SheduledFutureTask 进行排序。time小的会排在前面如果time相同则会比较sequenceNumber, 就是说如果两个任务的执行时间相同,谁先提交就谁先执行
# 参考文档
- [java并发编程--Executor框架](https://www.cnblogs.com/MOBIN/p/5436482.html)