-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartTest.java
More file actions
77 lines (68 loc) · 2.4 KB
/
Copy pathStartTest.java
File metadata and controls
77 lines (68 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package ThreadTest;
import java.util.ArrayList;
import java.util.concurrent.*;
// Runnable 接口定义一个可执行任务,之后依托Thread类来执行
class task implements Runnable{
private static int startCount=10;
private int id=startCount++;
public task(){}
@Override
public void run() {
while(id-->0){
System.out.println("$"+id);
}
}
}
/*
*Callable是一个具有类型参数的泛型,Callable<String> 的声明要求 call函数的返回值也是String
* Callable对象的调用必须依赖于Executors管理器,Pool.submit(Callable c) 返回一个Future<T> 对象
* Future对象可以调用IsDone方法检查任务是否被执行完毕,返回一个boolean值
* */
class CallTask implements Callable{
private int id;
CallTask(int id){
this.id=id;
}
@Override
public Object call() throws Exception {
return "this.id="+id;
}
}
/*sleep方法,使当前线程停止指定的时间,和Thread.yield()方法不同,
yield()方法调用之后将其交给线程调度器去管理,选择性的停止和开始线程,当在一个单一线程里边调用yield时,效果和不调用一样,
因为只有一个线程在调用,切换失败
* */
public class StartTest {
public static void main(String[] args) throws InterruptedException, ExecutionException {
//1.通过Executors新建一个线程池
ExecutorService executorService=Executors.newCachedThreadPool();
ArrayList<Future<String>> futures=new ArrayList<>();
for (int i=0;i<5;i++){
futures.add(executorService.submit(new CallTask(i)));
}
for (Future<String> future:futures){
try{
System.out.println(future.get()+" "+future.isDone());
}catch (InterruptedException e){
e.printStackTrace();
}catch (ExecutionException e){
e.printStackTrace();
} finally {
executorService.shutdown();
}
}
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000); //主线程休眠
} catch (InterruptedException e) {
}
System.out.print("2"); //1
}
});
t.start();
t.join();
System.out.print("1"); //2
}
}