Implementing Runnable:
The simplest way to create a thread is to create a class which implements the Runnable interface. A Runnable abstracts a unit of executable code. You could construct a thread on any object which implements Runnable. For implement Runnable, a class required only implements a single method called run ( ), which is declared like this:
public void run ( )
Inside run ( ) you will declare the code which constitutes the new thread. It is important to understand which run ( ) can call other methods, use other classes, and declare variables, only like the main thread can. The only difference is that run ( ) establishes the entry point for another, concurrent thread of execution inside your program. This thread will end whenever run ( ) returns.
After you create a class which implements Runnable, you would instantiated an object of type Thread from inside that class. Thread declares various constructors. The one which we will use is display here:
Thread (RunnableOb,String threadName)
In this constructor, threadOb is an object of a class which implements the Runnable interface. This declares where execution of the thread will start. The name of the new thread is specified through threadName.
After the new thread is created, it will not begin running until you call its start ( ) method, that is declared inside Thread. Within essence start ( ) .executes a call to run ( )
The start( ) method is displays here:
void start ( )
Here is an instance which creates a new thread and begins it running.
/ / Create a second thread.
class NewThread implements Runnable {
Thread t ;
NewThread ( ) {
/ / Create a new, second thread
t = new Thread (this, "Demo Thread");
System.out.println ("Child thread:" + t);
t. start ( ); / / Start the thread
}
/ / This is the entry point for the second thread.
public void run ( ) {
try {
for (int i= 5; i> 0; i - - ) {
System.out.println ("Child Thread:" + i);
Thread.sleep (500) ;
}
} catch (InterruptedException e) {
System.out.println ("Chiled intrrupted".);
}
System.out.println ("Exiting child thread.");
}
}
class ThreadDemo {
public static void main(String args [ ] ) {
new NewThread ( );
try {
for( int i= 5 ; i> 0;i - - ) {
system.out.println("Main Thread:" + i);
Thread.sleep (1000);
}
}catch (InterrruptedExpection e ) {
System.out.println ("Main thread interrupted.");
}
System.out.println ("Main thread exiting.");
}
}
A new Thread object Inside NewThread's Constructor is created through the subsequent statement.
t= new thread (this, "Demo Thread");
Passing this as the first argument denotes which you need the new thread to call the run ( ) method on this object. Further, start ( ) is called, that begins the thread of execution beginning at the run ( ) method. That causes the child threads for loop to being. After calling start ( ), NewThread's constructor returns to main ( ). Whenever the main thread resumes, it enters it's for loop. All threads continue running sharing the CPU until their loops finish. An output generate through this program is as follows:
Child thread: thread[demo thread ,5 ,main]
Main Thread: 5
Child Thread : 5
Child Thread : 4
Main Thread : 4
Child Thread : 3
Child Thread : 2
Main thread : 3
Child Thread : 1
Exiting child thread.
Main thread: 2
Main Thread :1
Main thread exiting.
As described earlier, in a multithreaded program, a main thread must be the last thread to finish running. The Java run-time system may "hang" if the main thread finished before a child thread has completed. A preceding program ensures in which the main thread finished last since the main thread sleeps for 1,000 milliseconds among iteration's and the child thread sleeps for only 500 millisecond. This causes the child thread to terminate previously than the main thread. Soon, you will see a better way to ensure in which the main thread finishes last.