• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Thread not waking up after sleep

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This will run the code over and over again.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
brevity is the soul of wit - shakepeare. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic