JUC概述
JUC概述1:
首先是进程和线程的概念:
进程:是指系统在系统中正在运行的一个应用程序,程序一旦运行就是进程,进程是资源分配的最小单位
线程:进程之内独立执行,是程序执行的最小单位
线程的六大状态:在线程的枚举类中
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 {@code Object.wait()}
* on an object is waiting for another thread to call
* {@code Object.notify()} or {@code Object.notifyAll()} on
* that object. A thread that has called {@code Thread.join()}
* 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;
}
状态名称 | 说明 |
---|---|
new | 初始状态 |
runnable | 运行状态 |
blocked | 阻塞状态 |
waiting | 等待状态,一直等(不见不散) |
time_waiting | 超时等待,(过时不候) |
terminated | 终止状态 |
wait和sleep的区别:
- sleep是Thread的静态方法,wait是Object的方法,任何对象实例化都能调用
- sleep不会释放锁,他也不需要占用锁,wait会释放锁,但是调用它的前提是当前线程占有锁
- 它们都可以interrupted被中断
并发和并行:
并发是指多个事情在同一个时间段中执行
并行是指多个事情在同一时刻执行
管程:
是一种同步机制,保证同一时间内只有一个线程访问被保护数据或者代码
jvm同步基于进入(加锁)和退出(解锁),是管程对象实现的
大意就是进加锁,退是解锁,通过管程对象管理
用户线程:自定义线程 主线程结束了,用户线程还存在,则表示JVM还存在
public class demo {
public static void main(String[] args) {
Thread a = new Thread(() -> {
System.out.println(Thread.currentThread().getName()+"::"+Thread.currentThread().isDaemon());
while (true){}
}, "a");
a.start();
System.out.println(Thread.currentThread().getName());
}
}
守护线程:
ex:垃圾回收 没有用户线程了,都是守护线程,JVM结束
public class demo {
public static void main(String[] args) {
Thread a = new Thread(() -> {
System.out.println(Thread.currentThread().getName()+"::"+Thread.currentThread().isDaemon());
while (true){}
}, "a");
//设置子线程为守护线程
a.setDaemon(true);
a.start();
System.out.println(Thread.currentThread().getName());
}
}