1.什么是进程?
总结一句话就是系统中正在运行的程序;应用程序一旦运行就是进程;进程——资源分配的最小单位。比如说你现在打开哔哩哔哩,哔哩哔哩就是运行的程序,即进程。里面的各种操作比如看视频之类的就是进程中的一个线程。一个进程里面可以有一堆线程。
2.线程?
2.1线程的概念
系统分配处理器时间资源的基本单元,或者说进程之内独立执行的一个单元执行流。线程——程序执行的最小单位。
2.2线程的状态
这是线程类中的状态枚举类,枚举了线程中的各种状态
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;
}
NEW(新建),RUNNABLE(准备就绪),BLOCKED(阻塞),WAITING(不见不散),TIMED_WAITING(过时不候),这两个的区别就是一种情况下你是舔狗,跟女生出来见面到点儿了她不来,你就一直在那边等着。另一种情况就是你是直男,到点儿了不来直接走,比较硬气,不过我估计你这对象也就黄了。TERMINATED(终结)。
2.3用户线程和守护线程
平时用到的自定义的基本都是用户线程,后台中一种特殊的线程比如说垃圾回收。
public class Test {
public static void main(String[] args) {
new Thread(()->{
System.out.println(Thread.currentThread().getName() + ":: " + Thread.currentThread().isDaemon());
while(true){
}
},"aa").start();
System.out.println(Thread.currentThread().getName() + " is over");
}
}
这段代码就是对用户线程的测试。isDemon()为true说明是守护线程,false说明是用户线程我们运行一下。
可以看到他还没完全结束,这时候main线程结束了,返回值为false说明是用户线程,主线程虽然结束但用户线程没结束,jvm不退出,还在运行。
public class Test {
public static void main(String[] args) {
Thread aa = new Thread(() -> {
System.out.println(Thread.currentThread().getName() + ":: " + Thread.currentThread().isDaemon());
while (true) {
}
}, "aa");
aa.setDaemon(true);
aa.start();
System.out.println(Thread.currentThread().getName() + " is over");
}
}
现在我把这个用户线程改成守护线程,这样主线程结束后就没有任何用户线程了。所以jvm会退出。
2.4wait/sleep的区别
sleep是Thread的静态方法,wait是Object的方法谁都能用,sleep不会释放锁,它也不需要占用锁。wait需要释放锁所以他必须在synchronized中。他们都可以被interrupted方法中断。
3.管程
管程就是一中同步机制,保证同一个时间,只有一个线程访问被保护的数据或者代码。我们执行线程操作首先要持有管程对象,方法执行结束之后会释放管程对象。