Solved

Why doesn't my thread wake up

Posted on 2006-07-05
12
231 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

707 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

14 Experts available now in Live!

Get 1:1 Help Now