Java多线程使用有三种方法,一种是通过java Thread(类)、Runnable(接口)和ExecutorService(接口)。
第一种:Thread类
public class TestLine {
public static void main(String[] args) throws IOException {
ThreadNumber thread1 = new ThreadNumber("a",1);
ThreadNumber thread2 = new ThreadNumber("z",2);
thread1.start();
thread2.start();
}
private static class ThreadNumber extends Thread{
private String name;
private int index;
public ThreadNumber(String name,int index){
this.name = name;
this.index = index;
}
public void run(){
System.out.println(this.index + " = " + this.name);
}
}
}
第二种:Runnable接口
public class TestLine {
public static void main(String[] args) throws IOException {
Thread thread1 = new Thread(new ThreadNumber("a",1));
Thread thread2 = new Thread(new ThreadNumber("z",2));
thread1.start();
thread2.start();
}
private static class ThreadNumber implements Runnable{
private String name;
private int index;
public ThreadNumber(String name,int index){
this.name = name;
this.index = index;
}
public void run(){
System.out.println(this.index + " = " + this.name);
}
}
}
第三种:ExecutorService接口
public class TestLine { public static void main(String[] args) throws IOException, InterruptedException { //声明线程数 5个线程 ExecutorService threadPool = Executors.newFixedThreadPool(5); //分配线程,可以分配多个任务,任务会存放到对列中等待, //根据声明时指定的当前执行线程数量进行执行任务,其它任务等待空闲线程 for(int i = 0; i < 10; i++){ threadPool.execute(new ThreadNumber("name" + i,i)); } // 关闭启动线程,不再向对列中存放任务,已有的任务线程(包含对列中任务)会全部处理 threadPool.shutdown(); //等待所有子线程执行完成,设置等待时长,该设置会等待所有任务和线程执行完成或者超时 threadPool.awaitTermination(15, TimeUnit.DAYS); System.out.println("所有线程执行完成"); } private static class ThreadNumber extends Thread{ private String name; private int index; public ThreadNumber(String name,int index){ this.name = name; this.index = index; } public void run(){ System.out.println(this.index + " = " + this.name + "," + new Date()); } } }
ExecutorService声明线程使用的三种模式 ( 转 )
1.CachedThreadPool
CachedThreadPool首先会按照需要创建足够多的线程来执行任务(Task)。随着程序执行的过程,有的线程执行完了任务,可以被重新循环使用时,才不再创建新的线程来执行任务。我们采用《Thinking In Java》中的例子来分析。
首先,任务定义如下(实现了Runnable接口,并且复写了run方法):
package net.jerryblog.concurrent;
public class LiftOff implements Runnable{
protected int countDown = 10; //Default
private static int taskCount = 0;
private final int id = taskCount++;
public LiftOff() {}
public LiftOff(int countDown) {
this.countDown = countDown;
}
public String status() {
return "#" + id + "(" +
(countDown > 0 ? countDown : "LiftOff!") + ") ";
}
@Override
public void run() {
while(countDown-- > 0) {
System.out.print(status());
Thread.yield();
}
}
}
采用CachedThreadPool方式执行编写的客户端程序如下:
package net.jerryblog.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CachedThreadPool {
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
for(int i = 0; i < 10; i++) {
exec.execute(new LiftOff());
}
exec.shutdown();
}
}
上面的程序中,有10个任务,采用CachedThreadPool模式,exec每遇到一个LiftOff的对象(Task),就会创建一个线程来处理任务。现在假设遇到到第4个任务时,之前用于处理第一个任务的线程已经执行完成任务了,那么不会创建新的线程来处理任务,而是使用之前处理第一个任务的线程来处理这第4个任务。接着如果遇到第5个任务时,前面那些任务都还没有执行完,那么就会又新创建线程来执行第5个任务。否则,使用之前执行完任务的线程来处理新的任务。
2.FixedThreadPool
FixedThreadPool模式会使用一个优先固定数目的线程来处理若干数目的任务。规定数目的线程处理所有任务,一旦有线程处理完了任务就会被用来处理新的任务(如果有的话)。这种模式与上面的CachedThreadPool是不同的,CachedThreadPool模式下处理一定数量的任务的线程数目是不确定的。而FixedThreadPool模式下最多的线程数目是一定的。
采用FixedThreadPool模式编写客户端程序如下:
package net.jerryblog.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FixedThreadPool {
public static void main(String[] args) {
//三个线程来执行五个任务
ExecutorService exec = Executors.newFixedThreadPool(3);
for(int i = 0; i < 5; i++) {
exec.execute(new LiftOff());
}
exec.shutdown();
}
}
3.SingleThreadExecutor模式
SingleThreadExecutor模式只会创建一个线程。它和FixedThreadPool比较类似,不过线程数是一个。如果多个任务被提交给SingleThreadExecutor的话,那么这些任务会被保存在一个队列中,并且会按照任务提交的顺序,一个先执行完成再执行另外一个线程。
SingleThreadExecutor模式可以保证只有一个任务会被执行。这种特点可以被用来处理共享资源的问题而不需要考虑同步的问题。
SingleThreadExecutor模式编写的客户端程序如下:
package net.jerryblog.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SingleThreadExecutor {
public static void main(String[] args) {
ExecutorService exec = Executors.newSingleThreadExecutor();
for (int i = 0; i < 2; i++) {
exec.execute(new LiftOff());
}
}
}
这种模式下执行的结果如下:
#0(9) #0(8) #0(7) #0(6) #0(5) #0(4) #0(3) #0(2) #0(1) #0(LiftOff!)
#1(9) #1(8) #1(7) #1(6) #1(5) #1(4) #1(3) #1(2) #1(1) #1(LiftOff!)
第一个任务执行完了之后才开始执行第二个任务。