forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadPoolExecutorDemo.java
More file actions
50 lines (40 loc) · 1.24 KB
/
Copy pathThreadPoolExecutorDemo.java
File metadata and controls
50 lines (40 loc) · 1.24 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
package ALiBaBa;
import java.util.concurrent.*;
/**
* @Author MaoTian
* @Classname ThreadPoolExecutorDemo
* @Description TODO
* @Date 上午12:03 2019/8/19
* @Version 1.0
* @Created by mao<[email protected]>
*/
class TempThread implements Runnable{
@Override
public void run(){
System.out.println(Thread.currentThread().getName() + "正在被执行");
try{
TimeUnit.SECONDS.sleep(1);
}catch(InterruptedException e){
}
}
}
class TempThread2 implements Callable {
@Override
public String call(){
System.out.println(Thread.currentThread().getName() + "正在被执行");
return Thread.currentThread().getName();
}
}
public class ThreadPoolExecutorDemo{
public static void main(String[] args) throws ExecutionException, InterruptedException {
BlockingQueue<Runnable> bq=new ArrayBlockingQueue<>(10);
ThreadPoolExecutor ex=new ThreadPoolExecutor(3,6,1,TimeUnit.MILLISECONDS,bq);
Runnable t1 = new TempThread();
ex.execute(t1);
TempThread2 t2=new TempThread2();
FutureTask futureTask=new FutureTask(t2);
ex.submit(futureTask);
String s=futureTask.get().toString();
System.out.println(s);
}
}