一、主線程等待法:優點:實現簡單,缺點:代碼冗余
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
|
package
com.test.thread;
public
class
CycleWait
implements
Runnable {
private
String value;
@Override
public
void
run() {
try
{
Thread.sleep(
5000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
value =
"we have data now"
;
}
public
static
void
main(String[] args)
throws
InterruptedException {
CycleWait cycleWait =
new
CycleWait();
Thread t =
new
Thread(cycleWait);
t.start();
while
(cycleWait.value ==
null
) {
Thread.sleep(
100
);
}
System.out.println(cycleWait.value);
}
}
|
運行結果:
1
|
we have data now
|
二、使用Thread類的join()阻塞當前線程,以等待子線程處理完畢。優點:比“主線程等待法”更簡單 缺點:粒度不夠細
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package
com.test.thread;
public
class
CycleWait
implements
Runnable {
private
String value;
@Override
public
void
run() {
try
{
Thread.sleep(
5000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
value =
"we have data now"
;
}
public
static
void
main(String[] args)
throws
InterruptedException {
CycleWait cycleWait =
new
CycleWait();
Thread t =
new
Thread(cycleWait);
t.start();
// join方法,在start后
t.join();
System.out.println(cycleWait.value);
}
}
|
三、通過Callable接口實現:通過FutureTask 或者 線程池獲取
1、future task
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
MyCallable.
class
:
package
com.test.thread;
import
java.util.concurrent.Callable;
// 實現callable接口
public
class
MyCallable
implements
Callable<String> {
@Override
public
String call()
throws
Exception {
String value =
"test"
;
System.out.println(
"ready to work"
);
Thread.sleep(
5000
);
System.out.println(
"task done"
);
return
value;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
FutureTaskDemo.
class
:
package
com.test.thread;
import
java.util.concurrent.ExecutionException;
import
java.util.concurrent.FutureTask;
//future task
public
class
FutureTaskDemo {
public
static
void
main(String[] args)
throws
ExecutionException, InterruptedException {
FutureTask<String> task =
new
FutureTask<String>(
new
MyCallable());
new
Thread(task).start();
if
(!task.isDone()) {
System.out.println(
"task has not finished, please wait!"
);
}
System.out.println(
"task return:"
+ task.get());
}
}
|
2、線程池
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
|
TreadPoolDemo.
class
:
package
com.test.thread;
import
java.util.concurrent.ExecutionException;
import
java.util.concurrent.ExecutorService;
import
java.util.concurrent.Executors;
import
java.util.concurrent.Future;
public
class
TreadPoolDemo {
public
static
void
main(String[] args) {
// 創建線程池
ExecutorService executorService =
Executors.newCachedThreadPool();
// 向線程池中提交任務
Future<String> future = executorService.submit(
new
MyCallable());
// 判斷任務是否完成
if
(!future.isDone()) {
System.out.println(
"task not finished, please wait~~"
);
}
try
{
System.out.println(future.get());
}
catch
(InterruptedException e) {
e.printStackTrace();
}
catch
(ExecutionException e) {
e.printStackTrace();
}
finally
{
// 將線程池關閉
executorService.shutdown();
}
}
}
|