public class SynDemo1 extends Thread {
public static void main(String[] args) {
Web12306 web = new Web12306();
Thread a1 = new Thread(web, "a");
Thread a2 = new Thread(web, "b");
Thread a3 = new Thread(web, "c");
a1.start();
a2.start();
a3.start();
}
}
class Web12306 implements Runnable {
private int i = 10;
public void run() {
try {
add();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add() throws InterruptedException {
while (i > 0) {
synchronized (Web12306.class) {
if (i <= 0) {
break;
} else {
System.out.println(Thread.currentThread().getName() + ":" + i--);
Thread.sleep(1000);
}
}
}
}
}
public static void main(String[] args) {
Web12306 web = new Web12306();
Thread a1 = new Thread(web, "a");
Thread a2 = new Thread(web, "b");
Thread a3 = new Thread(web, "c");
a1.start();
a2.start();
a3.start();
}
}
class Web12306 implements Runnable {
private int i = 10;
public void run() {
try {
add();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add() throws InterruptedException {
while (i > 0) {
synchronized (Web12306.class) {
if (i <= 0) {
break;
} else {
System.out.println(Thread.currentThread().getName() + ":" + i--);
Thread.sleep(1000);
}
}
}
}
}