?? callabletest.java
字號:
import java.util.concurrent.*;
/**
* Description:
* <br/>Copyright (C), 2008-2010, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
class RtnThread implements Callable<Integer>
{
//實現call方法,作為線程執行體
public Integer call()
{
int i = 0;
for ( ; i < 100 ; i++ )
{
System.out.println(Thread.currentThread().getName()
+ " 的循環變量i的值:" + i);
}
//call()方法可以有返回值
return i;
}
}
public class CallableTest
{
public static void main(String[] args)
{
//創建Callable對象
RtnThread rt = new RtnThread();
//使用FutureTask來包裝Callable對象
FutureTask<Integer> task = new FutureTask<Integer>(rt);
for (int i = 0 ; i < 100 ; i++)
{
System.out.println(Thread.currentThread().getName()
+ " 的循環變量i的值:" + i);
if (i == 20)
{
//實質還是以Callable對象來創建、并啟動線程
new Thread(task , "有返回值的線程").start();
}
}
try
{
//獲取線程返回值
System.out.println("子線程的返回值:" + task.get());
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -