多线程:FutureTask方式应用以及jdk源码分析

多线程:FutureTask模式应用以及jdk源码分析
多线程三种方式:
http://blog.****.net/aboy123/article/details/38307539
runnable与callable区别:
https://www.cnblogs.com/frinder6/p/5507082.html

package com.hailong.hailongTest.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import com.hailong.hailongTest.InsertSort;
import com.hailong.hailongTest.SelectSort;

public class UserServiceByFuture {
	
	public static Map<String, String> UserServiceByFuture(final int[] testInts1, final int[] testInts2) {
		
//		InsertSort insertSort = new InsertSort();
//		SelectSort selectSort = new SelectSort();
//		Map<String, String> result = new HashMap<String, String>();
//		result.put("a", insertSort.getInsertSort(testInts1));
//		result.put("b", selectSort.getSelectSort(testInts2));
		
		//-----------------------------------------------------------
		Callable<String> callableString = new Callable<String>() {

			public String call() throws Exception {
				
				InsertSort insertSort = new InsertSort();
				return insertSort.getInsertSort(testInts1);
				// TODO Auto-generated method stub
			}
		};
		
		FutureTask<String> futureTask = new FutureTask<String>(callableString);
		new Thread(futureTask).start();
		//-----------------------------------------------------------
		Callable<String> callableString2 = new Callable<String>() {

			public String call() throws Exception {
				
				SelectSort selectSort = new SelectSort();
				return selectSort.getSelectSort(testInts2);
				// TODO Auto-generated method stub
			}
		};
		
		FutureTask<String> futureTask2 = new FutureTask<String>(callableString2);
		new Thread(futureTask2).start();
		//-----------------------------------------------------------
		Map<String, String> result = new HashMap<String, String>();
		try {
			result.put("a", futureTask.get());
			result.put("b", futureTask2.get());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}
	
	public static void main(String[] args) {
		long currentTime = System.currentTimeMillis();
		int[] testInts1 = {1,44,6,2,45,98,45};
		int[] testInts2 = {1,44,6,2,45,98,45};
		// TODO Auto-generated method stub
		Map<String, String> result = UserServiceByFuture(testInts1, testInts2);
		
		System.out.println(result.get("a"));
		System.out.println(result.get("b"));
		System.out.println(System.currentTimeMillis() - currentTime);
	}

}