3

I want to make a background task in one thread and send information from that thread to another thread which runs the main program and update the information each period of time.

Thread1:

public void doTask()
{
    // ...
    someData = ...;
}

Thread2:

SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        new MainFrame(someData);
    }
});

How could I achieve this in java?

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417

4 Answers4

3

Use the Java Concurrency API. Usage of BlockingQueues, Future<> will help you.

Note: This is a classic producer-consumer problem. This can be achieved having a shared queue to hold data where one thread will push data into the queue and the othe will consume the data. Proper synchronisation is needed to achieve a correct solution. BlockingQueue implementations like LinkedBlockingQueue will be helpful.

Update: Part of you problem [the consumer] can be derived from this answer of mine. But this solution uses the simpler wait-notify approach.

Community
  • 1
  • 1
Swaranga Sarma
  • 13,055
  • 19
  • 60
  • 93
2

I using Executor/ExecutorService, output from these methods starting java.swing.Action (create a new EDT if doesn't exists),

or you can test that For EDT

if (SwingUtilities.isEventDispatchThread()) {...

both ways for your Wratever sould be always in EDT, I vote for wraps output to the GUI into Action

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks man. I'm really not fimiliar with using threads or even Java Concurrency API, could you please provide a very simple example that achieves what I'm looking for? :) – Eng.Fouad Aug 25 '11 at 07:14
  • yes here is my http://stackoverflow.com/questions/7036509/how-do-i-simulate-a-buffered-peripheral-device-with-swingworker/7049095#comment-8432327, made just by my enjoy, look here too http://stackoverflow.com/questions/7053865/cant-get-arrayindexoutofboundsexception-from-future-and-swingworker-if-thread , with intention to starting MultiThreading wrapped into SwingWorker, should be required for all of methods implements >Future, not only SwingWorker, in all cases don't forget to implements PropertyChangeListener for listening statuses from Future – mKorbel Aug 25 '11 at 07:36
  • This example: http://stackoverflow.com/questions/7053865/cant-get-arrayindexoutofboundsexception-from-future-and-swingworker-if-thread was very helpful. Thanks :) – Eng.Fouad Aug 25 '11 at 08:14
  • glad to helps, that was my nightMare, but really you have to redirect your thanks to @trashgod – mKorbel Aug 25 '11 at 08:23
  • +1 for `SwingWorker`, a `RunnableFuture` that is convenient if the destination thread is the EDT. There's a related example [here](http://stackoverflow.com/questions/4637215/can-a-progress-bar-be-used-in-a-class-outside-main/4637725#4637725). – trashgod Aug 25 '11 at 09:50
1

Java doesn't have a direct mechanism for a specific thread to send a message to another SPECIFIC thread.

But there are indirect ways of achieving this though. For example, you can define a message queue object on one thread and then pass a reference to that queue to the second thread. The second thread can insert message objects into this queue; and the first thread can keep polling the queue to get itself alerted to new messages.

Also answered in this thread

Communicating between two threads

The above link has some sample (pseudo) code.

Community
  • 1
  • 1
Basanth Roy
  • 6,272
  • 5
  • 25
  • 25
0

This is maybe an overengineered aproach for this case, but nonetheless you could use EventBus - each Thread could publish and be subsrcibed to events.

An example is here.

davorp
  • 4,156
  • 3
  • 26
  • 34