java - 在 Java 中向线程发出信号

标签 java linux multithreading

<分区>

我有以下场景。我的 Java 应用程序(在 Linux 平台上)中有两个线程在运行,线程一创建就 hibernate 。我希望线程在设置环境变量时唤醒。

我最初想出让线程不断检查变量的想法,即像忙等待状态。但由于它消耗 cpu 周期,我知道它效率低下。所以我想到了如果设置了环境变量就唤醒一个线程的想法。

那么有没有办法用Java实现呢? 提前致谢。

最佳答案

我为此编写了一个 Doze 类。

它通过在内部使用 BlockingQueue 来完全避免使用 Thread.sleep。正如 main 方法所示,它使用起来很简单。您只需打瞌睡一段时间,如果有人调用Doze.wakeup() 方法,您就会被唤醒。

您需要安排您的 Doze 对象可用于更新属性的包。在更新时它应该调用它的 wakeup()

/**
 * Use one of these to doze for a certain time.
 *
 * The dozing is fully interruptable.
 *
 * Another thread can stop the caller's doze with either a wakeup call or an abort call.
 *
 * These can be interpreted in any way you like but it is intended that a Wakeup is
 * interpreted as a normal awakening and should probably be treated in exactly the
 * same way as an Alarm. An Abort should probably be interpreted as a suggestion
 * to abandon the proces.
 */
public class Doze {
  // Special alarm messages.
  public enum Alarm {
    // Standard timeout.
    Alarm,
    // Just wake from your doze.
    Wakeup,
    // Abort the whole Doze process.
    Abort;
  }
  // My queue to wait on.
  private final ArrayBlockingQueue<Alarm> doze = new ArrayBlockingQueue<>(1);
  // How long to wait by default.
  private final long wait;

  public Doze(long wait) {
    this.wait = wait;
  }

  public Doze() {
    this(0);
  }

  public Alarm doze() throws InterruptedException {
    // Wait that long.
    return doze(wait);
  }

  public Alarm doze(long wait) throws InterruptedException {
    // Wait that long.
    Alarm poll = doze.poll(wait, TimeUnit.MILLISECONDS);
    // If we got nothing then it must be a normal wakeup.
    return poll == null ? Alarm.Alarm : poll;
  }

  public void wakeup() {
    // Just post a Wakeup.
    doze.add(Alarm.Wakeup);
  }

  public void abort() {
    // Signal the system to abort.
    doze.add(Alarm.Abort);
  }

  // Demo of use.
  public static void main(String[] args) throws InterruptedException {
    // Doze for 1 second.
    final Doze d = new Doze(1 * 1000);

    // Start a dozing thread.
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          Alarm a = d.doze();
          // Wait forever until we are aborted.
          while (a != Alarm.Abort) {
            System.out.println("Doze returned " + a);
            a = d.doze();
          }
          System.out.println("Doze returned " + a);
        } catch (InterruptedException ex) {
          // Just exit on interrupt.
        }
      }
    }).start();

    // Wait for a few seconds.
    Thread.sleep(3000);

    // Wake it up.
    d.wakeup();

    // Wait for a few seconds.
    Thread.sleep(3000);

    // Abort it.
    d.abort();


  }
}

关于java - 在 Java 中向线程发出信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18788457/

相关文章:

multithreading - 如何处理阻塞调用期间需要释放锁的情况?

java - 检查 xhtml JSF 中的 session 访问

java - Applet 在 Tomcat 中不显示来自 URL 的图像

java - 无法在我的快速排序实现中调用排序方法

linux - linux 程序的帮助在哪里?

c++ - 终止使用 QueueUserWorkItem(win 32/nt5) 创建的线程池中长时间运行的线程

java - 如何在 GWT 中获取一年中的星期数

linux - 从 linux 交叉编译到 ARM-ELF (ARM926EJ-S/MT7108)

linux kallsyms R 符号不显示

java - 使用 Spring 在 weblogic 中的服务器启动时启动线程