有返回值的线程

  • 实现CallAble接口
  • 实例:
@SuppressWarnings("unchecked")
public class CallAbleTest {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		CTest c1 = new CTest("a");
		CTest c2 = new CTest("b");
		ExecutorService e = Executors.newFixedThreadPool(2);
		Future<String> f1 = e.submit(c1);
		Future<String> f2 = e.submit(c2);
		System.out.println(f1.get());
		System.out.println(f2.get());
		e.shutdown();
	}
	
	public static class CTest implements Callable{
		
		private String name;
		
		public CTest(String name) {
			this.name = name;
		}
		
		public Object call() throws Exception {
			return name+"返回值";
		}
		
	}
}

posted @ 2019-10-21 09:47  kungFuPander  阅读(85)  评论(0编辑  收藏  举报