- /*
- 需求:死锁程序
- */
- class SiSuo
- {
- public static void main(String[] args)
- {
- Suo s=new Suo();
- Thread t1=new Thread(s);
- Thread t2=new Thread(s);
- t1.start();
- t2.start();
- }
- }
- class Suo implements Runnable
- {
- private int num=1000;
- Object obj=new Object();
- public void run()
- {
- while(true)
- synchronized(obj)
- {
- show();
- }
- }
- public synchronized void show()
- {
- synchronized(obj)
- {
- if(num>0)
- System.out.println(Thread.currentThread().getName()+"show......"+num--);
- }
- }
- }
复制代码 怎么我运行了好几次都没有锁住啊??? |