0
    public Thread thread = new Thread();

    public void start() {
        running = true;
        thread.start();
    }

public void run() {

    while(running) {

        System.out.println("test");

        try {
            thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

My problem is that the program will not print out "test" nor will it seem to loop despite 'running' being true. Is there a way I can continuously loop in the run method?

3 Answers3

2

You haven't actually asked run() to be called. All you've done is declare a run() method unrelated to the Thread.

Put your run() method in a Runnable and pass that to the Thread.

public Thread thread = new Thread(new Runnable() {

    public void run() {

        while (running) {

            System.out.println("test");

            try {
                thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
});
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • if class implements Runnable: 'Thread name = new Thread(this);' . If not, change 'this' to the name of your runnable. You need to pass your runnable to a thread. Edit: commented on the wrong post. Sorry, on my phome, hard to navigate. – Vince Oct 22 '13 at 21:07
2

The problem appears to be that you aren't running the run method that you think you're running in the thread.

First, you've created a Thread called thread. In your class's start method, you set running to true and call thread.start(). But that just calls Thread's run() method, which does nothing.

public void run()

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

You aren't calling your own run method.

You have created a run method. I can't see your class definition here, but I'm assuming that your class implements Runnable. You need to send an instance of your class as an argument to the Thread, by using the Thread constructor that takes a Runnable. Then the Thread will know to run your Runnable's run() method.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

Well you need to call start() to start the thread. Otherwise neither running will be true nor thread.start() get executed. Well i can guess you were intended to do something like this:

class MyTask implements Runnable
{
   boolean running = false;
   public void start() {
        running = true;
        new Thread(this).start();
    }

public void run() {

    while(running) {

        System.out.println("test");

        try {
            Thread.sleep(1000); 
              // you were doing thread.sleep()! sleep is a static function
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

  public static void main(String[] args)
  {
     new MyTask().start();
  }
}
Sage
  • 15,290
  • 3
  • 33
  • 38