0

我有一个生成线程的主类,我们称它们为 MainClass 和 MyThread。

public class MainClass extends javax.swing.JFrame {
   int sharedVariable;
   MyThread threadInstance;
   public MainClass (){
      sharedVariable = 2;
      threadInstance = new MyThread(this);
      threadInstance.run();
   }

  public int getSharedVariable(){ return sharedVariable; }

  public static void main(String[] args){
    //begin main class
 }
} 


public class MyThread implements Runnable {

     MainClass class;
     public MyThread(MainClass main_class){
         this.main_class= main_class;
      }

     @Override
     public run(){

     while(this.main_class is still active){

      //grab status of sharedVariable and wait for x amount of time.
      }
     }
    } 

问题是我不知道如何实现 while 条件来检查 MainClass 实例是否还活着,如果是,它必须使用 this.main_class.getSharedVariable() 来获取 sharedVariable 的值,然后等待 x多少时间。MainClass 有 main 方法。

4

3 回答 3

3

我建议保留Thread实例,然后在方法退出threadInstance.interrupt()之前立即调用。main(...)

就像是:

 public static void main(String[] args){
    MainClass mainClass = new MainClass();
    try {
        ...
        // do main stuff here
        ...
    } finally {
        mainClass.threadInstance.interrupt();
    }
 }

然后在你的线程中你会做:

 while(!Thread.currentThread().isInterrupted()){
     ...
 }

您还想InterruptedException正确处理:

 try {
     Thread.sleep(1000);
 } catch (InterruptedException e) {
     // always a good pattern to re-interrupt the thread here
     Thread.currentThread().interrupt();
     // if we are interrupted quit
     return;
 }

顺便说一句,在构造期间将对象的实例泄漏到另一个线程是非常糟糕的形式:

 new MyThread(this);

See here: Why shouldn't I use Thread.start() in the constructor of my class?

Also, you aren't starting a thread when you call threadInstance.run();. You are just running it in the current thread. You should use threadInstance.start() but not inside of the constructor like that.

于 2012-10-26T19:32:45.970 回答
1

You can use CountDownLatch which is very convenient for such tasks as waiting other threads to finish some activity (you can change Thread.sleep(...) argument in main to, say, 12000L and see what happens):

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

class OtherThread extends Thread {
    private final CountDownLatch sharedLatch;

    OtherThread(CountDownLatch sharedLatch) {
        this.sharedLatch = sharedLatch;
    }

    @Override
    public void run() {
        boolean wokenByMain = false;
        try {
            wokenByMain = sharedLatch.await(10000L, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return; // or not return, whatever makes more sense in your case
        }
        System.out.println("heh: " + wokenByMain);
    }
}
class SOSample {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        OtherThread otherThread = new OtherThread(latch);
        otherThread.start();
        System.out.println("Scheduled other thread to be started");
        Thread.sleep(1000L);
        System.out.println("going to release other thread");
        latch.countDown();
    }

}
于 2012-10-26T19:46:49.600 回答
-1
public class MainClass extends JFrame implements Runnable {
  public static void main(String [] args) {
    final Thread t=new Thread(new MainClass() {
            public void run(){
              //something
            });
    Thread t2=new Thread(new MyThread() {
            public void run() {
              while(t.isAlive) {
                //something
              }
            }
    });
  }
}
于 2015-09-15T20:49:54.263 回答