Actually, what other ways are available apart from开发者_开发知识库 extending the Thread class and implementing the Runnable interface?
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:
- Implement the interface
java.lang.Runnable
and pass an instance of the class implementing it to theThread
constructor. - Extend
Thread
itself and override itsrun()
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.
Threads can be created mainly in 3 different ways
- Extend the java.lang.Thread class'
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.
}
}
- Implement the java.lang.Runnable interface
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();
}
}
- Implement the java.util.concurrent.Callable interface
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
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);
There are actually total 4 ways to create thread in java :
- By extending
java.lang.Thread
class - By implementing
java.lang.Runnable
interface - By using anonymous inner class
- By implementing
Callable
interface.
There are only Two ways for creating a Thread already you have mentioned but a third way is there to invoke the Thread.
In java1.5 there is another way to invoke a thread. That is by “ExecutorService”. All these classes are from the “java.util.concurrent” package. There are various ways to create a “ExecutorService” using “Executors” factory class. The following is one of the way to create “ExecutorService”..
ExecutorService es= Executors.newSingleThreadExecutor();
RunnableImpl r = new RunnableImpl();
Future fu=es.submit(r);
Using “ExecutorService” methods we can submit eighter Runnable or Callable to the service for execution.
How ever this cannot be said as the new way to create a Thread. It is because ExecutorService internally uses “ThreadFactory” class to create a new thread which internally uses eighter first or second method. So we have to say that there are only two ways to create threads but there is a new way in java1.5 to invoke a thread but not to create a 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
精彩评论