Lang/Java

Thread Runnable 사용 시 setName

hamaganatanadda 2023. 2. 11. 13:23
public class ThreadTest {
public static void main(String[] args) throws InterruptedException {
//1번
Thread thread1 = new Thread(new Worker("11"));
Thread thread2 = new Thread(new Worker("22"));
//2번
//thread1.setName("A2");
//thread2.setName("B2");
thread1.start();
thread2.start();
}
private static class Worker implements Runnable {
private String name;
public Worker(String name) {
this.name = name;
}
@Override
public void run() {
Thread currentThread = Thread.currentThread();
currentThread.setName(this.name);
System.out.println(Thread.currentThread().getName() + "//thread started");
}
}
}