Reputation:
what i need to do is a Producer/Consumer program. I need to do a 2 producers threads (1st will keeps sending a AcionEvent with 4 s breaks, 2nd will do the same with 10 s breaks). Consumer thread need to be JFrame with a JTextArea. I need to implement ActionPerformedListner to catch events created by producers. When i will catch 1st even i need to clear JTextArea text. when i will catch second event i need to fill it with some text. I dont know how to send ActionEvent to the consumer thread. Any help?
Upvotes: 0
Views: 73
Reputation:
No so hard buddy, first both threads(producer/consumer) should be in waiting
state, and for doing this you need two objects, one for signaling first thread, second one for the second thread.
and nothe that here two threads for producer may is not required.
something like this.
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){}
}
}
And the Consumer thread. final Object signalConsumer=new Object();//pass the reference to the producer thread.
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){}
}
}
and in the UI thread which you catch the event just notify the producer thread.
Upvotes: 1