/*
* 作成日: 2006/06/14
*
* shinnou 追加宿題の解答
*/
public class Thread1 extends Thread {
public void run() {
try {
for(int i = 0;i < 10;i++) {
Thread.sleep(1000);
System.out.println("Thread1");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public class Thread2 extends Thread {
public void run() {
try {
for(int i = 0;i < 3;i++) {
Thread.sleep(5000);
System.out.println("Thread2");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public class ShinnouHW {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.start();
try {
t1.join();
t2.join();
System.out.println("Both threads have finished");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
実行例
>java ShinnouHW
Thread1
Thread1
Thread2
Thread1
Thread1
Thread1
Thread1
Thread1
Thread2
Thread1
Thread2
Both threads have finished
>