• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with run()

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I am new to thread. I wrote :
public void start()
{
if(runThread==null)
{
runThread.start();
}
}

public void stop()
{
if(runThread!=null)
{
runThread.stop();
}
}

public void run()
{
while (true)
{
System.out.println("RUN");
}
}
and in keylistener event, i wrote,
if (running)
{
runThread.suspend();
running = false;
}

else
{
runThread.resume();
running = true;
}
it compiles perfectly fine. and the program works. but the problem is, it never prints out, RUN which I print out in run(). Can anyone tell me whatz wrong?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's your start() method (I'm guessing this is an applet

If runThread is null when this method is called, you'll get a NullPointerException when it tries to call start() on a null variable. Otherwise, if runThread is non-null, then it won't call start(), in turn, on it! Therefore, you don't see "RUN!" because the Thread is never started.
I think the simplest change that will fix the immediate problem is to change this "==" to a "!=".
There are all sorts of other problems with what you've shown here; the most important is that the deprecated stop() method should almost never be used, and in fact suspend() and resume() should be approached with great caution -- but we'll cross that bridge when we come to it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic