Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to ensure threads run in a particular order.  RSS feed

 
Alberto Caraz
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application in which I have a set of objects, and I need to query them for a message in certain order, and then to transmit that message to all of the objects.

For example, imagine there are 20 'objects' playing a game. So my application is in charge of querying the object whose turn it is, obtain his response and then broadcasting the message to all of the 20 objects, including the one who just sent the message.

So I have a Thread with a loop like this:



As you can see, I'm using a separate thread for each listener.inform(message) method, because I don't want an observer to be able to slow down my cycle if it decides to have a very consuming inform(String) method.

The problem is: As it is, the object whose turn it is gets notified of the "getMessage()" before getting notified of the previous message. That is, the loop manages to continue (and call the new getMessage()) before the newly created thread has the opportunity to call inform(String).

I really need, however, to ensure that by the time an object gets queried for a message, he has access to all the messages that have been transmitted. In the game analogy, I really want the object to see all the previous activity before he makes his move.

I don't know how to solve this, short of putting a very long sleep or something equally nasty. Could someone indicate me how/if I can solve this?

Thanks a lot
[ May 09, 2007: Message edited by: Alberto Caraz ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi alberto,
Firstly, its not a good idea to spawn a new thread everytime you have to inform your listeners. You should use a thread pool instead.
Are you using jdk 5? If yes you can use the executors available.
If you use a threadpool, you will be submitting tasks to the threadpool. You can use a wait-notify scheme on these tasks to wait for all tasks to complete before you get the next message.
If you are using jdk 5 you can use the FutureTask available therein. If not, you can model your task on the same.
Hope i answered your question.
 
Alberto Caraz
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitesh,

Thanks for your answer! You definitely provided a lot of elegant ways to go about executing tasks asynchronously.

However, the problem with all of the suggestions is that the new FutureTask or Thread from the pool is still not guaranteed to run before the getMessage() method has been called. So unless I WAIT for the thing to return (which I don't want to do and which is exactly why I'm spawning new threads in the first place), I cannot guarantee that this happens. Therefore, I'm not sure if the solutions you suggested actually resolve my problem. Is my reasoning wrong? If not, can you help me think of some other solution?

What I mean is: I do not want to WAIT until the inform() for the listeners is done. I just want to wait enough to make sure that the inform() method has been called (and not necessarily that it has finished). Precisely, I want NOT to have to wait until the method is done. I just want to ensure that I will have called inform() before the next getMessage(), and not that the inform() has ended before the next getMessage().

Once more, thank for your time
[ May 10, 2007: Message edited by: Alberto Caraz ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So unless I WAIT for the thing to return (which I don't want to do and which is exactly why I'm spawning new threads in the first place)

What i thought was you do not want to inform all the listeners sequentially because a slow listener should not make other listeners slow down.
However, it seems you have a different requirement. You want the listeners to process the event asynchronously and may or may not to be informed asynchronously.
So, the responsibility lies on the listeners. The listeners should return as soon as they receive the message. The processing should happen in a separate thread.
This will make the code calling the listeners as single threaded, as follows:



and the listener should follow the following pseudo code:



Hope the above is clear. The difference lies in identifying the point where the sync processing is converted to async.
[ May 10, 2007: Message edited by: Nitesh Kant ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!