简体   繁体   English

如何处理java线程

[英]How to deal with java threads

I have a class called communicator. 我有一个叫做沟通者的课。 This class is a listener of a thread receiving events from another program. 此类是从另一个程序接收事件的线程的侦听器。 Also this class has a method call refresh that sends and action to the program a waits for the response that comes through the listener. 此类还有一个方法调用刷新,它向程序发送和操作,等待通过侦听器发出的响应。

Both methods are in the same class but called by diferent threads. 两种方法属于同一类,但由不同的线程调用。

public void processRefreshEvent(ManagerEvent event){
    //processing event
    //...
    //I'm done
    notify();
}


public synchronized void refresh() throws Exception {
    isRefreshing = true;    
    try {
                    manager.send(new refresh());
    } catch (ManagerException e) {
        isRefreshing = false;
    }

    try {
            wait(5000);
    } catch (InterruptedException e) {
    } finally{
        isRefreshing = false;
    }
}

when executing the code above I get the follow exception: 当执行上面的代码时,我得到以下异常:

 java.lang.IllegalMonitorStateException: current thread not owner at java.lang.Object.wait(Native Method) at Communicator.refresh(Communicator.java:203) ... 

What is the proper way to "wait" for another thread to finish. “等待”另一个线程完成的正确方法是什么。 Thanks. 谢谢。

You need to synchronize your threads on a monitor. 您需要在监视器上同步线程。 For instance (using the current object as the monitor): 例如(使用当前对象作为监视器):

public void processRefreshEvent(ManagerEvent event){
        //processing event
        //...
        //I'm done
    synchronized(this) {
        notify(); // you are basically notifying any thread who has blocked
                  // on this monitor - in our case, the instance of this object
    }
}


public synchronized void refresh() throws Exception {
        isRefreshing = true;    
        try {
                    manager.send(new refresh());
        } catch (ManagerException e) {
                isRefreshing = false;
        }

        try {
          synchronized(this) {
                wait(5000); // wait will give up the monitor
          }
        } catch (InterruptedException e) {
        } finally{
                isRefreshing = false;
        }
}

The methods wait() and notify() may only be called from a thread that is currently synchronized on their instance. 方法wait()notify()只能从当前在其实例上同步的线程调用。

Declare "processRefreshEvent" synchronized , or better yet, just the block of code that modifies the state that is used by the refresh method, together with the notify() call. 声明“processRefreshEvent” synchronized ,或者更好的是,只是修改refresh方法使用的状态的代码块,以及notify()调用。

public void processRefreshEvent(ManagerEvent event){
  // processing event
  synchronized (this) {
    // modify shared state with results of processing.
    notify();
  }
}

You say you want to wait until another thread has finished? 你说你想等到另一个线程完成? Then just call join() on the Thread object you want to wait for. 然后只需要在要等待的Thread对象上调用join()。

From Object.wait() 's JavaDocs: "The current thread must own this object's monitor." 来自Object.wait()的JavaDocs:“当前线程必须拥有此对象的监视器。” So you need to synchronize on the object that you're invoking wait on. 所以你需要同步你正在调用的对象等待。

Alternatively you could use BlockingQueue , which implements Collection and Queue . 或者,您可以使用BlockingQueue ,它实现CollectionQueue BlockingQueue does all the work of wait and notify. BlockingQueue完成等待和通知的所有工作。 Your thread can just call take() , which will block until data is available. 你的线程只能调用take() ,它将阻塞直到数据可用。 You add data to the queue using the various insert methods (add, put, etc). 您可以使用各种插入方法(add,put等)将数据添加到队列中。 BTW the insert methods call notify while take() calls wait . BTW插入方法在take()调用wait调用notify

Please read the JavaDoc on java.lang.Object.wait() and notify() . 请阅读java.lang.Object.wait()notify()上的JavaDoc。

You should synchronize the wait() with the proper monitor, in this case: 您应该将wait()与适当的监视器同步,在这种情况下:

try{
    synchronized(this){
          wait(5000);
    }
}
catch (InterruptedException e) {
} finally{
            isRefreshing = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM