Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run thread from Main method in Java application?

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);



    }


}
like image 303
user482594 Avatar asked Jul 29 '11 20:07

user482594


People also ask

How do you run a thread method in Java?

Java Thread run() methodThe run() method of thread class is called if the thread was constructed using a separate Runnable object otherwise this method does nothing and returns. When the run() method calls, the code specified in the run() method is executed. You can call the run() method multiple times.

How do I access main thread?

How to access Main thread? For accessing main thread you require the Thread class object to refer it. You can create this by using the CurrentThread property of the Thread class. It will return the reference to the thread in which it used.

Can we start thread using Run method?

No. start method of thread class is implemented as when it is called a new Thread is created and code inside run() method is executed in that new Thread. While if run method is executed directly than no new Thread is created and code inside run() will execute on current Thread and no multi-threading will take place.

Which method is used for main thread?

The main thread is created automatically when our program is started. To control it we must obtain a reference to it. This can be done by calling the method currentThread( ) which is present in Thread class. This method returns a reference to the thread on which it is called.


2 Answers

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.

like image 108
Nathan Hughes Avatar answered Sep 21 '22 12:09

Nathan Hughes


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());



    }


}
like image 44
Jason Wheeler Avatar answered Sep 21 '22 12:09

Jason Wheeler