线程方法没有运行?

时间:2013-12-21 11:54:19

标签: java multithreading

当我编译并运行StartThreads类时,我得到一个带有false的整数1到1000000的列表,最后它打印为true; 现在我想要找出的是为什么类threadone在类MyVariables = true时应该打印一次实例变量时什么都不打印?     公共类MyVariables {         public boolean startApp = false;     }

public class ThreadOne implements Runnable {
Thread t;
MyVariables x;
public ThreadOne(MyVariables x) {
    t = new Thread(this, "Thread One");
    this.x = x;
}   
@Override
public void run() {
    while(this.x.startApp != false) {
        System.out.println("Starting");
    }
}
public void start() {
    t.start();
}

}  

public class ThreadTwo implements Runnable {
Thread t;
MyVariables x;
public ThreadTwo(MyVariables x) {
    t = new Thread(this, "Thread One");
    this.x = x;
}   
@Override
public void run() {
    synchronized(this.x) {
        for(int i = 0; i <= 1000001; i++) {
            if(i == 1000001) {
                this.x.startApp = true;
                System.out.println(this.x.startApp);

            }
            else {
                System.out.println(this.x.startApp);
                System.out.println(i);
            }
        }
    }

}
public void start() {
    t.start();
}

}


public class StartThreads {

public static void main(String[] args) {
    MyVariables a = new MyVariables();
    ThreadOne x = new ThreadOne(a);
    ThreadTwo y = new ThreadTwo(a);
    x.start();
    y.start();
}

}

1 个答案:

答案 0 :(得分:3)

为什么ThreadOne应该像你说的那样表现?它是while循环永远不会运行,而是被跳过,因为条件不是真的。我认为你期望它等待某些东西改变为true,但它没有做任何类似的事情,相反,一旦它看到条件为假,就完成执行。

请注意,这是丑陋的代码:

while(this.x.startApp != false) {

为什么说某事不是假?这与 true 相同。 更好

while (x.startApp) {

现在关于你的实际问题,也许你应该做你的while循环:

while (!x.startApp) {
  Thread.sleep(1);  // surrounded with try/catch
}
System.out.println("Starting");

如,

class ThreadOne implements Runnable {
   Thread t;
   volatile MyVariables x;

   public ThreadOne(MyVariables x) {
      t = new Thread(this, "Thread One");
      this.x = x;
   }

   @Override
   public void run() {
      while (!x.startApp) {
         try {
            Thread.sleep(1);
         } catch (InterruptedException e) {
         }
      }
      System.out.println("Starting");
   }

   public void start() {
      t.start();
   }

}

class ThreadTwo implements Runnable {
   private static final int MAX_I = 10001;
   Thread t;
   volatile MyVariables x;

   public ThreadTwo(MyVariables x) {
      t = new Thread(this, "Thread One");
      this.x = x;
   }

   @Override
   public void run() {
      synchronized (this.x) {
         for (int i = 0; i <= MAX_I; i++) {
            if (i == MAX_I) {
               this.x.startApp = true;
               System.out.println(this.x.startApp);

            } else {
               System.out.println(this.x.startApp);
               System.out.println(i);
            }
         }
      }

   }

   public void start() {
      t.start();
   }

}

public class StartThreads {

   public static void main(String[] args) {
      MyVariables a = new MyVariables();
      ThreadOne x = new ThreadOne(a);
      ThreadTwo y = new ThreadTwo(a);
      x.start();
      y.start();
   }

}

class MyVariables {
   public volatile boolean startApp = false;
}

另外,我认为你的布尔字段至少应该是易变的。


使用PropertyChangeListener和观察者模式的另一种方法:

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class StartThreads2 {
   public static void main(String[] args) {
      final MyVariables2 myVars2 = new MyVariables2();
      final RunOne runOne = new RunOne();
      final RunTwo runTwo = new RunTwo(myVars2);

      myVars2.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent pcEvt) {
            if (MyVariables2.START_APP.equals(pcEvt.getPropertyName())) {
               if (pcEvt.getNewValue().equals(Boolean.TRUE)) {
                  new Thread(runOne).start();
               }
            }
         }
      });

      new Thread(runTwo).start();
   }
}

class MyVariables2 {
   public static final String START_APP = "start app";
   private volatile boolean startApp = false;
   private PropertyChangeSupport pcSupport = new PropertyChangeSupport(this);

   public boolean isStartApp() {
      return startApp;
   }

   public void setStartApp(boolean startApp) {
      boolean oldValue = this.startApp;
      boolean newValue = startApp;
      this.startApp = startApp;
      pcSupport.firePropertyChange(START_APP, oldValue, newValue);
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.removePropertyChangeListener(listener);
   }
}

class RunOne implements Runnable {
   @Override
   public void run() {
      System.out.println("Starting RunOne");
   }
}

class RunTwo implements Runnable {
   private static final int MAX_I = 10001;
   private MyVariables2 myVars2;

   public RunTwo(MyVariables2 myVars2) {
      this.myVars2 = myVars2;
   }

   @Override
   public void run() {
      for (int i = 0; i <= MAX_I; i++) {
         System.out.println("startApp: " + myVars2.isStartApp());
         System.out.printf("i: %05d%n", i);
      }

      myVars2.setStartApp(true);      
      try {
         Thread.sleep(10);
      } catch (InterruptedException e) {}
      System.out.println("startApp: " + myVars2.isStartApp());
   }
}