t.join(); // ͤ觹 Threads Тѹ t 稡֧ҧ
:: ¹ҡѹ :: ѡѺ Threads
หน้า 1 จาก 1
t.join(); // ͤ觹 Threads Тѹ t 稡֧ҧ
t.join(); // ͤ觹 Threads Тѹ t 稡֧ҧ
package threads;
public class Sample implements Runnable {
String name;
public Sample(String name) {
this.name = name;
}
public synchronized void run() {
for(int i = 0; i < 20; ++i) {
System.out.println(name+" "+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread t = new Thread(new Sample("A"));
Thread t1 = new Thread(new Sample("B"));
t.start();
// t.stop();
System.out.println("Main0");
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main1");
t1.start();
// t.start();
System.out.println("Main2");
}
}
-------------------------------------
Main0
A 0
A 1
A 2
A 3
A 4
A 5
A 6
A 7
A 8
A 9
A 10
A 11
A 12
A 13
A 14
A 15
A 16
A 17
A 18
A 19
Main1
Main2
B 0
B 1
B 2
B 3
B 4
B 5
B 6
B 7
B 8
B 9
B 10
B 11
B 12
B 13
B 14
B 15
B 16
B 17
B 18
B 19
package threads;
public class Sample implements Runnable {
String name;
public Sample(String name) {
this.name = name;
}
public synchronized void run() {
for(int i = 0; i < 20; ++i) {
System.out.println(name+" "+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread t = new Thread(new Sample("A"));
Thread t1 = new Thread(new Sample("B"));
t.start();
// t.stop();
System.out.println("Main0");
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main1");
t1.start();
// t.start();
System.out.println("Main2");
}
}
-------------------------------------
Main0
A 0
A 1
A 2
A 3
A 4
A 5
A 6
A 7
A 8
A 9
A 10
A 11
A 12
A 13
A 14
A 15
A 16
A 17
A 18
A 19
Main1
Main2
B 0
B 1
B 2
B 3
B 4
B 5
B 6
B 7
B 8
B 9
B 10
B 11
B 12
B 13
B 14
B 15
B 16
B 17
B 18
B 19
:: ¹ҡѹ :: ѡѺ Threads
หน้า 1 จาก 1
Permissions in this forum:
คุณไม่สามารถพิมพ์ตอบ