三個線程同時輸出12345 678910 1112131415....以此類推
阿新 • • 發佈:2017-07-04
ext sta .... clas star start extends for read
package thread;
public class th {
public static int j = 0;
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
t tt = new t();
tt.start();
th.xun();
System.out.println("線程1");
d dd = new d(); dd.start();
}
}
public static synchronized void xun()
{
for (int i = 0; i < 5; i++) {
th.j++;
System.out.println(j);
}
}
}
class t extends Thread{ public void run() {
th.xun();
System.out.println("線程2");
super.run();
}
}
class d extends Thread{ public void run() {
th.xun();
System.out.println("線程3");
super.run();
}
}
三個線程同時輸出12345 678910 1112131415....以此類推