简体   繁体   English

如何使线程从 Java 中的另一个线程休眠

[英]How to make a thread sleep from another thread in Java

I have two threads of execution(say Thread1 and Thread2).我有两个执行线程(比如 Thread1 和 Thread2)。 Thread2 is listening for a particular event and as the event occurs it wants to stop execution of Thread1 and trigger an action. Thread2 正在监听一个特定的事件,当事件发生时它想要停止 Thread1 的执行并触发一个动作。 After it is done with the action it needs to continue the execution of Thread1 from where it stopped.完成操作后,它需要从停止的位置继续执行 Thread1。

What kind of approach should I take to do this in Java?在 Java 中我应该采取什么样的方法来做到这一点?

The clean way to do it, IMHO, is to make Thread1 regularly poll some state variable to see if it has been asked to pause.恕我直言,干净的方法是让 Thread1 定期轮询一些 state 变量,看看它是否被要求暂停。 If it's been asked to pause, then it should suspend its execution, waiting for some lock to be released.如果它被要求暂停,那么它应该暂停执行,等待某个锁被释放。

Thread2 should ask Thread1 to pause by changing the value of the shared state variable, then potentialy wait for Thread1 to accept the pause request, then execute its action and release the lock on which Thread1 is waiting. Thread2 应该通过更改共享 state 变量的值来请求 Thread1 暂停,然后可能等待 Thread1 接受暂停请求,然后执行其操作并释放 Thread1 正在等待的锁。

In short, the two threads must collaborate.简而言之,两个线程必须协作。 There is no way that I'm aware of to pause a thread cleanly without its collaboration.我不知道没有它的协作可以干净地暂停一个线程。

You (of course) need a reference to the thread you wish to stop.您(当然)需要引用您希望停止的线程。 You can pause it by calling the 'suspend' method:http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#suspend () on the thread.您可以通过在线程上调用“暂停”方法来暂停它:http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#suspend ()。 Similarly you can call 'resume' to let the thread run again.同样,您可以调用“恢复”让线程再次运行。

However, be aware that this is extremely prone to dead-lock problems since you have no idea where the thread is stopped.但是,请注意,这极容易出现死锁问题,因为您不知道线程在哪里停止。

It seems like you need some sort of synchronization between threads.似乎您需要在线程之间进行某种同步。 I suppose you want T1 not to go to sleep, but wait until T2 performs action.我想你不希望T1让 go 进入睡眠状态,而是等到T2执行操作。 In such scenario, you could use any synchronization primitives Java provides.在这种情况下,您可以使用 Java 提供的任何同步原语。 For example synchronized keyword:例如synchronized关键字:

class T1 implements Runnable {
  private final Object lock;

  public T1(Object lock) {
    this.lock = lock;
  }

  public function run() {
    while(!currentThread().isInterrupted()) {
      waitForEvent();
      synchronized (lock) {
        // here T2 sleeps and wait until we perform event processing
      }
    }
  }
}

class T2 implements Runnable {
  private final Object lock;

  public T1(Object lock) {
    this.lock = lock;
  }

  public function run() {
    while(!currentThread().isInterrupted()) {
      synchronized (lock) {
        // do some work and release lock
      }
    }
  }
}

Object lock = new Object();
new Thread(new T1(lock)).start();
new Thread(new T2(lock)).start();

And btw, methods Thread#stop() , Thread#suspend() and Thread#resume() are deprecated and it's not recommended to use them.顺便说一句,方法Thread#stop()Thread#suspend()Thread#resume()已被弃用,不建议使用它们。

You can keep a reference to the other thread in Thread2 and pause it.您可以保留对Thread2中另一个线程的引用并暂停它。 But the real question is why you need two thread if they have to wait for each other to run?但真正的问题是,如果它们必须相互等待运行,为什么还需要两个线程?

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

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