本帖最后由 那些人之一 于 2013-12-8 16:10 编辑
是死锁的代码,为什么看老师视频,同一样的代码,老师至少每次可以输出点东西,而我每次都没有,我代码有问题?详情看下面的代码和图
- // 死锁实例
- class Piao implements Runnable //extends Thread
- {
- private int num=100;
- Object obj=new Object();
- boolean flag=true;
- public void run()
- {
- if(flag)
- while (true)
- {
- synchronized(this)
- {
- show();
- }
- }
- else
- while (true)
- this.show();
- }
- public synchronized void show()
- {
- synchronized(obj)
- {
- if(num>0)
- {
- try
- {
- Thread.sleep(10);
- }
- catch (InterruptedException e)
- {
- System.out.println(Thread.currentThread().getName()
- +"----票数:"+num--);
- }
- }
- }
- }
- }
- class Deadlock
- {
- public static void main(String[] args)
- {
- Piao d =new Piao();
- Thread t1=new Thread(d);
- Thread t2=new Thread(d);
- t1.start();
- try
- {
- Thread.sleep(10);
- }
- catch (InterruptedException e)
- {
- }
- d.flag=false;
- t2.start();
- }
- }
复制代码
- <P><IMG border=0 alt="" src="http://bbs.itheima.com/forum.php?mod=image&aid=31802&size=300x300&key=ef51e7599b942e7d&nocache=yes&type=fixnone" aid="attachimg_31802"></P>
复制代码
|
|