ForkJoinTask以及实现callable接口创建线程的学习

package ssl;


import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;


//要实现一个ForkJoinTask需要继承RecursiveTask,并实现compute方法,T为该方法的返回类型
public class ForkJoinTest extends RecursiveTask{


//设置阈值
private static final int THRESHOLD=2;
private int start;
private int end;


public ForkJoinTest(int start, int end){
this.start=start;
this.end=end;
}

//ForkJoinTask需要实现compute方法
@Override
protected Integer compute() {
int sum=0;

//如果任务足够小就执行任务
boolean canCompute =(end-start)<=THRESHOLD;
if(canCompute){
for(int i=start;i<=end;i++){
sum+=i;
}
}else{
//如果任务大于阈值,就分裂成两个子任务计算
int middle=(start+end)/2;
ForkJoinTest leftTask=new ForkJoinTest(start, middle);
ForkJoinTest rightTask=new ForkJoinTest(middle+1, end);
//执行子任务,每个子任务执行fork()方法时,又会进入compute方法
leftTask.fork();
rightTask.fork();
//等待子任务执行完,并得到其结果,执行join方法会等待子任务执行完并得到其结果
int leftResult=leftTask.join();
int rightResult=rightTask.join();

//合并子任务
sum=leftResult+rightResult;
}

return sum;
}

public static void main(String[] args) {
//ForkJoinTask需要ForkJoinPool对象来执行
ForkJoinPool forkJoinPool=new ForkJoinPool();
//生成一个计算任务,负责计算1+2+3+4
ForkJoinTest Task=new ForkJoinTest(1, 4);
//调用forkJoinPool的sumbit(任务)方法来执行任务,
Future result=forkJoinPool.submit(Task);

try{
System.out.println(result.get());
}catch(InterruptedException e){
}catch(ExecutionException e){
}
}


}

实现callable接口创建线程学习
package ssl;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestCallable {

	public static void main(String[] args) {
		//创建一个线程池
		ExecutorService executorService=Executors.newCachedThreadPool();
		Executor executor=Executors.newCachedThreadPool();
		//创建线程
		C c=new C();
		//通过Executor对象的execute方法执行c线程
		executor.execute(c);
		//new一个任务
		Task task=new Task();
		//通过线程池对象提交任务,返回给Future对象
		Future result=executorService.submit(task);
		executorService.shutdown();
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println("主线程在执行...");
		
		try {
			//通过Future对象的get方法获取线程返回结果
			System.out.println("Task线程执行结果:"+result.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
		System.out.println("所有线程执行完毕...");
	}

}

//泛型为call方法的返回类型
class Task implements Callable{

	//核心call方法,有返回值
	@Override
	public Integer call() throws Exception {
		System.out.println("子线程在执行");
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		int sum=0;
		for(int i=0;i<10;i++){
			sum+=i;
		}
		//返回该线程的执行结果
		return sum;
	}
}

class C implements Runnable{

	@Override
	public void run() {
		System.out.println("c");
	}
	
}


 
 

你可能感兴趣的:(java)