当主线程仍在运行时,线程如何通知主线程 [英] How does a Thread notify the main thread, while the main thread is still running

查看:185
本文介绍了当主线程仍在运行时,线程如何通知主线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的初学者.我想通过产生一个线程来实现一个计时器.我要运行的方式是即使在生成线程之后,主线程也应继续工作.但是,一旦线程完成Task,它应该通知主线程该任务已完成.我查看了Thread.join()和Thread.sleep().对于这两种方法,主线程都等待线程完成任务.但是我希望主线程继续运行.

I am a beginner for Java. I want to implement a Timer by spawning a Thread. The way I want to run is that the main thread should continue working even after spawning the thread. However once the thread completes the Task, it should notify the main thread that the task is done.I looked at Thread.join() and Thread.sleep(). For both these methods, the main thread waits for the thread to complete the task. However I want the main thread to keep on running.

我在下面编写了代码,其中主线程调用一个线程,并且该线程执行任务10 s.但是,我无法通知主线程有关任务完成的信息. 请帮我.

I have written a code below, where the main thread calls a thread, and the thread executes the task for 10 s. However I am not able to notify the main thread about the task completion. Kindly help me out.

public class MainThreadtest {
   public static void main(String[] args) {
      dispthread2 dt2 = new dispthread2();
      dt2.start();
      System.out.println("Thread dt2 has started");
   }
}

二等班

public class dispthread2 extends Thread {
   public void run() {
      long endTimemillis = System.currentTimeMillis() + 10000;
      while (System.currentTimeMillis() < endTimemillis) {
      }
      System.out.println("The task is done");
   }
}

推荐答案

一种解决此问题的方法,无需使用Thread#join,因此允许两个线程独立运行:

One way to solve this that does not require use of Thread#join and thus allows the two threads to run independently:

  1. 使用观察者模式侦听器接口,例如可以通过Java Beans PropertyChangeSupport和PropertyChangeListener获得的接口.
  2. 不扩展Thread,而是实现Runnable.


例如:


For example:

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.swing.event.SwingPropertyChangeSupport;

public class MainThreadTest {
   public static void main(String[] args) {
      DispThread2 dt2 = new DispThread2();
      dt2.addPropertyChangeListener(DispThread2.STATE, new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent evt) {
            System.out.println("DT2 Thread state (from Main Thread): " + evt.getNewValue());
            if (evt.getNewValue() == DispThreadState.DONE) {
               System.out.println("Thread dt2 is now done (from Main Thread)");
            }
         }
      });
      new Thread(dt2, "DT2").start();
      System.out.println("Thread dt2 has started");
   }
}

class DispThread2 implements Runnable {
   public static final String STATE = "state";
   private volatile DispThreadState state = DispThreadState.PENDING;
   private PropertyChangeSupport support = new SwingPropertyChangeSupport(this);

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      support.addPropertyChangeListener(listener);
   }

   public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
      support.addPropertyChangeListener(propertyName, listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      support.removePropertyChangeListener(listener);
   }

   public void run() {
      setState(DispThreadState.RUNNING);
      long endTimemillis = System.currentTimeMillis() + 10000;
      while (System.currentTimeMillis() < endTimemillis) {
      }
      System.out.println("The task is done");
      setState(DispThreadState.DONE);
   }

   public DispThreadState getState() {
      return state;
   }

   public void setState(DispThreadState state) {
      DispThreadState oldValue = this.state;
      DispThreadState newValue = state;
      this.state = state;
      support.firePropertyChange(STATE, oldValue, newValue);
   }
}

enum DispThreadState {
   PENDING, RUNNING, DONE
}

这篇关于当主线程仍在运行时,线程如何通知主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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