Solved

Why doesn't my thread wake up

Posted on 2006-07-05
12
251 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 96
Apps blocked by Java 9 79
Desingning Refactoring existing code 2 24
eclipse console opening separately 2 21
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

773 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