- Java47
- 数据库21
- 微服务21
- 多线程20
- Spring 系列19
- 设计模式18
- 消息中间件15
- Server14
- container 容器12
- 计算机网络8
- Go3
- Machine Learning3
- 单片机2
- Python2
- 前端2
- JUC2
- 数据结构2
- 应用1
- 数学1
- Git1
- 搜索引擎1
- 操作系统1
Future 接口
Future 接口定义了操作异步任务执行的一些方法,如获取异步任务的执行结果、取消任务的执行、判断任务是否被取消、判断任务执行是否完毕等等。
public interface Future<V> {
/**
* Attempts to cancel execution of this task. This attempt will
* fail if the task has already completed, has already been cancelled,
* or could not be cancelled for some other reason. If successful,
* and this task has not started when {@code cancel} is called,
* this task should never run. If the task has already started,
* then the {@code mayInterruptIfRunning} parameter determines
* whether the thread executing this task should be interrupted in
* an attempt to stop the task.
*
* <p>After this method returns, subsequent calls to {@link #isDone} will
* always return {@code true}. Subsequent calls to {@link #isCancelled}
* will always return {@code true} if this method returned {@code true}.
*
* @param mayInterruptIfRunning {@code true} if the thread executing this
* task should be interrupted; otherwise, in-progress tasks are allowed
* to complete
* @return {@code false} if the task could not be cancelled,
* typically because it has already completed normally;
* {@code true} otherwise
*/
boolean cancel(boolean mayInterruptIfRunning);
/**
* Returns {@code true} if this task was cancelled before it completed
* normally.
*
* @return {@code true} if this task was cancelled before it completed
*/
boolean isCancelled();
/**
* Returns {@code true} if this task completed.
*
* Completion may be due to normal termination, an exception, or
* cancellation -- in all of these cases, this method will return
* {@code true}.
*
* @return {@code true} if this task completed
*/
boolean isDone();
/**
* Waits if necessary for the computation to complete, and then
* retrieves its result.
*
* @return the computed result
* @throws CancellationException if the computation was cancelled
* @throws ExecutionException if the computation threw an
* exception
* @throws InterruptedException if the current thread was interrupted
* while waiting
*/
V get() throws InterruptedException, ExecutionException;
/**
* Waits if necessary for at most the given time for the computation
* to complete, and then retrieves its result, if available.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return the computed result
* @throws CancellationException if the computation was cancelled
* @throws ExecutionException if the computation threw an
* exception
* @throws InterruptedException if the current thread was interrupted
* while waiting
* @throws TimeoutException if the wait timed out
*/
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
用户线程
一般情况下,我们启动一个线程,默认就是用户线程。它是系统的工作线程,程序的业务操作都由它来完成。
守护线程
守护线程是服务与其它线程的线程,例如垃圾回收线程就是一个守护线程。
守护线程作为一个服务线程,是不能独立存在的,必须要一个服务对象。还是用垃圾回收线程来说明,当 Java 程序在运行的过程中,必然有一个主线程也就是 main 方法执行的线程,当主线程还在执行的过程中,垃圾回收线程会一直运行;当程序运行结束,主线程结束,那么垃圾回收线程也会相应的退出。
源码说明
在 Java 中,在不特殊设置下,启动一个线程默认都是用户线程,我们可以通过配置 Thread 中的一个属性来决定即将启动的线程是用户线程还是守护线程。
Path
Path 介绍
Java Path 接口是 Java NIO 更新的一部分,同 Java NIO 一起已经包括在 Java 6 和 Java 7 中。Java Path 接口是在 Java 7 中添加到 Java NIO 的。
Java Path 实例表示文件系统中的路径。一个路径可以指向一个文件或一个目录。路径可以是绝对路径,也可以是相对路径。绝对路径包含从文件系统的根目录到它指向的文件或目录的完整路径。相对路径包含相对于其它路径的文件或目录的路径。
在许多方面,java.nio.file.Path 接口类似于 java.io.File 类,但是有一些差别。不过,在许多情况下,可以使用 Path 接口来替换 File 类的使用。
Pipe 管道
Java NIO 管道是两个线程之间的单向数据连接。Pipe 有一个 source 通道和一个 sink 通道。数据会被写到 sink 通道,从 source 通道读取。
创建管道
通过 Pipe.open() 方法打开管道。
Pipe pipe = Pipe.open();