FutureTask java

 

在java中执行执行多线程工作流,有时候主线程需要拿到子线程的执行结果,而且会需要根据一点的条件终止子线程的执行,这样的需求用 jdk中的futureTask实现比较好,代码如下

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class TestFutureTask {

	private FutureTask task = new FutureTask(new Callable<String>() {

		@Override
		public String call() throws Exception {

			Thread.sleep(1000 * 3);

			return "hello";
		}

	});

	private void start() throws Exception {
		Thread t = new Thread(task);
		t.run();

		System.out.println(task.get());

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		TestFutureTask test = new TestFutureTask();

		test.start();

	}

}

 

你可能感兴趣的:(java tuthertask)