QQ1252776430 2015-11-24 05:58 采纳率: 22%
浏览 2073

java线程synchronized(this){}为何对其它synchronized方法产生影响。

直接上代码:

public class NewThread implements Runnable{

String name;
Thread t;
static boolean suspendFlag;

NewThread(String threadname)
{
    name=threadname;
    t=new Thread(this,name);
    System.out.println("New thread:"+t);
    suspendFlag=false;
    t.start();
}

public void run()
{

    try
    {

        for(int i=15;i>0;i--)
    {

            System.out.println(name+":"+i);

    Thread.sleep(1000);
    synchronized(this){while(suspendFlag){this.wait();}}


    }   
    }catch(InterruptedException e){}

    System.out.println(name+" exiting.");

    }

synchronized  void mysuspend(){suspendFlag=true;}
synchronized  void myresume(){suspendFlag=false;notify();}

}

public class SuspendResume {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

NewThread ob1=new NewThread("One");

try{
Thread.sleep(2000);

ob1.mysuspend();
System.out.println("Suspending thread One");
Thread.sleep(2000);


ob1.myresume();
System.out.println("Resuming thread One");

Thread.sleep(2000);

}catch(InterruptedException e){}

try{System.out.println("Waiting for threads to finiish.");
ob1.t.join();

}catch(InterruptedException e){}

System.out.println("Main thread exiting.");

}

}
运行结果:
New thread:Thread[One,5,main]
One:15
One:14
One:13
Suspending thread One
One:12
Resuming thread One
One:11
One:10
Waiting for threads to finiish.
One:9
One:8
One:7
One:6
One:5
One:4
One:3
One:2
One:1
One exiting.
Main thread exiting.
如预期一样,ob1.mysuspend();ob1.myresume();的调用在ob1线程运行中是正常时间控制挂起和重启。
但将run()方法中synchronized(this){}模块稍微修改:
public void run()
{

    try
    {
        synchronized(this){
        for(int i=15;i>0;i--)
    {

            System.out.println(name+":"+i);

    Thread.sleep(1000);
    while(suspendFlag){this.wait();}


    }
        }
    }catch(InterruptedException e){}

    System.out.println(name+" exiting.");

    }


    其余代码不变

结果是:
New thread:Thread[One,5,main]
One:15
One:14
One:13
One:12
One:11
One:10
One:9
One:8
One:7
One:6
One:5
One:4
One:3
One:2
One:1
Suspending thread One
One exiting.
Resuming thread One
Waiting for threads to finiish.
Main thread exiting.

结果是主线程对ob1.mysuspend();ob1.myresume();的调用没有按照预期的时间进行,而是卡在ob1线程运行结束后才开始调用,就好像ob1得运行将ob1.mysuspend();ob1.myresume();锁住一样,sychonized模块将循环体包围住,mysuspend()就像被锁住不能动了似的,为什么会有两次的运行结果差异?谢谢。

  • 写回答

4条回答 默认 最新

  • QQ1252776430 2015-11-24 06:10
    关注

    第二种情况的结果的末尾:
    One:2
    One:1 //线程ob1在synchronized模块中的输出内容。
    Suspending thread One //主线程成功调用了ob1.mysuspend()的输出。
    One exiting. //线程ob1在synchronized模块外输出内容。

    主线程非常巧合的再线程执行完最后一个synchronized模块输出后开始启动。主线程再”等待”ob1中synchroized()模块的结束,双方是互相独立的synchronized锁代码,
    怎么会互相等待的呢。

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料