• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with run()  RSS feed

 
Sandy Abraham
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!