Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

want to start new thread when another thread is sleeping  RSS feed

 
Saurabh Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I want to start a thread when another thread has executed once and is sleeping.
I am checking the state of the thread if it is in "Timed_wait" state I am starting the new thread. But in is this the opther thread is never started. I get the state as "Runnable" all the time.
here is code snippet:


Please advise if this is correct way of achieving what I want or any other way.

Thanks and regards,
Saurabh
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your thread do? I mean does it sleep for sometime or wait on a monitor?
The javadoc of TIMED_WAITING says the following:
Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:

* Thread.sleep
* Object.wait with timeout
* Thread.join with timeout
* LockSupport.parkNanos
* LockSupport.parkUntil

Furthermore, i dont think its a good idea to take any actions based on the state as the state may change between checking the state and action execution.
Also, the javadoc for getState is as follows:
Returns the state of this thread. This method is designed for use in monitoring of the system state, not for synchronization control.

So, i dont think it is a good idea to do this.
Probably, if you elaborate on the requirement, i will be able to suggest something else.
 
Saurabh Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nitesh.
I got it working another way.

Actually my application creates 2 thread, thread-1 connects to DB, fetches some records and write to a file. And then sleeps for 15 min and repeat.
The thread-2 starts a process which executes a native "exe" file. This "exe" file reads from the same file thread-1 wrote to every 15 min.

Earlier, I was starting both the threads simultaneously. This, sometimes, resulted in "exe" reading stale data(i.e. thread-1 couldn't finish before "exe").

So, I thought of checking the state of thread-1 and starting thread-2 when Thread-1 is "sleeping"(run once).

Now I am achieving it differently. I am now setting the main thread to sleep for some time. During this time thread-1 finishes once and sets a flag.

When main thread wakes up, it checks the flag and accordingly start thread-2.

It is now working fine. However, better suggestions are always welcome...

Thanks and regards,
Saurabh
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just one thread that periodically does both steps? Then you know the two steps run in sequence. Look into java.util.Timer which is a great way to have something run on a schedule.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, i think its a producer-consumer scenario. You can use a wait-notify mechanism to address this. Googling will give you the results.
The approach you have taken has the following problems:
1) Whenever the main thread wakes up, it is not sure that DB reader has finished its job. So it will waste unnecessary CPU cycles.
2) If the db thread takes long to read data and write to the file(I/O operations may take long), then, the main thread may not be able to see the flag being set to true. This will be in a scenario, where the DB thread has written to file more than once when the main thread was sleeping.(I assume that the DB thread sets the flag back to false when it wakes up.)
So, i think its better to use a way where its 100% sure that when the consumer wakes up there is data to consume.
3) For every read you will spawn a new thread.

Having said the above, i think, this does not make sense when producer is writing one record at a time and consumer is consuming the same.
It will make sense, when the producer batches up the reads and writes a bulk to the file.

If write one - read one is the expected behaviour, then as James suggested, it makes sense to put both the pieces of code in the same thread.

Hope it helps.

[ April 12, 2007: Message edited by: Nitesh Kant ]
[ April 12, 2007: Message edited by: Nitesh Kant ]
 
Saurabh Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James and Nitesh.

I am doing it through TimerTask and it looks fine now.

Thanks for your advise.

Thanks and regards,
Saurabh
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool!

For traditional producer consumer where they may run at different rates, look into BlockingQueue. It does all that wait and notify stuff for you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!