问个简单的问题,在给现成设定了名字时,为什么执行run方法中打印线程的名字都是系统指定的名字,而不是我设定的名字?
3 回答
public class TestThread {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(2);
RunnableTest rt = new RunnableTest();
Thread t1 = new Thread(rt, "t1");
Thread t2 = new Thread(new RunnableTest(), "t2");
service.execute(t1);
service.execute(t2);
}
}
class RunnableTest implements Runnable {
public void run() {
for (int i = 0; i <= 20; i++) {
// 得到当前线程的名字
String str = Thread.currentThread().getName();
System.out.println(str + ":" + i);
}
}
}
举报
0/150
提交
取消