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

Thread not waking up after sleep  RSS feed

 
Gerome Kawa
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
In the following programs AcceptAndSaveTransmittedFile servlet spawns the thread SendResponseThread.
The intention is the thread will be spawned when the servlet is initialized and then the thread will look into Transmission
folder and move files to TransmissionReceived folder at 10sec interval.

But after the first time the thread moves the folders , it goes to sleep and doesn not wake up !!!
Any idea why this is happening. I would hughly appreciate if someone can let me know where am making the mistake.





[EJFH Added "CODE" tags for formatting.]
[ January 25, 2005: Message edited by: Ernest Friedman-Hill ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me as if after the sleep() call, the run() method will return, which will terminate the Thread. Were you expecting something else to happen?
 
Gerome Kawa
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am expecting the run() method to be called after the duration of the sleep. but it is not happening after the first execution of run()
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The run() method of a Runnable is invoked exactly once by the Thread. When it returns, the Thread dies. If you want the run() method to keep going after the sleep() returns, put the whole body of the run() method -- or at least the part that you want to repeat -- in a "while (true)" loop.
 
Gerome Kawa
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does while(true) mean ... i mean what is 'true' or what needs to be 'true' to keep the run() method running always ?

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This will run the code over and over again.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gerome Kawa:
what does while(true) mean ... i mean what is 'true' or what needs to be 'true' to keep the run() method running always ?
In the codethe <statements> will be executed repeatedly as long as <expression> evaluates to the literal boolean value "true". Since the literal boolean value "true" is equals to the literal boolean value "true" (itself), "while ( true )" repeats indefinitely. You can use "break" to exit the loop (same with for loops).

If you want something more explicit, how about this?"! done" of course evaluates to "true" since "! false == true".
[ January 27, 2005: Message edited by: David Harkness ]
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even with the while loop, there are other more serious threading issues with that code .....

There are potentially many threads in the Servlet's GET/POST logic.

There is only one SendResponseThread.

Think about .... well, the static file counter for one ....

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