2010-11-16 72 views
0

警报父母我在J​​ava中的以下设置,Java中,对对象的变化

public class Main { 

    // starts sub class 
    SubClass sub = new SubClass(); 
    sub.start(); 

    // sub will keep running and call method alert() on a specif change 
    // method alert is void but updates a public variable named status on sub 
    while(1==1) { 

     // I should ideally be able to catch/read sub status result here 
     // how can I do it? 
    } 
} 

我是新来的Java所以这可能是无效的,我的做法可能是错误的。既然如此,请指向正确的方向。

谢谢。

+0

你的问题是什么? – 2010-11-16 23:15:07

回答

0

,你应该把你的代码放到一个主要方法:)

2

我相信SubClass.start()启动,以便家长在与孩子并行运行启动一个新线程。然后主类可以在sub对象上执行Object.wait(),并且​​线程可以在sub对象上执行Object.notify()

+0

我认为世界上只有两个人能够正确使用Object.notify(),而我们其他人则打算使用Object.notifyAll()。 – 2010-11-17 03:59:15

0

如果你的子类是没有一个Runnable,

public class Main 
{ 
    public static void main(String args[]) 
    { 
     Thread myThread = new Thread(new Runnable() 
     { 
      public void run() 
      { 
       //Instantiate your SubClass here and do stuff. You can pass yourself as a parameter if you plan to do callbacks. 
      } 
     }); 
     myThread.setDaemon(true); 
     myThread.start(); 
    } 
} 

或者,如果你已经实现在子类中的Runnable接口则只需使用的线程机制(wait()的,通知(),等等等等)。