2012-10-26 116 views
0

我有一个产生线程的主类,让我们称它们为MainClass和MyThread。线程依赖关系Java

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. 
     } 
    } 
    } 

的问题是我不知道如何实现它检查是否MainClass实例仍然活着,而条件,如果是,它必须使用this.main_class.getSharedVariable()来获取值共享变量,然后等待x时间。 MainClass有主要方法。

+1

'.run()' - >'.start()'。此外,“*还活跃*”是什么意思? –

+0

“活着”或“活跃”是什么意思? –

+0

你可以通过getState()方法获取线程状态[here](http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.State.html) – PbxMan

回答

3

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

喜欢的东西:

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); 

在这里看到:Why shouldn't I use Thread.start() in the constructor of my class?

而且,当你调用threadInstance.run();你是不是启动一个线程。你只是在当前线程中运行它。你应该使用threadInstance.start()而不是像这样的构造函数。

1

您可以使用CountDownLatch这是此类任务非常方便,因为等待其他线程来完成一些活动(您可以在主,比方说,12000L改变Thread.sleep(...)参数,看看会发生什么):

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(); 
    } 

} 
-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 
       } 
      } 
    }); 
    } 
} 
+2

我已经格式化了你的代码给你,但这个答案也会从一些解释中获益。 – durron597

+1

考虑改进你的答案。 _ [仅限代码的答案可能属于'非常低质量'...并且是删除的候选人......我们一直都在吹捧我们不是代码工厂。我们是教别人钓鱼的人。仅有代码的答案只能为一天的人提供食物](http://meta.stackexchange.com/questions/148272/is-there-any-benefit-to-allowing-code-only-answers-while-blocking-code - 只-疑问句)_ – MickyD