日期:2014-05-18 浏览次数:20904 次
public class Demo{
public static int rocket = 20;
public static void main(String[] args){
MyThread mt1 = new MyThread();
Thread t1 = new Thread(mt1);
MyThread2 t2 = new MyThread2();
t1.start();
t2.start();
}
}
class MyThread implements Runnable{
public void run(){
synchronized("11"){
while(Demo.rocket>0){
if(Demo.rocket==1){
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
Demo.rocket--;
System.out.println("1111卖票,剩余:"+Demo.rocket+"张票");
}
}
}
}
class MyThread2 extends Thread{
public void run(){
synchronized("11111"){
while(Demo.rocket>0){
Demo.rocket--;
System.out.println("22222222卖票,剩余 :"+Demo.rocket+"张票........");
}
}
}
}
package com.xiehou.th;
public class Demo {
public static int rocket = 20;
public static void main(String[] args) {
Demo demo=new Demo();
MyThread mt1 = new MyThread(demo);
Thread t1 = new Thread(mt1);
MyThread2 t2 = new MyThread2(demo);
t1.start();
t2.start();
}
}
class MyThread implements Runnable {
public Demo demo;
public MyThread(Demo demo){
this.demo=demo;
}
public void run() {
synchronized (demo) {
while (Demo.rocket > 0) {
if (Demo.rocket == 1) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
Demo.rocket--;
System.out.println("[1111]卖第:" + Demo.rocket + "张票");
}
}
}
}
class MyThread2 extends Thread {
public Demo demo;
public MyThread2(Demo demo){
this.de