728x90
1.
class SimpleThread extends Thread{
public SimpleThread(String name) {
super(name);
}
public void run() {
for(int i=0; i<3; i++) {
System.out.println(i + " " + getName());
try {
sleep((int)(Math.random()*1000));
}catch(InterruptedException e) {}
}
System.out.println("DONE! " + getName());
}
}
public class ExerciseCh11_6 {
public static void main(String[] args) {
new SimpleThread("Seoul").start();
new SimpleThread("Pusan").start();
}
}
2.
class MyThread extends Thread{
MyThread(String name){
super(name);
}
public void run() {
while(true) {
System.out.println(getName() + " is now running");
try {
sleep(5*10000);
}catch(InterruptedException e) {
System.out.println("Interrupted is received");
break;
}
}//end while
}//end run()
}//end MyThread
public class ExerciseChp11_7 {
public static void main(String[] args) {
MyThread t1 = new MyThread("ThreadOne");
MyThread t2 = new MyThread("ThreadTwo");
t1.start();
t2.start();
try {
Thread.sleep(30*1000);
}catch(Exception e) {}
t1.interrupt();
t2.interrupt();
}
}
반응형
'전.java' 카테고리의 다른 글
[Java] StringTokenizer (0) | 2023.01.16 |
---|---|
[Java] 입출력 (0) | 2023.01.16 |
단정 (0) | 2021.02.14 |
예외 처리 (0) | 2021.02.14 |
도형 클래스 (Figure) (0) | 2021.02.09 |