The run() method will execute as if executed by a different CPU.
Java Concurrency In Practice Github Code Inside YourDmytro Timchenko Follow Aug 21 4 min read A Java Thread is like a virtual CPU that can execute your Java code inside your Java application.From inside your application, you can create and start more threads that can execute parts of your application code in parallel with the main thread.Threads are instances of class java.lang.Thread or instances of subclasses of this class. In addition to being objects, java threads can also execute code. How to Create a Java Thread Java lets you create a thread one of three ways: By implementing the Runnable interface. Lets have a look at how these ways help in implementing the Java thread. Runnable Interface The easiest way to create a thread is to create a class that implements the Runnable interface. A Java object that implements the Runnable interface can be executed by a Java Thread. The Runnable interface is a standard Java Interface that comes with the Java platform. Here is an example of implementing the Runnable interface: The above MyRunnable is just a task which we want to run in a separate thread. Run as a Lambda Starting with Java 8, any interface which exposes a single abstract method is treated as a functional interface, which makes it a valid lambda expression target. We can rewrite the above Runnable code using a lambda expression: Callable Interface Callable is an improved version of Runnable that was added in Java 1.5. Both interfaces are designed to represent a task that can be executed by multiple threads. Runnable tasks can be run using the Thread class or ExecutorService whereas Callables can be run only using the latter. The Callable interface is a generic interface containing a single call() method which returns a generic value: The call() method is called in order to execute the asynchronous task. If the task is executed asynchronously, the result is typically propagated back to the creator of the task via a Java Future. This is the case when a Callable is submitted to an ExecutorService for concurrent execution. We can run our task with ExecutorService: Quite often, IO operations like reading from or writing to disk or network, are good candidates for tasks that can be executed concurrently. IO operations often have long waiting times in between reading and writing blocks of data. By executing such tasks in a separate thread, you avoid blocking your main application thread unnecessarily. Thread Subclass The other way is to create a subclass of Thread and override the run() method. The run() method is what is executed by the thread after you call start(). Here is an example of creating a Java Thread subclass: Also, note that MyThread cannot extend any other class, as Java doesnt support multiple inheritance. One of them is by calling method start(): The start() call will return as soon as the thread is started.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |