1. 程式人生 > >java多執行緒之Executor

java多執行緒之Executor

程式

程序:執行的程式

執行緒:程序中負責程式執行的執行單元,一個程序至少包括一個執行緒。

單執行緒:一個程序一個執行緒

多執行緒:一個程序多個執行緒

 

多執行緒是為了更好的利用CPU,提高程式執行的速度。

實現方式:繼承Thread類、實現Runnable介面

public class Test {
    public static void main(String[] args)  {
        MyThread thread = new MyThread();
        thread.start();
    }
}
class MyThread extends Thread{
    private static int num = 0;
    public MyThread(){
        num++;
    }
    @Override
    public void run() {
        System.out.println("主動建立的第"+num+"個執行緒");
    }
}
 
public class Test {
    public static void main(String[] args)  {
        System.out.println("主執行緒ID:"+Thread.currentThread().getId());
        MyRunnable runnable = new MyRunnable();
        Thread thread = new Thread(runnable);
        thread.start();
    }
} 
class MyRunnable implements Runnable{
    public MyRunnable() {
    }
 
    @Override
    public void run() {
        System.out.println("子執行緒ID:"+Thread.currentThread().getId());
    }
}
 

start方法和run方法的區別:start會建立新執行緒,run這是普通的方法呼叫,不會建立新執行緒。

public class Test {
    public static void main(String[] args)  {
        System.out.println("主執行緒ID:"+Thread.currentThread().getId());
        MyThread thread1 = new MyThread("thread1");
        thread1.start();
        MyThread thread2 = new MyThread("thread2");
        thread2.run();
    }
}
 
class MyThread extends Thread{
    private String name;
 
    public MyThread(String name){
        this.name = name;
    }
 
    @Override
    public void run() {
        System.out.println("name:"+name+" 子執行緒ID:"+Thread.currentThread().getId());
    }
}

 

Java多執行緒

帶返回結果之Executor

package threadtst;

import java.util.Date;
import java.util.concurrent.Callable;

public class MyCallable implements Callable<Object> {
	
	private String taskNum;
	
	MyCallable(String taskNum){
		this.taskNum = taskNum;
	}

	@Override
	public Object call() throws Exception {
		System.out.println(">>>"+taskNum+"任務啟動");
		Date date1 = new Date();
		Thread.sleep(1000);
		Date date2 = new Date();
		long time = date1.getTime()-date2.getTime();
		System.out.println(">>>"+taskNum+"任務終止");
		return taskNum+"任務執行時間"+time+"毫秒";
	}

}
package threadtst;

import java.util.ArrayList;
import java.util.List;
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;

public class ExceutorTst {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		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 f = pool.submit(c);
			list.add(f);
		}
		
		pool.shutdown();
		
		for(Future f: list){
			System.out.println(f.get().toString());
		}
	}

}

 

執行結果

 

>>>0任務啟動
>>>2任務啟動
>>>1任務啟動
>>>3任務啟動
>>>4任務啟動
>>>1任務終止
>>>3任務終止
>>>4任務終止
>>>0任務終止
>>>2任務終止
0任務執行時間-1001毫秒
1任務執行時間-1001毫秒
2任務執行時間-1001毫秒
3任務執行時間-1001毫秒
4任務執行時間-1001毫秒
 

執行緒狀態

五種

new

runnable

running

blocked

dead