获取线程处理结果的一个方法

时间:2022-12-20 08:55:05

 

 

package thread.demo;

import java.util.ArrayList;
import java.util.List;

/**
* @Description:
* @创建人:helloworld.tang@qq.com
* @创建时间:2015-8-15 下午10:32:23
*
*/

public class ThreadFiledValue {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {

Task task = new Task();
Thread thread = new Thread(task);
thread.start();
while (thread.isAlive()) {
System.out.println(Thread.currentThread() + "is alive");
}
for (String temp : task.getList()) {
System.out.println(temp);
}

}

}

class Task implements Runnable {
private List<String> list = new ArrayList<String>();

public List<String> getList() {
return list;
}

public void run() {
for (int i = 0; i < 10; i++) {
list.add(Thread.currentThread() + ":no->" + i);
}
}

}