Use of Future, RunnableFuture, Callable, FutureTask

1 Future interface It is mainly used to define the result of accepting asynchronous execution. We will mainly look at its implementation class below. See 5 public interface Future<V> {<!– –> boolean cancel(boolean mayInterruptIfRunning); boolean isCancelled(); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; } 2 RunnableFuture […]

Using Java to make clever use of thread applications (2) – to implement the Runnable interface

Directory Implement the Runnable interface 1. Operation steps 2. Code example 3. Why doesn’t mr call start(), but mr is passed as a parameter into the instance of the Thread class? 4. Which scenarios are the two methods applicable to? 5. Create and start threads using anonymous inner classes Some advantages of anonymous inner classes: […]

When to use Runnable? When to use Callable ?

When it comes to Java, you have to talk about multithreading. Even if you don’t want to talk about it, the interviewer has to ask you to talk about it, right? When it comes to multithreading, you don’t have to mention threads (isn’t this nonsense). When it comes to threads, you have to talk about […]

There are several ways to obtain multithreading, the difference between callable interface and runnable interface, and the use of callable interface

1. How many ways to obtain multi-threading? (1) Traditionally, the thread class is inherited and the runnable interface is implemented. After java5, the callable interface and java thread pool are obtained. (2) Functional interface: This is a functional interface, so it can be used as an assignment object for lambda expressions or method references. 2. […]

Java ~ Executor ~ RunnableFuture [source code]

Foreword Articles Related series: “Java ~ Executor [Catalogue]” (continuously updated) Related series: “Java ~ Executor ~ RunnableFuture【Source Code】” (learning process/more Omissions / for reference only / no longer updated) Related series: “Java ~ Executor ~ RunnableFuture【Summary】” (learning summary/latest and most standard/continuously updated) Related series: “Java ~ Executor ~ RunnableFuture【Questions】” (Learning Answers/Continuous Updates ) References: “The […]

Relationship among Thread, Runnable, Callable, Future, FutureTask

Thread and Runnable Thread public class MyThread extends Thread {<!– –> public MyThread(String name) {<!– –> super(name); } @Override public void run() {<!– –> String name = Thread. currentThread(). getName(); System.out.println(name + “start running”); } public static void main(String[] args) {<!– –> new MyThread(“testThread”).start(); } } A thread consists of 4 states: Created -> Ready […]

[Multi-threading] (1) (Process and thread Multi-thread programming Create thread Implement Runnable Anonymous inner class inherits Thread, Runable Lambda expression Construction method and properties Interrupt thread Wait, get, sleep thread )

Article directory thread process and thread Multithreaded programming in Java Inherit Thread, rewrite run Multi-threading reflects the effect of concurrent execution The difference between start and run create thread Implement the Runnable interface Use anonymous inner class to inherit Thread Implement Runable using an anonymous inner class Using Lambda expressions Thread class and common methods […]

Thread creation (discrimination of Runnable, Future, CompletionService, CompletableFuture)

Table of Contents Use Thread directly Use Runnable Using Callable with FutureTask Use the thread pool to perform tasks Completion Service ListenableFuture Completable Future References Use Thread directly Directly let a class inherit the Thread class, override the run method, and call the start method directly when calling externally. Because of java’s single inheritance model, […]

Multi-threaded Runnable, Callable, Thread

Create a thread Java provides three methods of creating threads: By implementing the Runnable interface; By inheriting from the Thread class itself; Create threads through Callable and Future. Create threads by implementing the Runnable interface The easiest way to create a thread is to create a class that implements the Runnable interface. In order to […]