• Post Reply Bookmark Topic Watch Topic
  • New Topic

Priority in Thread

 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a class called as MessageHandler in which a method of another class MessageClient is called. MessageClient is a class which reads and writes to a socket. It sends a message, spawns another thread (say A) to read it, once the message is read it spawns another thread (say B) to process it. The processing might take long. So I want that once the message it read by the thread A it should return back to the MessageHandler class and the processing by thread B keeps happening later. I tried putting thread A on a MAX_PRIORITY and B on MIN_PRIORITY but still sometimes a part of B is executed before A returns.

Could you please guide?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread priorities should not be relied on. If you *need* that behavior you should implement it in some other, definite, way.
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayank gupta wrote:It sends a message, spawns another thread (say A) to read it, once the message is read it spawns another thread (say B) to process it. The processing might take long.


Why are you starting thread A, only to wait for it to finish, and then start thread B? Why can't thread A start thread B?

If the thread that starts thread A, simply waits, you can achieve the same thing by having the thread do the action without starting thread A.


mayank gupta wrote: but still sometimes a part of B is executed before A returns.


If you mean that thread B is running before thread A is done, then obviously, thread B was started too early.

If you mean that thread B is running before the thread that started it returns, then it depends... If it is only a few milliseconds, then it is fine. That's how schedulling works -- without using synchronization, there is no way to enforce the guarantee that one task will do something before another. If it is more than a few milliseconds, then you should look into the code. You could have a dependancy that requires one thread to block on the other thread.

Henry
 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you suggest how?
 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry Henry. I wanted to say that thread A starts Thread B. I didnt specify that earlier. I want thread A to finish and return and then Thread B to start processing.
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayank gupta wrote:I am sorry Henry. I wanted to say that thread A starts Thread B. I didnt specify that earlier. I want thread A to finish and return and then Thread B to start processing.


If thread A starts thread B as its last operation, how do you even notice that thread A didn't terminate? Why do you even care?

Henry
 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I care because I want the message that is read from the network to be returned to MessageHandler class. At the same time in thread B i want it to proceed doing something else with the message.
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayank gupta wrote:I care because I want the message that is read from the network to be returned to MessageHandler class. At the same time in thread B i want it to proceed doing something else with the message.



Then I definitely missed something else -- as you said thread A was started just for the read. There is no "return", as it is going to terminate. Please clarify....

Henry
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway it looks like you have two threads that need an order, and are trying to use thread priorities... as Ulf suggested -- don't.

Use synchronization and the wait/notify mechanism.

Henry
 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might have done it the wrong way, please help.


MessageHandler calls MessageClientObj.sendMessage(). Inside MessageClient the senMessage() method, the message is writen to the socket and it spawns a thread A to read the reply message from the network. The reply message that is read needs to go back to the MessageHandler class. Also, thread A spawns another thread B which then performs some additional action on the reply message. Now I want the reply message to first reach the MessageHandler class so that only after some processing is done on it, is the other action that thread B is performing, should be completed.
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayank gupta wrote:Now I want the reply message to first reach the MessageHandler class so that only after some processing is done on it, is the other action that thread B is performing, should be completed.


How is this message being sent to the message handler class? Obviously, it is not being returned, as that method should have returned right after it started thread A.

Regardless, as already mentioned, you will need to synchronize, wait/notify, etc. There is no way to guarantee what you want by thread priorities.

Henry
 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i capture the message received from the network and return it to MessageHandler?
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayank gupta wrote:How can i capture the message received from the network and return it to MessageHandler?


Interesting. This topic is written in a way that you already know how to pass the response back -- and is trying to solve a race condition. From your previous post, now you are implying that you didn't implemented it yet. Please elaborate on what you want.

Thanks,
Henry
 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had been trying to solve the race condition to somehow solve this problem of passing the response back. I do realise I was using a complicated mechanism to solve it.
I want to know how to solve this problem of sending the response back.
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayank gupta wrote:I had been trying to solve the race condition to somehow solve this problem of passing the response back. I do realise I was using a complicated mechanism to solve it.
I want to know how to solve this problem of sending the response back.


No magic here. As already mentioned, you will need to synchronize and wait/notify. It doesn't matter how you get the response back. Thread B will need to wait for a ready state. And you need to set that state, once you get the response back, and is ready to set thread B free.

Henry
 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the suggestion. I will try it and get back with the results.
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

BTW... forgot to mention... if you don't want to deal with synchronization, status flag, wait and notify, and getting it all right, you can also use the java.util.concurrent.CountDownLatch class.

Just create a count down latch prior to creating thread B. Have thread B, call await() on the count down latch as it first operation. Then when the main thread is ready, it can release thread B via a call to the countDown() method.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!