Java 从线程捕获ActionEvents

Java 从线程捕获ActionEvents,java,multithreading,swing,actionlistener,actionevent,Java,Multithreading,Swing,Actionlistener,Actionevent,我需要做的是制作/消费节目。我需要做2个线程(第一个将继续发送4秒中断的AcionEvent,第二个将做10秒中断的相同操作)。使用者线程需要是JFrame和JTextArea。我需要实现ActionPerformedListner来捕获制作人创建的事件。当我赶上第一名时,我甚至需要清除JTextArea文本。当我要赶上第二个事件,我需要填写一些文字。我不知道如何将ActionEvent发送到消费者线程。有什么帮助吗?没有那么难的伙伴,首先两个线程(生产者/消费者)都应该处于等待状态,为此需要两

我需要做的是制作/消费节目。我需要做2个线程(第一个将继续发送4秒中断的AcionEvent,第二个将做10秒中断的相同操作)。使用者线程需要是JFrame和JTextArea。我需要实现ActionPerformedListner来捕获制作人创建的事件。当我赶上第一名时,我甚至需要清除JTextArea文本。当我要赶上第二个事件,我需要填写一些文字。我不知道如何将ActionEvent发送到消费者线程。有什么帮助吗?

没有那么难的伙伴,首先两个线程(生产者/消费者)都应该处于
等待
状态,为此需要两个对象,一个用于发送第一个线程的信号,另一个用于第二个线程。
而且,这里可能不需要为producer提供两个线程。
类似这样的

final Object producer=new Object();//used for signaling the thread about the incoming event
final Object signalConsumer;//used for signaling consumer thread.
void run(){
while(true){//until end of the system life cycle, use a flag, or set the thread daemon
 try{
  synchronized(producer){producer.wait();}
  Thread.sleep(4000);//sleep for 4 s
   synchronized(consumer){signalConsumer.notify();}//send the first signal to consumer to clear the textbox
  Thread.sleep(10000);//sleep for 10 seconds
   synchronized(consumer){signalConsumer.notify();}//send the second signal to consumer for filling the textbox
 }catch(Exception ex){}
}
}
以及消费者线程。 最终对象signalConsumer=新对象()//将引用传递给生产者线程

void run(){
while(true){
 try{
  synchronized(signalConsumer){signalConsumer.wait();}//waiting for the first signal
  //clearing textbox, etc...
  synchronized(signalConsumer){signalConsumer.wait();}//waiting for the second signal
  //filling the textbox, etc...
 }catch(Exception ex){}
}
}

在您捕获事件的UI线程中,只需通知生产者线程。

您介意发布您描述的代码吗?仍然不明白:/i是否需要执行2个类,生产者1个类,消费者1个类?ui线程看起来应该是什么样子(在哪里通知制作人)是的,两个类,一个是pro,另一个是con。UI线程通过
actionPerformed()
方法从系统获取事件,该方法通知生产者。
void run(){
while(true){
 try{
  synchronized(signalConsumer){signalConsumer.wait();}//waiting for the first signal
  //clearing textbox, etc...
  synchronized(signalConsumer){signalConsumer.wait();}//waiting for the second signal
  //filling the textbox, etc...
 }catch(Exception ex){}
}
}