大家已经习惯用Runnable接口了,先看看Callable和Runnable接口的区别
//代码
package cn;
import java.util.concurrent.Callable;
public class Thee implements Callable{
String result="";
@Override
public Object call() throws Exception {
// TODO Auto-generated method stub
int i=0;
while(true){
i++;
Thread.sleep(1000);
System.out.println("现在i是:"+i);
if(i>2){
result="给result赋值,呵呵";
return result;
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
Thee tc=new Thee();
String vv;
try {
vv = tc.call().toString();
System.out.println("vv is--"+vv);
//Thread th1=new Thread(tc);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//输出
现在i是:1
现在i是:2
现在i是:3
vv is--给result赋值,呵呵