Solved

Why doesn't my thread wake up

Posted on 2006-07-05
12
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to debug htl and js pages 8 79
ArrayList Adding/settign data 3 37
Bitwise and to sum elements 2 53
Read CLOB data from Oracle using JAVA 3 41
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

739 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