1.1 什么是JUC
在Java中,线程部分是一个重点,本篇文章说的JUC 也是关于线程的。JUC就是java.util .concurrent工具包的简称。这是一个处理线程的工具包,JDK1.5开始出现的。·
1.2 线程和进程的概念
进程和线程
进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。在早期面向进程设计的计算机结构中,进程是程序的基本执行实体;在当代面向线程设计的计算机结构中,进程是线程的容器。程序是指令、数据及其组织形式的描述,进程是程序的实体。
线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。
总结来说:
进程:指在系统中正在运行的一个应用程序;程序一旦运行就是进程;进程是资源分配的最小单位
线程:系统分配处理器时间资源的基本单元,或者说进程之内独立执行的一个单元执行流。线程时程序执行的最小单位
线程的状态
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 区别
1. sleep是Thread的静态方法;wait是Object的方法,任何对象实例都能调用。
2. sleep不会释放锁,它也不需要占用锁;wait会释放锁,但调用它的前提是当前线程占有锁(即代码要在synchronized中)
3. 它们都可以被interrupt方法中断
串行、并行和并发
串行:多个任务按顺序一个一个的执行。
并行:指的是在同一时刻,任务可以同时开始进行,彼此之间没有依赖关系。
典型案例:烧水泡方便面
并发:同一时刻多个线程在访问同一个资源,多个线程争对一个点。
典型案例:电商秒杀
管程
管程:对于cpu来说就是一个监视器(Monitor),对于java来说就是我们常用的锁,管程它是一种同步机制,保证同一个时间,只有一个线程访问被保护数据或者代码。JVM的同步是基于进入和退出,使用管程对象来实现,进入的时候加锁,退出就解锁。
用户线程和守护线程
用户线程
我们平时创建的线程都是用户线程。主线程结束了,用户线程还在运行,jvm存活。
public class Test {public static void main(String[] args) {Thread thread = new Thread(new Runnable() {public void run() {System.out.println("线程名称:"+Thread.currentThread().getName() + " 是否为守护线程:" + Thread.currentThread().isDaemon());while (true){}}},"aa");thread.start();System.out.println("线程名称:"+Thread.currentThread().getName() + " 是否为守护线程:" + Thread.currentThread().isDaemon());} }
守护线程
比如垃圾回收。如果没有了用户线程,都是守护线程,jvm结束
public class Test {public static void main(String[] args) {Thread thread = new Thread(new Runnable() {public void run() {System.out.println("线程名称:"+Thread.currentThread().getName() + " 是否为守护线程:" + Thread.currentThread().isDaemon());while (true){}}},"aa");//设置线程为守护线程thread.setDaemon(true);thread.start();System.out.println("线程名称:"+Thread.currentThread().getName() + " 是否为守护线程:" + Thread.currentThread().isDaemon());} }
总结
守护线程都是为 JVM 中所有用户线程的运行提供便利服务: 只要当前 JVM 实例中尚存在任何一个用户线程没有结束,守护线程就全部工作;只有当最后一个用户线程结束时,守护线程随着 JVM 一同结束工作。
说白了:守护线程就是来守护用户线程的,如果用户线程都没有了,那守护线程也就没有存在的意义了。