Created
February 20, 2019 09:12
-
-
Save mikeyang01/abe47417404ab7988f7a6bf98670620e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
public static void main(String[] args) { | |
ExecutorService executor = Executors.newCachedThreadPool(); | |
Task task = new Task(); | |
Future<Integer> result = executor.submit(task); | |
executor.shutdown(); | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e1) { | |
e1.printStackTrace(); | |
} | |
System.out.println("主线程在执行任务"); | |
try { | |
System.out.println("task运行结果"+result.get()); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} catch (ExecutionException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("所有任务执行完毕"); | |
} | |
} | |
class Task implements Callable<Integer>{ | |
@Override | |
public Integer call() throws Exception { | |
System.out.println("子线程在进行计算"); | |
Thread.sleep(3000); | |
int sum = 0; | |
for(int i=0;i<100;i++) | |
sum += i; | |
return sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment