Java多线程问题求助,synchronized(obj)貌似没用错吧

tech2022-08-23  116

/*电影院一共有100张票,写3个窗口卖这100张票,使用多线程来模拟卖票的过程*/ public class HomeWork1 { public static void main(String[] args) { ticket1 t = new ticket1(); FutureTask task = new FutureTask(t); new Thread(task, "窗口A").start(); new Thread(task, "窗口B").start(); new Thread(task, "窗口C").start(); } } class ticket1 implements Callable { private int a = 100; Object obj = new Object(); @Override public Object call() throws InterruptedException { System.out.println(Thread.currentThread().getName()); while (a > 90) { Thread.sleep(100);//休眠时间 synchronized (obj) { a--; System.out.println(Thread.currentThread().getName() + "还剩" + a); } } return null; } }

如上代码,将多个线程操作一个数据,但是每次执行完成,结果总是只有一个线程在运行。请各路大神帮忙看看==

最新回复(0)