preface
Multithreading and concurrency are really yearning for a long time, there is always a mysterious feeling, want to explore a wave, but also worried that the level is not enough to control. Want to write in the way of reading notes, but feel that some of their own thinking; But you can’t write deep stuff without having enough concurrent programming experience. So while reading spring source code, I also want to spare some time to have a look at JUC. I can only record a process of learning JUC and try to use some specific code demo to deepen my understanding. So I wrote this series as “[First Acquaintance] -juc ·XXXX” to open the door to concurrent programming.
JUC
JUC namely Java. Util. Concurrent; That is, Java provides and sends packages. From the perspective of package structure, JUC mainly includes:
-
java.util.concurrent
Underneath this package are thread pools, concurrent collections, and some concurrent utility classes. Thread pool correlation is built around the Excetor framework; This is the focus of the rest of this article.
-
java.util.concurrent.atomic
Below this package are some atomic manipulation classes, which are concurrent helper classes. The basic implementation depends on CAS;
-
java.util.concurrent.locks
This, as the name suggests, is used to provide locks.
Classes for JUC modules
- The overall framework
- atomic
- locks
- Concurrent collections
- Concurrent tool
-
forkJoin
Fork-join has the following three classes in JUC:
public class ForkJoinPool extends AbstractExecutorService Copy the code
public abstract class ForkJoinTask<V> implements Future<V>, Serializable Copy the code
public class ForkJoinWorkerThread extends Thread Copy the code
Future
The Future provides a method for retrieving asynchronous execution results, unlike Runnable’s run method, which does not return results.
public interface Future<V> {
/ / cancel
boolean cancel(boolean mayInterruptIfRunning);
// If the task is cancelled before completion, true is returned.
boolean isCancelled(a);
// Return true if the task is completed, whether it is completed normally, cancelled, or if an exception occurs.
boolean isDone(a);
// Gets the result of the asynchronous execution. If no result is available, this method blocks until the asynchronous calculation is complete.
V get(a) throws InterruptedException, ExecutionException;
If no result is available, this method blocks, but with a time limit,
// This method throws an exception if the blocking time exceeds the specified timeout.
V get(long timeout, TimeUnit unit) throws InterruptedException,
ExecutionException, TimeoutException;
}
Copy the code
Callable
Call () declares a method called Call () that can return V or throw an exception
public interface Callable<V> {
V call(a) throws Exception;
}
Copy the code
The use of Callable and Future is generally used in conjunction with our thread pool.
Executor
The Executor interface is the top-level interface for thread pool implementations. It serves a similar role as spring’s BeanFactory, which provides top-level functional constraints that are left to different subclasses.
public interface Executor {
/**
* Executes the given command at some time in the future. The command
* may execute in a new thread, in a pooled thread, or in the calling
* thread, at the discretion of the <tt>Executor</tt> implementation.
*
* @param command the runnable task
* @throws RejectedExecutionException if this task cannot be
* accepted for execution.
* @throws NullPointerException if command is null
*/
void execute(Runnable command);
}
Copy the code
Here is the overall structure of the Executor framework in JUC:
ExecutorService
public interface ExecutorService extends Executor {
// Close the thread pool
void shutdown(a);
List<Runnable> shutdownNow(a);
// Whether the state is Shutdown
boolean isShutdown(a);
// Whether the state is Terminated
boolean isTerminated(a);
// If the timeout period exceeds, it monitors whether the ExecutorService is shut down
// Returns true if closed, false otherwise.
// The shutdown method is used in combination with the shutdown method.
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
// Return a Future object that receives an implementation of a Callable
The call() method in the Callable interface has a return value that returns the result of the execution of the task
// Differs from the run() method in the Runnable interface (void modifier, no return value).
<T> Future<T> submit(Callable<T> task);
<T> Future<T> submit(Runnable task, T result);
// Return a Future object, with which we can check whether the submitted task has been completed.Future<? > submit(Runnable task);// Return a List of Future objects for each Callable task.
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException;
// Add timeout control
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException;
// Receive parameters are a collection of Callable,
// Returns the result of one of the Callable collection tasks
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException;
// Add timeout control
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
Copy the code
The ExecutorService reexecutor interface provides extended control over thread pool state and timeout control over submitted task execution. The basic functionality of the thread pool is not perfect enough to really have the ability to handle specific services (after all, it is an interface).
Open a chapter, slowly learn ~