Thread

class MyThread extends Thread

{

public void run()

{

task code

}

}

Here is a simple procedure for running a task in a separate thread:

1. Place the code for the task into the run method of a class that implements the Runnable interface. That interface is very simple, with a single method:

public interface Runnable

{

void run();

}
You simply implement a class, like this:

class MyRunnable implements Runnable
{
public void run()
{
… task code …
}
}

2. Construct an object of your class:

Runnable r = new MyRunnable();

3. Construct a Thread object from the Runnable:

Thread t = new Thread(r);

4. Start the thread.

t.start();

IMPORTANT:

java.lang.Runnable 1.0

  • void run()

You must override this method and supply the instructions for the task that you want to have executed.

java.lang.Thread 1.0

  • Thread(Runnable target)

constructs a new thread that calls the run() method of the specified target.

  • void start()

starts this thread, causing the run() method to be called. This method will return immediately. The new thread runs concurrently.

  • void run()

calls the run method of the associated Runnable.