多线程笔记 | 通过匿名类创建 Runnable 线程
作者:互联网
书上说,执行一次性的线程,可以使用匿名的 Runnable 实例,但是,不知何时使用一次性的线程呢?
public class CreateDemo2New
{
public static final int MAX_TURN = 5;
static int threadNo = 1;
public static void main(String[] args)
{
Thread thread = null;
// 使用 Runnable 的匿名类创建和启动线程
for (int i = 0; i < 2; i ++) {
thread = new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < MAX_TURN; j ++) {
System.out.println(Thread.currentThread().getName() + ", 轮次:" + j);
}
System.out.println(Thread.currentThread().getName() + " 运行结束.");
}
}, "RunnabledThread" + threadNo ++);
thread.start();
}
System.out.println(Thread.currentThread().getName() + " 运行结束.");
}
}
标签:Runnable,currentThread,Thread,int,线程,多线程,public 来源: https://blog.csdn.net/EasySecurity/article/details/122382234