是否可以从Java中的辅助线程调用主线程? [英] Is it possible to call the main thread from a worker thread in Java?

查看:123
本文介绍了是否可以从Java中的辅助线程调用主线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为action()的方法,该方法可部署三个线程.每个部署的线程或工作线程都基于boolean类型为true的单个实例变量进入while循环,例如boolean doWork = true,每个线程将具有while(doWork){}循环.

I have a method called action() that deploys three threads. Each deployed thread or worker thread falls into a while loop based on a single instance variable of type boolean being true, for example boolean doWork = true, each thread will have a while(doWork){} loop.

当线程完成时,作业会将doWork设置为false,以阻止所有线程循环.然后,我希望能够以某种方式让主线程重新调用action()方法,以重新部署线程以完成另一项工作. (如果我使用工作线程之一来调用action()方法,可以吗?)一旦工作线程调用action()方法并以某种方式死亡,它将终止吗?

When a thread finishes the job will set the doWork to false stopping all the threads from looping. Then I would like to be able to somehow let the main thread recall the action() method to redeploy the threads to do another job. (If I use one of the worker threads to call the action() method is it OK ?) will the worker thread terminate once it calls the action() method and somehow die ?

为简单起见,我将示例限制为两个线程

I limited the example to two threads for simplicity

谢谢

class TestThreads{
    boolean doWork = true;

    void action(){
         ThreadOne t1 = new ThreadOne();
         ThreadTwo t2 = new ThreadTwo();
    }

    //innerclasses

    class ThreadOne implements Runnable{
          Thread trd1;
          public ThreadOne(){//constructor
              if(trd1 == null){
                   trd1 = new Thread(this);
                   trd1.start();
              }
          }
          @Override
          public void run(){
              while(doWork){
                   //random condition
                   //would set doWork = false;
                   //stop all other threads
              }
              action();//is the method in the main class
          }
    }
    class ThreadTwo implements Runnable{
          Thread trd2;
          public ThreadTwo(){//constroctor
              if(trd2 == null){
                   trd2 = new Thread(this);
                   trd2.start();
              }
          }
          @Override
          public void run(){
              while(doWork){
                   //random condition
                   //would set doWork = false;
                   //stop all other threads
              }
              action();//is the method in the main class
          }
    }

}

推荐答案

此实现如何:

声明一个类成员doWork,一个当前活动线程的计数器和一个同步对象:

Declare a class member doWork, a counter for currently active threads and a synchronization object:

private volatile boolean doWork = true;
private AtomicInteger activeThreads;
private Object locker = new Object();

主要:

while(true) {
    // call action to start N threads
    activeThreads = new AtomicInteger(N);
    action(N);
    // barrier to wait for threads to finish
    synchronized(locker) {
       while(activeThreads.get() > 0) {
           locker.wait();
       }
    }
}

在线程主体中:

public void run() {
   while(doWork) {
      ...
      // if task finished set doWork to false
   }

   // signal main thread that I've finished
   synchronized(locker) {
      activeThreads.getAndDecrement();
      locker.notify();
   }
}

这篇关于是否可以从Java中的辅助线程调用主线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆