5

I believe variables used in static main method should be also static as well. The problem is that I cannot use this in this method at all. If I remember correctly, I have to initiate thread with commnad myThread = new ThreaD(this).

The below codes produces an error because I used this in thread initiation. What have I done wrong here?

package app;

public class Server implements Runnable{

    static Thread myThread;


    public void run() {
        // TODO Auto-generated method stub  
    }

    public static void main(String[] args) {
        System.out.println("Good morning");

        myThread = new Thread(this);



    }


}
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
user482594
  • 16,878
  • 21
  • 72
  • 108

3 Answers3

12

You can't use this because main is a static method, this refers to the current instance and there is none. You can create a Runnable object that you can pass into the thread:

myThread = new Thread(new Server());
myThread.start();

That will cause whatever you put in the Server class' run method to be executed by myThread.

There are two separate concepts here, the Thread and the Runnable. The Runnable specifies what work needs to be done, the Thread is the mechanism that executes the Runnable. Although Thread has a run method that you can extend, you can ignore that and use a separate Runnable.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Hey thanks!, I just have one more question. It seems that thread always goes with method name `run()`. So, if I want to create another thread that does totally different job from the first one, do I have to create a new class and make a `run()` method there? and use that class to create a thread? – user482594 Jul 29 '11 at 20:10
  • @user482594: you don't have to, but it's a good idea to keep threads and runnables separate. Have runnables that are focused on particular tasks, then you can create threads and pass runnables into them. (But also check out Executors.) – Nathan Hughes Jul 29 '11 at 20:12
3

Change new Thread(this) to new Thread(new Server()):

package app;

public class Server implements Runnable{

    static Thread myThread;


    public void run() {
        // TODO Auto-generated method stub  
    }

    public static void main(String[] args) {
        System.out.println("Good morning");

        myThread = new Thread(new Server());



    }


}
Jason Wheeler
  • 872
  • 1
  • 9
  • 23
0
class SimpleThread extends Thread {
    public SimpleThread(String name) {
        super(name);
    }
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i + " thread: " + getName());
            try {
                sleep((int)(Math.random() * 1000));
            } catch (InterruptedException e) {}
        }
        System.out.println("DONE! thread: " + getName());
    }
}

class TwoThreadsTest {
    public static void main (String[] args) {
        new SimpleThread("test1").start();
        new SimpleThread("test2").start();
    }
}
fatnjazzy
  • 6,070
  • 12
  • 57
  • 83