线程创建
线程创建
- 继承
Thread 类,重写run() ; - 实现
Runnable 接口,重写run() ; - 实现
Callable 接口,实现call() 方法,通过FutureTask 包装器来创建Thread 线程; - 使用
ExecutorService 、Callable、Future 实现有返回结果的多线程。
前面两种可以归结为一类:无返回值,原因很简单,通过重写
继承Thread 类的方式
public class MyThread extends Thread {
public void run() {
System.out.println("MyThread.run()");
}
}
MyThread myThread1 = new MyThread();
MyThread myThread2 = new MyThread();
myThread1.start();
myThread2.start();
实现Runnable 接口的方式
如果自己的类已经
public class ThreadDemo02 {
public static void main(String[] args){
System.out.println(Thread.currentThread().getName());
Thread t1 = new Thread(new MyThread());
t1.start();
}
}
class MyThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName()+"-->我是通过实现接口的线程实现方式!");
}
}
实现
- 适合多个相同的程序代码的线程去处理同一个资源;
- 可以避免
Java 中的单继承的限制; - 增加程序的健壮性,代码可以被多个线程共享,代码和数据独立;
- 线程池只能放入实现
Runable 或callable 类线程,不能直接放入继承Thread 的类
实现Callable 接口,并通过FutureTask 包装器来创建Thread 线程
- 创建
Callable 接口的实现类 ,并实现call() 方法; - 创建
Callable 实现类的实现,使用FutureTask 类包装Callable 对象,该FutureTask 对象封装了Callable 对象的call() 方法的返回值; - 使用
FutureTask 对象作为Thread 对象的target 创建并启动线程; - 调用
FutureTask 对象的get() 来获取子线程执行结束的返回值;
public class ThreadDemo03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Callable<Object> oneCallable = new Tickets<Object>();
FutureTask<Object> oneTask = new FutureTask<Object>(oneCallable);
Thread t = new Thread(oneTask);
System.out.println(Thread.currentThread().getName());
t.start();
}
}
class Tickets<Object> implements Callable<Object>{
//重写call方法
@Override
public Object call() throws Exception {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName()+"-->我是通过实现Callable接口通过FutureTask包装器来实现的线程");
return null;
}
}
使用ExecutorService 、Callable、Future 实现有返回结果的线程
ExecutorService、Callable、
执行
import java.util.concurrent.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
/**
* 有返回值的线程
*/
@SuppressWarnings("unchecked")
public class Test {
public static void main(String[] args) throws ExecutionException,
InterruptedException {
System.out.println("----程序开始运行----");
Date date1 = new Date();
int taskSize = 5;
// 创建一个线程池
ExecutorService pool = Executors.newFixedThreadPool(taskSize);
// 创建多个有返回值的任务
List<Future> list = new ArrayList<Future>();
for (int i = 0; i < taskSize; i++) {
Callable c = new MyCallable(i + " ");
// 执行任务并获取Future对象
Future f = pool.submit(c);
// System.out.println(">>>" + f.get().toString());
list.add(f);
}
// 关闭线程池
pool.shutdown();
// 获取所有并发任务的运行结果
for (Future f : list) {
// 从Future对象上获取任务的返回值,并输出到控制台
System.out.println(">>>" + f.get().toString());
}
Date date2 = new Date();
System.out.println("----程序结束运行----,程序运行时间【"
+ (date2.getTime() - date1.getTime()) + "毫秒】");
}
}
class MyCallable implements Callable<Object> {
private String taskNum;
MyCallable(String taskNum) {
this.taskNum = taskNum;
}
public Object call() throws Exception {
System.out.println(">>>" + taskNum + "任务启动");
Date dateTmp1 = new Date();
Thread.sleep(1000);
Date dateTmp2 = new Date();
long time = dateTmp2.getTime() - dateTmp1.getTime();
System.out.println(">>>" + taskNum + "任务终止");
return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";
}
}