实现多线程的方式(四种)
- 继承Thread类
- 实现Runnable接口
- 实现Callable接口
- 使用线程池
方式一:继承Thread类
public class Thread0 extends Thread{
@Override
public void run() {
System.out.println("线程名字:"+Thread.currentThread().getName());
}
public static void main(String[] args) {
Thread0 thread0 = new Thread0();
//设置线程名字
thread0.setName("线程1");
Thread0 thread1 = new Thread0();
thread1.setName("线程2");
thread0.start();
thread1.start();
}
}
运行结果:
线程名字:线程2
线程名字:线程1
方式二:实现Runnable接口
public class Runnable0 implements Runnable{
@Override
public void run() {
System.out.println("线程名字:"+Thread.currentThread().getName());
}
public static void main(String[] args) {
Runnable0 runnable0 = new Runnable0();
Runnable0 runnable1 = new Runnable0();
Thread thread0 = new Thread(runnable0);
Thread thread1 = new Thread(runnable1);
thread0.setName("线程1");
thread1.setName("线程2");
thread0.start();
thread1.start();
}
}
运行结果:
线程名字:线程1
线程名字:线程2
方式三:实现Callable接口
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class Callable0 implements Callable<String> {
public String call() throws Exception {
return Thread.currentThread().getName();
}
public static void main(String[] args) {
Callable0 callable0 = new Callable0();
//执行 Callable,需要 FutureTask 实现类的支持,用于接收运算结果。
FutureTask<String> futureTask = new FutureTask<String>(callable0);
new Thread(futureTask).start();
try {
//接收线程运算后的结果
//FutureTask 可用于 闭锁 类似于CountDownLatch的作用,在所有的线程没有执行完成之后这里是不会执行的
String result = (String) futureTask.get();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
Thread-0
子父类关系:
java.util.concurrent.Future<V>:根接口
java.lang.Runnable:根接口
|--java.util.concurrent.RunnableFuture<V>:继承Future和Runnable接口
|--java.util.concurrent.FutureTask<V>:实现RunnableFuture类
|--java.util.concurrent.FutureTask.FutureTask(Callable<V>):FutureTask类的带参构造方法(参数为Callable实现类)
|--java.util.concurrent.FutureTask.FutureTask(Runnable, V):FutureTask类的带参构造方法(参数为Runnable实现类)
|--java.lang.Thread:实现Runnable接口
方式四:使用线程池
线程池提供了一个线程队列,队列中保存着所有等待状态的线程。避免了创建与销毁的额外开销,提高响应的速度
子父类关系:
java.util.concurrent.Executor : 负责线程的使用与调度的根接口
|--ExecutorService 子接口: 线程池的主要接口
|--ThreadPoolExecutor 线程池的实现类
|--ScheduledExecutorService 子接口:负责线程的调度
|--ScheduledThreadPoolExecutor :继承 ThreadPoolExecutor, 实现 ScheduledExecutorService *
工具类 : Executors
ExecutorService newFixedThreadPool() : 创建一个可重用的固定线程数量的线程池
ExecutorService newCachedThreadPool() : 创建一个可以根据需要创建新线程的线程池
ExecutorService newSingleThreadExecutor() : 创建一个单线程的线程池
ScheduledExecutorService newScheduledThreadPool() : 创建一个可以延迟或定时的执行任务的固定线程数量的线程池
1.newFixedThreadPool使用
public class ThreadPool0 {
public static void main(String[] args) {
fixedThreadPool();
}
/**
* newFixedThreadPool
* 创建一个指定工作线程数量的线程池,每当提交一个任务就创建一个工作线程,当线程 处于空闲状态时,它们并不会被回收,除非线程池被关闭了,如果工作线程数量达到线程池初始的最大数,
* 则将提交的任务存入到池队列(没有大小限制)中。由于newFixedThreadPool只有核心线程并且这些核心线程不会被回收,这样它更加快速底相应外界的请求。
*/
public static void fixedThreadPool(){
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 1; i <= 8; i++) {
final int index = i;
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println("执行时间是:"+System.currentTimeMillis()+"第" +index +"个线程" +Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}
执行结果:
执行时间是:1574668501676第2个线程pool-1-thread-2
执行时间是:1574668501676第1个线程pool-1-thread-1
执行时间是:1574668501676第3个线程pool-1-thread-3
执行时间是:1574668501676第4个线程pool-1-thread-4
执行时间是:1574668501677第5个线程pool-1-thread-5
执行时间是:1574668503676第6个线程pool-1-thread-2
执行时间是:1574668503676第7个线程pool-1-thread-1
执行时间是:1574668503676第8个线程pool-1-thread-3
因为设置了核心线程数为5,所以在第一秒只能执行前5个线程,等待两秒后继续执行后面2个线程
2.newCachedThreadPool使用
public class ThreadPool0 {
public static void main(String[] args) {
cachedThreadPool();
}
/**
* newCachedThreadPool
* 是一种线程数量不定的线程池,并且其最大线程数为Integer.MAX_VALUE,这个数是很大的,一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
* 但是线程池中的空闲线程都有超时限制,这个超时时长是60秒,超过60秒闲置线程就会被回收。调用execute将重用以前构造的线程(如果线程可用)。
* 这类线程池比较适合执行大量的耗时较少的任务,当整个线程池都处于闲置状态时,线程池中的线程都会超时被停止。
*/
public static void cachedThreadPool(){
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 1; i <= 8; i++) {
final int index = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println("执行时间是:"+System.currentTimeMillis()+"第" +index +"个线程" +Thread.currentThread().getName());
}
});
}
}
}
执行结果:
执行时间是:1574668947079第1个线程pool-1-thread-1
执行时间是:1574668948079第2个线程pool-1-thread-1
执行时间是:1574668949080第3个线程pool-1-thread-1
执行时间是:1574668950080第4个线程pool-1-thread-1
执行时间是:1574668951080第5个线程pool-1-thread-1
执行时间是:1574668952081第6个线程pool-1-thread-1
执行时间是:1574668953081第7个线程pool-1-thread-1
执行时间是:1574668954081第8个线程pool-1-thread-1
由于每个任务执行前休眠1秒,所以执行第二个任务的时候第一个任务已经完成,第二个任务会复用执行第一个任务的线程,不用每次新建线程。
3.newSingleThreadExecutor使用
public class ThreadPool0 {
public static void main(String[] args) {
singleThreadExecutor();
}
/**
* newSingleThreadExecutor
* 该类线程池内部只有一个核心线程,以无界队列方式来执行该线程,这使得这些任务之间不需要处理线程同步的问题,它确保所有的任务都在同一个线程中按顺序中执行,
* 并且可以在任意给定的时间不会有多个线程是活动的。
*/
public static void singleThreadExecutor(){
ExecutorService executorService = Executors.newSingleThreadExecutor();
for (int i = 1; i <= 8; i++) {
final int index = i;
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println("执行时间是:"+System.currentTimeMillis()+"第" +index +"个线程" +Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}
执行结果:
执行时间是:1574669442670第1个线程pool-1-thread-1
执行时间是:1574669442671第2个线程pool-1-thread-1
执行时间是:1574669442671第3个线程pool-1-thread-1
执行时间是:1574669442671第4个线程pool-1-thread-1
执行时间是:1574669442671第5个线程pool-1-thread-1
执行时间是:1574669442671第6个线程pool-1-thread-1
执行时间是:1574669442671第7个线程pool-1-thread-1
执行时间是:1574669442671第8个线程pool-1-thread-1
一个线程有顺序地去执行8个任务
4.newScheduledThreadPool使用
延迟执行:
package com.zyf.eflying.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ThreadPool0 {
public static void main(String[] args) {
scheduledThreadPool();
}
/**
* newScheduledThreadPool
* 此线程池,它的核心线程数量是固定的,而非核心线程数是没有限制的,并且当非核心线程闲置时会被立即回收,它可安排给定延迟后运行命令或者定期地执行。
* 这类线程池主要用于执行定时任务和具有固定周期的重复任务。
*
* 延迟执行任务实例
*/
public static void scheduledThreadPool(){
//设置池中核心数量是3
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
System.out.println("执行前的时间:"+System.currentTimeMillis());
scheduledExecutorService.schedule(new Runnable() {
@Override
public void run() {
System.out.println("执行时的时间:"+System.currentTimeMillis());
}
}, 2, TimeUnit.SECONDS);//设置延迟2秒后执行任务
}
}
执行结果:
执行前的时间:1574669740773
执行时的时间:1574669742777
从执行结果可以看出,执行前到执行时延迟2秒后执行任务
延迟+固定周期重复执行 :
package com.zyf.eflying.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ThreadPool0 {
public static void main(String[] args) {
scheduledThreadPool1();
}
/**
* newScheduledThreadPool
* 此线程池,它的核心线程数量是固定的,而非核心线程数是没有限制的,并且当非核心线程闲置时会被立即回收,它可安排给定延迟后运行命令或者定期地执行。
* 这类线程池主要用于执行定时任务和具有固定周期的重复任务。
*
* 固定周期重复执行任务实例
*/
public static void scheduledThreadPool1(){
//设置池中核心数量是3
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
System.out.println("执行前的时间:"+System.currentTimeMillis());
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("执行时的时间:"+System.currentTimeMillis());
}
}, 2, 3,TimeUnit.SECONDS);//这里设置延迟2秒后每3秒执行一次
}
}
执行结果:
执行前的时间:1574669921026
执行时的时间:1574669923033
执行时的时间:1574669926033
执行时的时间:1574669929033
从执行结果可以看出,执行前到执行时延迟2秒,而后每隔三秒循环执行任务
线程池的优点
1.重用线程池中的线程,避免因为线程的创建和销毁带来的性能开销
2.有效控制线程池的最大并发数,避免大量的线程之间因抢占系统资源而阻塞
3.能够对线程进行简单的管理,并提供特定的操作,如定时、定期、单线程、并发数的控制等