Actually, what other ways are available apart from extending the Thread class and implementing the Runnable interface?
Threads can be created mainly in 3 different ways
class SampleThread extends Thread {
//method where the thread execution will start
public void run(){
//logic to execute in a thread
}
//let’s see how to start the threads
public static void main(String[] args){
Thread t1 = new SampleThread();
Thread t2 = new SampleThread();
t1.start(); //start the first thread. This calls the run() method.
t2.start(); //this starts the 2nd thread. This calls the run() method.
}
}
class A implements Runnable{
@Override
public void run() {
// implement run method here
}
public static void main() {
final A obj = new A();
Thread t1 = new Thread(new A());
t1.start();
}
}
class Counter implements Callable {
private static final int THREAD_POOL_SIZE = 2;
// method where the thread execution takes place
public String call() {
return Thread.currentThread().getName() + " executing ...";
}
public static void main(String[] args) throws InterruptedException,
ExecutionException {
// create a pool of 2 threads
ExecutorService executor = Executors
.newFixedThreadPool(THREAD_POOL_SIZE);
Future future1 = executor.submit(new Counter());
Future future2 = executor.submit(new Counter());
System.out.println(Thread.currentThread().getName() + " executing ...");
//asynchronously get from the worker threads
System.out.println(future1.get());
System.out.println(future2.get());
}
}
Favor Callable interface with the Executor framework for thread pooling.
The Runnable or Callable interface is preferred over extending the Thread class
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. a ThreadFactory implementation will instantiate Thread
objects at some point, ...).
There are two different ways to specify which code to run in that Thread:
Thread
itself and override its run() method.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.
for creating a thread there is only one way in java
ie. Thread class start() method but there are different ways to run the thread by using different ways
like 1.Thread
2.Runnable
3.RunnableFeature
4.Callable
5.ERxecutorService...etc
There are actually total 4 ways to create thread in java :
java.lang.Thread
classjava.lang.Runnable
interfaceCallable
interface.Or you can create a Callable which is an interface which is similar to Runnable except that it defines a method call that can return a value. To instantiante a Callable, you can pass it to an executor. You can find a full explanation of multithreading and callable examples here
The preferred way of starting threads in Java 6 is using Executors:
ExecutorService es = Executors.newCachedThreadPool();
Runnable r = <your runnable here>;
es.execute(r);