java吧 关注:1,296,854贴子:12,833,906
  • 3回复贴,共1
synchronized函数为何没有实现原本功能


IP属地:广东来自Android客户端1楼2015-11-11 00:45回复
    class Xc8 implements Runnable {
    public static int chepiao = 100;
    String ax = new String("1");
    public void run() {
    while (true) {
    // synchronized既可以修饰代码块,有可以修饰函数,修饰函数不用参数
    synchronized (ax) {
    if (chepiao > 0) {
    System.out.println("第" + Thread.currentThread().getName()
    + "个车站正在卖第" + (101 - chepiao) + "张车票");
    --chepiao;
    } else {
    break;
    }
    }
    }
    }
    }
    public class Lab7 {
    public static void main(String[] args) {
    Xc8 xx = new Xc8();
    Thread aa = new Thread(xx);
    aa.start();
    Xc8 yy = new Xc8();
    Thread bb = new Thread(yy);
    bb.start();
    }
    }


    IP属地:广东5楼2015-11-11 10:43
    回复
      2026-02-04 11:53:23
      广告
      不感兴趣
      开通SVIP免广告


      IP属地:广东6楼2015-11-11 10:45
      回复
        不知道哪里出问题了 ,求助


        IP属地:广东7楼2015-11-11 10:45
        回复