How many ways to create Thread
There is exactly one way to create a new thread in Java and that is to instantiate java.lang.Thread (to actually run that thread you also need to call start()).
Everything else that creates threads in Java code falls back to this one way behind the cover (e.g. aThreadFactory implementation will instantiate Thread objects at some point, ...).
There are three different ways to specify which code to run in that Thread:
1. Implement the interface java.lang.Runnable and pass an instance of the class implementing it to the Thread constructor.
2. Extend Thread itself and override its run() method.
3. Divide and Conquer by Java Threads
The first approach (implementing Runnable) is usually considered the more correct approach because you don't usually create a new "kind" of Thread, but simply want to run some code (i.e. a Runnable) in a dedicated thread.
1. Implementing the Runnable Interface
The Runnable Interface Signaturepublic interface Runnable {
void run();
}
One way to create a thread in java is to implement the Runnable Interface and then instantiate an object of the class. We need to override the run() method into our class which is the only method that needs to be implemented. The run() method contains the logic of the thread.
The procedure for creating threads based on the Runnable interface is as follows:
1. A class implements the Runnable interface, providing the run() method that will be executed by the thread. An object of this class is a Runnable object.
2. An object of Thread class is created by passing a Runnable object as argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method.
3. The start() method is invoked on the Thread object created in the previous step. The start() method returns immediately after a thread has been spawned.
4. The thread ends when the run() method ends, either by normal completion or by throwing an uncaught exception.
Below is a program that illustrates instantiation and running of threads using the runnable interface instead of extending the Thread class. To start the thread you need to invoke the start() method on your object.
2.Extending Thread Class
The procedure for creating threads based on extending the Thread is as follows:
1. A class extending the Thread class overrides the run() method from the Thread class to define the codeexecuted by the thread.
2. This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call.
3. The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running.
Below is a program that illustrates instantiation and running of threads by extending the Thread class instead of implementing the Runnable interface. To start the thread you need to invoke the start() method on your object.
3. Divide and Conquer
There is a interesting property of threads which comes to our rescue.Thread maintains the private copy of object on which it operates called asworking copy in which it keeps the local modification which are made to the object. While main memory contains the master copy of every object. After all threads stop executing all the working copies are merged into the master copy. This becomes the curse in case of multiple objects modify the same field in the object because merging back creates the confusion and results in a unpredictable behavior. However if each thread works on the different field of the object merging won't cause a conflict and hence state remains consistent and predictable. This improves the performance many folds and hence application gain scalability which was missing in the single threaded application.
Snippet:
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
final List list = new ArrayList();
Person person = null;
for (int i = 0; i < 100; i++) {
person = new Person();
Runnable runnable = createFirstRunnable(person);
Runnable run = createSecondRunnable(person);
Thread thread = new Thread(runnable);
Thread tt = new Thread(run);
thread.start();
tt.start();
list.add(person);
}
for (Person p : list) {
System.out.println("This is " + p.getFirstName() +
" " + p.getLastName() + "'s Blog.");
}
}
private static Runnable createSecondRunnable(final Person person) {
Runnable run = new Runnable() {
@Override
public void run() {
person.setLastName("Gupta");
}
};
return run;
}
private static Runnable createFirstRunnable(final Person person) {
Runnable runnable = new Runnable() {
@Override
public void run() {
person.setFirstName("Subodh");
}
};
return runnable;
}
private static class Person {
private String firstName = null;
private String lastName = null;
public Person() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
}
Ref: Subodh's Blog, javabeginner