哥们测试过了没?我以前用毕老师讲的那个卖票程序试了下,完全没问题,可以直接new对象啊
- class Ticket implements Runnable
- {
- private int num = 2000;
- //Object obj = new Object();
- public void run()
- {
- while (true)
- {
- synchronized(new Object())
- {
- show();
- }
- }
- }
- public synchronized void show()
- {
- if (num>0)
- {
- try
- {
- Thread.sleep(10);
- }
- catch (Exception e){}
- System.out.println(Thread.currentThread().getName()+"...sale :"+num--);
- }
- }
- }
复制代码
主函数我就不写上了,就是开启线程 |