Threads in Java are similar to any other objects in Java. Usually, threads are instance of Java class java.lang.thread or thread will be the instance of subclass belonging to the class java.lang.thread. Thread classes will use methods and constructors to perform operations on the thread. Extension of object class and implementation of runnable interface are done using thread class.
Creating a Thread
The syntax for creating a thread by deriving it from a thread class is:
Public class derived class extends TH1
{
Public void run()
{
*/ A implemented Class TH1 (Thread) */
}
}
To start a thread the programmer must call the start method of the thread using TH1.start();
If no code is detailed for the thread execution, the thread will be stopped immediately after the start of the thread. The execution of a thread can be specified using two ways. The first way to execute a thread is to create a subclass and override the run() method. The second way is to pass an object which implements the java.lang.runnable thread to the constructor.
Thread subclass Method
First thing to do in this method is to specify the code which runs in the thread. Then create a subclass for this particular thread and override the run() method. The thread will execute the run() method after the call of start(). The example given below will help you to understand better.
public class World1 extends TH1
{
public void RUN()
{
system.out.println(“This world is Soulful”);
}
To create a thread in the above example you can use the following code:
World1 myTH1= new World1;
myTH1.start();
As soon as the thread is started, the start() call will be returned. The start() call won’t wait for the execution of the run() method. After the execution of the run() method; the text “This world is soulful” will be printed.
The anonymous subclass of thread is created using:
Thread TH1= new TH1()
{
Public void RUN()
{
System.out.println (“This is a Run class Thread”);
}
}
TH1.start();
In the given example, the text “This is a Run class Thread” will be printed out as soon as the execution of the run method is completed by a new thread.
Runnable Interface:
Runnable Interface is the second method to define the code which will be executed by the thread. This method is done by creating a class which will implement java.lang.runnable. The runnable object will be executed by a thread. The example for implementing runnable interface is given below.
public class W1 implements Runnable
{
public void RUN()
{
System.out.println(“This World is soulful”);
}
}
After passing the instance of W1 to a thread, the run() method will be executed. The following code is an example for passing the instance to the class.
Thread TH1= new TH1(new W1());
Thread.start();
The thread will call the RUN() method of the World instance and print the text “This World is soulful”.
- Many: Java interview Q&A available here
The example for anonymous implementation of Runnable is:
Runnable W1= new Runnable()
{
public void RUN()
{
System.out.println (“This World is an Amazing place”);
}
}
Thread TH1= newTH1(new W1());
TH1.start();
Which is Best Subclass or Runnable?
There is no definition for which of these two methods are best. Most Java programmers personally prefer implementation of Runnable method because the execution Runnable is done by thread pool which makes it easier to queue up the instances of Runnable. Sometimes Implementation of both Subclass and Runnable methods in a single thread will be required by the instance.
Common mistakes consummated by developers while creating a thread:
Calling RUN() instead of start().
While creating a thread most programmers will run() the method of the thread instead of the start(). The example code is given below.
Thread newTH1= newTH1(W1());
newTH1.RUN(); // In this position, Function should will be start();
In the above code run() method will be executed first but it is not executed by the newly created thread. Rather the run() method is executed by the thread which created the new thread. To execute the code using new thread, the programmer should call the thread using newTH1.start() method.
Thread Name
A name is given to every thread created using Java. The name is used to differentiate between the threads. For an example if several threads are used to write System.out, using different names between the threads will help you to identify which thread wrote the particular thread.
Thread.currentThread
The method Thread.currentThread() will return a reference to the instance which executes the currentThread(). Using this process access to the Java thread object which representing a thread executing a single code block. If you have a reference to thread object, it is easy to call methods on it.
Thread names and Thread.currentThread are not compulsory but using these will make the coding process easier.