1번코드
public class NumberPrinter implements Runnable{
private static final int REPETITIONS = 100;
private static final int DELAY = 1000;
private int number;
public NumberPrinter(int n) { number = n; }
public void run() {
try{
for (int i = 1; i <= REPETITIONS; i++){
Thread t = Thread.currentThread();
String name = t.getName();
System.out.println(name + \": \" + i);
Thread.sleep(DELAY);
}
}
catch (InterruptedException exception) {}
}
}
public class NumberThreadRunner{
public static void main(String[] args) {
NumberPrinter r1 = new NumberPrinter(0);
Thread t1 = new Thread(r1);
t1.setName(\"t1\");
Thread t2 = new Thread(r1);
t2.setName(\"t2\");
t1.start();
t2.start();
}
}
2번 코드
public class NumberPrinter implements Runnable{
private static final int REPETITIONS = 100;
private static final int DELAY = 1000;
private int number;
public NumberPrinter(int n) { number = n; }
public void run() {
try{
for (int i = 1; i <= REPETITIONS; i++){
Thread t = Thread.currentThread();
String name = t.getName();
System.out.println(name + \": \" + ++number);
Thread.sleep(DELAY);
}
}
catch (InterruptedException exception) {}
}
}
public class NumberThreadRunner{
public static void main(String[] args) {
NumberPrinter r1 = new NumberPrinter(0);
Thread t1 = new Thread(r1);
t1.setName(\"t1\");
Thread t2 = new Thread(r1);
t2.setName(\"t2\");
t1.start();
t2.start();
}
}
3번코드
public class NumberPrinter implements Runnable{
private static final int REPETITIONS = 100;
private static final int DELAY = 1000;
private int number;
public NumberPrinter(int n) { number = n; }
public void run() {
try{
for (int i = 1; i <= REPETITIONS; i++){
Thread t = Thread.currentThread();
String name = t.getName();
System.out.println(name + \": \" + ++number);
Thread.sleep(DELAY);
}
}
catch (InterruptedException exception) {}
}
}
public class NumberThreadRunner{
public static void main(String[] args) {
NumberPrinter r1 = new NumberPrinter(0);
Thread t1 = new Thread(r1);
t1.setName(\"t1\");
NumberPrinter r2 = new NumberPrinter(0);
Thread t2 = new Thread(r2);
t2.setName(\"t2\");
t1.start();
t2.start();
}
}
이렇게 코드 3개가 각각 어떻게 다르고, 어떨때 어떤 코드 쓰는지좀 설명해줄수 있어?
예나 지금이나 쓰레드 예제는 항상 변하질 않는구나.. 거의 5년만에 보는데도 눈에 익네 -_-;
3분째 보고 있는데 걍 틀린그림찾기 하는 기분.. 어짜피 다 같은 동작 아님?
같은동작이라기보다 비슷한동작이맞긴한데... 과제자체가 이게 어떻게 다른지를 설명하라는 과제라서...