Oscar wrote:
Thanks for you reply.I've finished the rough version, running all the
business-side code in the main thread.

void main()
{
  // initialize all the mina stuff

 //fetching the message from the queue
}


void messageReceived(...)
{
 // put the message into the queue.
// need to lock the queue
}

The reason why I want to do it is to make the logic-side code clear and easy
to handle.
We often do this way when we are developing MMO Games. ;)
I have done that too (ie, pushing the message in a blocking queue, and let the main thread process them), but after a few time, I found it more convenient to delegate the process to the handler thread. You just have to assume that the main thread is the Handler thread :)

Now, from the performance POV, the main issue is that if you are doing a lot of processing after having received a message, then you will block one thread for a while, potentially forbid other sessions to be processed. There are solutions for that. In any cases, it's better than having one single thread dealing with all the messages !




On Fri, Mar 13, 2009 at 12:02 AM, Thomas Harning Jr. <[email protected]>wrote:

On Thu, Mar 12, 2009 at 10:45 AM, Oscar <[email protected]> wrote:
hi all-
To simplify the business-side code, I wanna run the messageReceived in
the
main thread.

I know I could put all the received object to the  unique queue, and then
the main thread fetch and handle the message in the queue.

Any better way?
If this sort of mechanism is what you absolutely want, then I suggest
having the messageReceived event post the work-item to an Executor (in
your case, an instance created by
Executors.newSingleThreadExecutor()).

That way all work is done in a single thread.

If you want things run in the main thread... then you're "stuck" with
starting the Mina queue and using a BlockingQueue implementation.

--
Thomas Harning Jr.




--
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org


Reply via email to