多线程状态依赖问题 [英] multithread state-dependent issue

查看:107
本文介绍了多线程状态依赖问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了以下代码片段:

I was given following code snippet:

public class ThreadTest{

  private static class Thread01 extends Thread{
    private Thread02 _th2; 
    public int foo = 0;

    public void setThrd02(Thread02 thrd2){
       _th2 = thrd2;
    }

    public void run(){
      try{
        for(int i=0;i<10;i++) foo += i;

        synchronized(this){this.notify();};

        synchronized(_th2){_th2.wait();};

        System.out.print(" Foo: " + _th2.foo);
      }catch(InterruptedException ie){ ie.printStackTrace();}    
    }
  }

  private static class Thread02 extends Thread{

    private final Thread01 _th1;
    public int foo = 0;

    public Thread02(Thread01 th1){
      _th1 = th1;
    }

    public void Run(){
       try{
         synchronized(_th1){_th1.wait();}
         foo = _th1.foo;

         for(int i=0;i<10;i++) foo += i;
         synchronized(this){this.notify();};
           }
       catch(InterruptedException ie){ie.printStackTrace();}

    }

  }

  public static void main(){
    Thread01 th1 = new Thread01();
    Thread02 th2 = new Thread02(th1);

    th1.setThrd02(th2);

    th1.start(); th2.start();
    th1.join(); th2.join();
  } 
}

我认为代码的假设和相应目的是比如
th2先运行,它会通过调用_th1.wait()更改为等待状态;
然后,th1计算foo并唤醒th2,th1进入等待状态;
Th2从thread1读取foo并更新为110,然后唤醒th1和th2退出。
然后th1退出。

I think the assumption and corresponding purpose of the code is like th2 run first, it is changed to waiting status by calling _th1.wait(); Then, th1 calculates foo and wake up th2, th1 goes into waiting status; Th2 reads foo from thread1 and updated to 110, then wakes up th1 and th2 exit. Then th1 exit.

线程可能非常危险,因为线程1很可能先运行而线程2将永远等待。

The threads could be very risk because it is very possible that thread one runs first and thread 2 will wait forever.

我不确定代码的任何其他潜在问题。

I am not sure any other potential problems of the code.

解决问题的一种可能方法是,例如在thread1

One possible way that can fix the problem is, for example in the thread1

公共类ThreadTest {

public class ThreadTest{

private static boolean updated = false;
private static boolean finished = false;

private static boolean updated = false; private static boolean finished = false;

private static Thread01 extends Thread {

private static Thread01 extends Thread{

public void Run (){
//做计算
而(完成){
wait();
}
//输出结果
}
}

public void Run(){ // do calcuation while(finished){ wait(); } // output result } }

私有静态Thread02扩展线程{
public void run(){

private static Thread02 extends Thread{ public void run(){

while(false){
wait();
}

while(false){ wait(); }

foo = th1.foo;
//做计算
//类似机制通知线程1
}
}

foo = th1.foo; // do calculation // similar mechanism to notify thread 1 } }

推荐答案

无法保证在您的主题中订购。在Thread02执行 synchronized(_th1)之前,Thread01足以超过
synchronized(this){this.notify();}; {_th1.wait();}
让两个线程无限期地等待。

There is no guarantee of ordering in your threads. It's sufficient for Thread01 to go past synchronized(this){this.notify();}; before Thread02 does synchronized(_th1){_th1.wait();} to have both threads waiting indefinitely.

注意:你打电话等待通知 on _th1和_th2是无关紧要的。此处的线程将被视为任何其他对象。

Note: The fact that you are calling wait and notify on _th1 and _th2 is irrelevant. Threads here will be treated as any other object.

这篇关于多线程状态依赖问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆