Solved

Why doesn't my thread wake up

Posted on 2006-07-05
12
245 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java asynchronous logging 4 50
Impossible to extract MSI from new JAVA releases 2 49
print map entry 34 55
Error in @AspectJ Based AOP with Spring 2 1
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now