In Java concurrency, there are two low level concurrency implementations in Java to create a thread - extend the Thread class or implement Runnable interface. These basic approaches neither return value from the thread nor throw exception should anything goes wrong. There's a third approach since Java 1.5 - use Java Concurrency package which supports to receive return values from thread execution. approach #1: extend Thread classWhen you extend the Thread class to create your custom Thread class, you also implement a run() method which will be invoked when the thread gets started. Since you extend Thread class, you won't be able to extend any other class. Therefore, this approach has the least flexibility for coders. public class MyThread extends Thread{ approach #2: implement Runnable interfaceWhen you implements the Runnable interface to create your own custom thread class, you also need to implement a run() method which will be invoked when your thread gets started. Your custom thread class will be a thread that you instantiate with your Runnable via a constructor. After that, starting a thread is the same, you call upon start() method of the thread to spawn a thread. This approach of implementing the Runnable interface leaves some room for the Thread class to implement other interfaces and extend one other class. public class MyRunnable implements Runnable{ [1] A Runnable is passed to Thread's constructor for constructing a Thread using the Runnable approach. approach #3: java.util.concurrency package - ExecutorServiceSince Java 1.5, there's additional way to spawn threads - java.util.concurrent package. Specifically, ExecutorService. Think of ExecutorService as a thread pool that provides methods to execute threads. Execute a Runnable If you have legacy or existing Runnable classes that you'd like them to run in threads, and you have no return values from the threads to process, you
import java.util.concurrent.*; [4] Previously submitted tasks are executed, but no new tasks will be accepted further. [5,6] Blocks until all tasks/threads have completed execution but wait no more than an hour. Submit a Callable Using Java Concurrency package approach, if you have no result to return back from the spawned threads, you 'execute a runnable' . If you do, then you want to 'submit a callable'. Each submission of Callable will return a Future. You then invoke get() on each Future to get the result object of returned from Callable's call() method. Callable interface is similar to runnable interface in that both are designed for classes whoses instances are to be executed by threads. The differences are:
import java.util.concurrent.*; [1] submitting a Callable<V> object to the ExecutorService returns a Future<V> object. This V is also what call() method returns. [2] call() returns a V that is introduced in the genericized form of the declared Callable<V> [3] get() method of Future will block until the task is completed or until timeout. It is the equivalent of both the join() call and the get value call as get() returns the value calculated by the Callable instance. what to read nextreferences
0 Comments
Leave a Reply. |
Categories
All
Archives
May 2020
|