JUC是什么
java.util.concurrent在并发编程中使用的工具类
进程/线程
进程
进程是一个具有一定独立功能的程序关于某个数据集合的一次运动活动。它是操作系统动态执行的基本单元,在统的操作系统中,进程即是基本的分配单元,也是基本的执行单元。
线程
通常在一个进程中可以包含若干个线程,当然一个进程中至少可以利用进程所拥有的资源,在引入线程的操作系统中,通常都是把进程作为资源分配的基本单位,而把线程作为独立运行和独立调度的基本单位,由于线程比进程更小,基本上不拥有系统资源,故对它的调度所付出的开销就会小得多,能更高效的提高系统多个程序间并发执行的程度。
例子
使用QQ,查看进程一定有一个QQ.exe的进程,可以用qq和A文字聊天、和B视频聊天,个C传文件,给D发一段语音,QQ支持录入信息的搜索。
word如没有保存,停电关机,再通电后打开word可以恢复之前保存的文档,word也会检查你的拼写。两个线程:容灾备份、语法检查。
创建线程的方式
传统的是继承thread类和runnable接口,java5之后实现callable结构和java的线程池获得。
实现Callable接口
/**
* @author phd
* @version 1.0
* @date 2020/9/2
*/
class MyThread implements Callable<Integer>{
@Override
public Integer call() throws Exception {
TimeUnit.SECONDS.sleep(4);
System.out.println("class MyThread implements Callable<Integer>");
return 1234;
}
}
public class CallableDemo {
public static void main(String args[]){
// FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread());
// futureTask.run();
FutureTask futureTask = new FutureTask(new MyThread());
new Thread(futureTask,"A").start();
try {
System.out.println(futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
两者的区别:
- 是否有返回值
- 是否抛异常
- 落地方法不一样,一个是run,一个是call
FutureTask
- 未来任务,用它就干一件事,异步调用。
- 在主线程中需要执行比较耗时的操作时,但又不想阻塞主线程,可以把这些作业交给FutureTask对象在后台完成,当主线程将来需要时,就可以通过Future对象获得后台作业的计算结果或者执行状态。
- 一般FutureTask多用于耗时的计算,主线程可以在完成自己的任务后,再去获取结果。
- 仅在计算完成时才能检索结果;如果计算尚未完成,则阻塞get方法。一旦计算完成,就不能重新开始或取消计算。
- 而get方法结果只有在计算完成时获取,否则会一直阻塞直到任务转入完成状态。然后会返回结果或者抛出异常。
线程的状态
通过 Thread.State 可以查看线程的状态
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,(新建)
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,(准备就绪)
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,(阻塞)
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,(不见不散)
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,(过时不候)
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;(终结)
}
wait/sleep的区别
功能都是暂停当前线程,区别在于
- wait放开锁去睡,放开手中的锁。
- sleep握紧锁去睡,醒了手里还有锁。
并发和并行
并发,指的是多个事情,在同一时间段内同时发生了。
并行,指的是多个事情,在同一时间点上同时发生了。