Java There are three main ways to realize multithreading: inheriting Thread class, implementing Runnable interface, using Executor Service, Callable and Future to realize multithreading with returned results. The first two methods of thread execution have no return value, only the last one is with return value.
1. Implementing multithreading by inheriting Thread class
Although the method of inheriting the Thread class is listed as a multi-threaded implementation by me, Thread is essentially an instance of implementing the Runnable interface, which represents an instance of a thread, and the only way to start a thread is through the start() instance method of the Thread class. The start() method is a native method that starts a new thread and executes the run() method. This way to achieve multithreading is very simple. By extending Thread directly through its own class and copying run() method, you can start a new thread and execute the run() method you defined. For example:
Start threads where appropriate as follows:
2. Implementation of Runnable Interface for Multithreading
If your own class already extends another class, you cannot extend Thread directly. At this point, you must implement a Runnable interface, as follows:
To start MyThread, you need to first instantiate a Thread and pass in your own MyThread instance:
In fact, when a Runnable target parameter is passed to Thread, the run() method of Thread calls target.run(), referring to the JDK source code:
3. Using ExecutorService, Callable and Future to Realize Multithreading with Return Results
Executor Service, Callable, Future are all functional classes in the Executor framework. For a detailed understanding of the Executor framework, you can visit http://www.javaeye.com/topic/366591, which is explained in detail here. Threads returning results are a new feature introduced in JDK 1.5. It's really practical. With this feature, I don't have to go through a lot of trouble to get the return value, and even if it's implemented, there may be a lot of loopholes.
Tasks with returnable values must implement the Callable interface. Similarly, tasks without returnable values must have the Runnable interface. After the Callable task is executed, a Future object can be obtained, and the Object returned by the Callable task can be obtained by calling get on the object. Combined with the thread pool interface ExecutorService, the legendary multithreading with the return result can be realized. The following provides a complete multi-threaded test example with returned results, which has been verified under JDK 1.5 that no problem can be used directly. The code is as follows:
Code description:
The Executors class in the above code provides a series of factory methods for pioneering thread pools, and the returned thread pools all implement the ExecutorService interface.
public static ExecutorService newFixedThreadPool(int nThreads)
Create a pool of threads with a fixed number of threads.
public static ExecutorService newCachedThreadPool()
Create a cacheable thread pool and call execute to reuse previously constructed threads if they are available. If no existing thread is available, create a new thread and add it to the pool. Terminate and remove threads that have not been used for 60 seconds from the cache.
public static ExecutorService newSingleThreadExecutor()
Create a single threaded Executor.
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
Create a thread pool that supports timed and periodic task execution, which in most cases can be used to replace the Timer class.
ExecutoreService provides a submit() method that passes a Callable, or Runnable, back to Future. If the Executor background thread pool has not completed the Callable calculation, the call returns the get() method of the Future object, blocking until the calculation is completed.
Reproduced in: http://www.cnblogs.com/yezhenhan/archive/2012/01/09/2317636.html