java多线程之Executor

版权声明:本文为博主原创文章,转载请注明作者和出处。 https://blog.csdn.net/xinqing5130/article/details/80478309

程序

进程:运行的程序

线程:进程中负责程序执行的执行单元,一个进程至少包括一个线程。

单线程:一个进程一个线程

多线程:一个进程多个线程

多线程是为了更好的利用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

猜你喜欢

转载自blog.csdn.net/xinqing5130/article/details/80478309