The threads can be created in Java in
the following two ways.
1. By implementing the Runnable interface.
2. By extending the Thread class also you can create a thread.
Creating thread by implementing
Runnable interface:
You can easily create thread by
defining a class that implements Runnable
interface. Any object in Java that implements Runnable interface is capable of producing threads. Besides implementing
Runnable interface, the class must
also have to define some other methods and constructors as stated below.
1. The class that implements Runnable interface must override a method called run () defined by the interface.
2. You must instantiate an object of type Thread from within the class that
implements the Runnable interface by
defining one of the constructors defined by the Thread class.
3. You need to define the start () method within the class that implements Runnable interface. The start () method is defined by the Thread class. The start () method will call the run
() method. So until you call the start
() method the thread will not run.
Before we start with the program example,
we will first analyze the general forms of the above mentioned methods and
constructor.
a) The run
() method:
public void run ()
|
The run () method is defined by the interface Runnable. This method can call other methods, use other classes and
declare variables, just like the main thread can do. The run () method sets up the entry point for another thread that is in
concurrent execution within a program. The thread will be ended when the run () method returns.
b) The Thread
class defines many constructors. The two of which are shown below
public Thread (Runnable threadObject, String
threadName)
|
public Thread (String threadName)
|
c) The start
() method:
The start () method is defined by Thread. Until you call the start () method the thread will not
start running. The start () method
executes the call to the run () method.
The general form is as shown below.
void start ()
|
The below example program illustrate the
creation of a thread by implementing Runnable
interface.
package blog.exception.mainThread;
public class MyThread implements Runnable
{
Thread th;
MyThread()
{
th=new Thread (this, "My Child
Thread");// constructor of Thread
class.
System.out.println("My Thread Name "+th);
th.start();
}
@Override
public void run()
{
try
{
for(int i=3; i>0 ;i--)
{
System.out.println("My Child Thread "+i);
Thread.sleep(500);
}
}
catch(InterruptedException ex)
{
System.out.println("error in child "+ex);
}
}
}
|
package blog.exception.mainThread;
public class DemoMyThread
{
public static void main(String[] args)
{
new MyThread();
try
{
for(int i=3;i>0;i--)
{
System.out.println("Main Thread "+i);
Thread.sleep(1000);
}
}
catch(InterruptedException ex)
{
System.out.println("error in main thread
"+ex);
}
System.out.println("Main Thread
Terminating");
}
}
|
Output:
My Thread Name Thread[My Child Thread,5,main]
Main Thread 3
My Child Thread 3
My Child Thread 2
My Child Thread 1
Main Thread 2
Main Thread 1
Main Thread Terminating
|
The output
may vary depending on your system speed.