Solved

Why doesn't my thread wake up

Posted on 2006-07-05
12
258 Views
Last Modified: 2010-03-31
I'm working on a servlet.  In the init() method I just call another method, startUp().

Basically I'm polling a database table for a certain value.  If the value = 0, I want the thread to sleep for 30 seconds...maybe a process is going to update the table soon..and then I poll again.  But the thread doesn't wake up!!  It dies!

This is all I'm doing:

boolean running = true;
while (running) {
  // POLL TABLE, DO WORK
  if (key = 0)
  {
    try
    {
      logger.debug("Sleeping");
      Thread.sleep(30000);
    }
    catch (Exception ex)
    {
      printStackTrace(ex);
    }
  }
}

Why does it die, and how can I keep it going, but sleep for a bit?
0
Comment
Question by:arctanx
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17046303
>>if (key = 0)

I think you meant

if (key == 0)

but how is 'key' being handled?
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17046342
> // POLL TABLE, DO WORK
By any chance, this routine has return statement.
0
 
LVL 2

Author Comment

by:arctanx
ID: 17046443
Yeah I meant key == 0.

There's some code that says

key = getKey()

which polls a db table for a certain code, if the code is 0, I want the thread to sleep.

I should note that I'm in the process of converting a service, with a main(String args[]) method, to a servlet, with init().  So I'm getting rid of the main method.

Could this be it??

No returns afaik...

..and it works fine as a service, why when I switch to a servlet it doesn't.

BTW, my servlet conversion was just extending HTTPServlet, and adding init(), and that's it.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 17046564
Are there any fields? A servlet shouldn't have them
0
 
LVL 92

Expert Comment

by:objects
ID: 17047078
you shouldn't be sleeping in init()
0
 
LVL 2

Author Comment

by:arctanx
ID: 17047223
Yeah there's a few (fields that is).

Should I do the work in a new class in main or run, and have the servlet call the new class?

Like...

while (running)
{
  new NewClass(ARGS HERE);
}

class NewClass
{
  main (String args[])
  {
    yada, yada, yada
    ** thread will sleep here **
  }
}
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 17047282
> Yeah there's a few (fields that is).

theres nothing stopping you having fileds :)

> Should I do the work in a new class in main or run, and have the servlet call the new class?

yes, and have the new class start a new thread that does the waiting.
0
 
LVL 2

Author Comment

by:arctanx
ID: 17047339
cool, i'll try that, i'll redo my init method to call a new class with main.

i'll close this and post a new question if i still have problems.

(geez i'm starting to sound like help desk).
0
 
LVL 92

Expert Comment

by:objects
ID: 17047353
it doesn't need a main, it would be more like:

public class WaitClass implements Runnable
{
   ...
   public WaitClass()
   {
      new Thread(this).start();
   }

   public void run()
   {
      // do your waitng in here
   }
}
0
 
LVL 2

Author Comment

by:arctanx
ID: 17047358
awesome!
thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17048563
So the code you posted was not a separate thread?
0
 
LVL 2

Author Comment

by:arctanx
ID: 17049912
No not at first.

It is now, it works good, did it last night.

Plus I was extending Thread, now I'm using Runnable and doing things in the run method.

My servlet just calls to the new class.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question