1、JUC介绍
JUC (java.util.concurrent)是在并发编程中使用的工具类,主要包括以下三个
(1)java.util.concurrent
(2)java.util.concurrent.atomic
原子性:不可分割。Int i=0; i++,
(3)java.util.concurrent.locks
2、进程/线程
(1)进程/线程是什么?
进程:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动。它是操作系统动态执行的基本单元,在传统的操作系统中,进程既是基本的分配单元,也是基本的执行单元。
线程:通常在一个进程中可以包含若干个线程,当然一个进程中至少有一个线程,不然没有存在的意义。线程可以利用进程所拥有的资源,在引入线程的操作系统中,通常都是把进程作为分配资源的基本单位,而把线程作为独立运行和独立调度的基本单位,由于线程比进程更小,基本上不拥有系统资源,故对它的调度所付出的开销就会小得多,能更高效的提高系统多个程序间并发执行的程度。
(2)进程/线程例子?
1)使用QQ,查看进程一定有一个QQ.exe的进程,我可以用qq和A文字聊天,和B视频聊天,给C传文件,给D发一段语言,QQ支持录入信息的搜索,多个线程。
2)写需求文档,用word写文档,同时用网易云音乐放歌,同时用QQ聊天,多个进程。
3)word如没有保存,停电关机,再通电后打开word可以恢复之前未保存的文档,word也会检查你的拼写,两个线程:容灾备份,语法检查,多个线程。
(3)线程状态
1)NEW,(新建)
2)RUNNABLE,(准备就绪)
3)BLOCKED,(阻塞)
4)WAITING,(不到一直等,不见不散)
5)TIMED_WAITING,(等到约定时间就放弃,过时不候)
6)TERMINATED;(终结)
源码解析:
T
hread.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;(终结)
}
(4)wait/sleep的区别?
1)相同点:
两者的功能都是使当前线程暂停,在哪儿睡在哪儿醒过来。
2)区别:
Wait:将锁释放,自己睡眠。
sleep:加锁睡眠,醒了手里还有锁。
(5)什么是并发?什么是并行?
1)并发:同一时刻多个线程在访问同一个资源,多个线程对一个点
例子:1.小米9今天上午10点,限量抢购。
2.春运抢票。
3.电商秒杀活动。
2)并行:多项工作一起执行,之后再汇总
例子:1.泡方便面,电水壶烧水,一边撕调料倒入桶中…
2.做饭,一边煮粥,一边洗菜,一边烧水…
3.跑步的时候听歌…