一、继承Java.lang.Thread类(线程子类)
public class Demo02 {
public static void main(String[] args) {
SubThread st1 = new SubThread("线程甲");
SubThread st2 = new SubThread("线程乙");
SubThread st3 = new SubThread("线程丙");
st1.start();
st2.start();
st3.start();
}
}
//继承Thread,重写run()方法
class SubThread extends Thread{
public SubThread(String name) {
super(name);
}
@Override
public void run(){
for(char c = 'A';c <= 'x';c++) {
System.out.printf("[%s]:%s\n",this.getName(),c);
}
}
}
二、实现Java.lang.Runnable接口(线程执行类)
public class Demo03 {
public static void main(String[] args) {
//每个Runnable接口的实现类,封装了线程的执行逻辑
EmailTask emailTask = new EmailTask();
//创建3个线程,分别发送邮件
Thread th1 = new Thread(emailTask,"线程1");
Thread th2 = new Thread(emailTask,"线程2");
Thread th3 = new Thread(emailTask,"线程3");
//启动线程
th1.start();
th2.start();
th3.start();
}
}
//子类
class EmailTask extends Task implements Runnable{
@Override
public void execute() {
//获取当前线程对象
Thread currentThread = Thread.currentThread();
//获取线程名称
String name = currentThread.getName();
System.out.println(name+":使用Java Email技术,通过smtp协议发送邮件");
}
@Override
public void run() {
execute();
}
}
//父类
abstract class Task{
public abstract void execute();
}
三、实现Java.util.concurrent.Callable接口
允许子线程返回结果、抛出异常
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Demo04 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
//Callable接口实现类:不同数据范围的计算任务
SumCallTask st1 = new SumCallTask(1,200);//1-200
SumCallTask st2 = new SumCallTask(201,400);//201-400
SumCallTask st3 = new SumCallTask(401,500);//401-500
//Callable ---> FutureTask(Runnable接口实现类)
FutureTask<Integer> futureTask1 = new FutureTask<Integer>(st1);
FutureTask<Integer> futureTask2 = new FutureTask<Integer>(st2);
FutureTask<Integer> futureTask3 = new FutureTask<Integer>(st3);
//创建并启动线程
Thread th1 = new Thread(futureTask1);
Thread th2 = new Thread(futureTask2);
Thread th3 = new Thread(futureTask3);
th1.start();
th2.start();
th3.start();
//线程执行结束,分别获取各自线程的返回结果
System.out.println("开始分别获取");
Integer sum1 = futureTask1.get();
Integer sum2 = futureTask2.get();
Integer sum3 = futureTask3.get();
System.out.println("sum1="+sum1);
System.out.println("sum2="+sum2);
System.out.println("sum3="+sum3);
//汇总
System.out.println("汇总");
Integer sum = sum1 + sum2 + sum3;
System.out.println("最终结果="+sum);
}
}
//通过Callable接口实现类SumCallTask封装某个范围内数据的累加和
class SumCallTask implements Callable<Integer>{
private int begin,end;
public SumCallTask(int begin, int end) {
this.begin = begin;
this.end = end;
}
@Override
public Integer call() {
int total = 0;
for(int i = begin;i <= end;i++) {
total += 1;
}
return total;
}
}
四、线程池
线程池,按照配置参数创建并管理若干线程对象。程序中如果需要使用线程,将一个执行任务传给线程池,线程池就会使用一个空闲状态的线程来执行这个任务。执行结束以后,该线程并不会死亡,而是再次返回线程池中成为空闲状态,等待执行下一个任务。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//通过线程池创建
public class Demo05 {
public static void main(String[] args) {
//创建固定大小的线程池(10)
ExecutorService threadPool = Executors.newFixedThreadPool(10);
//不确定数量的线程请求
while(true) {
//向线程池提交一个执行任务(Runnable接口实现类对象)
//线程池分配一个“空闲线程”执行该任务
//如果没有空闲线程,则该任务进入“等待队列(工作队列)”
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("当前运行的线程名为: " + Thread.currentThread().getName());
try {
Thread.sleep(1000);//当前线程休眠1000毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}