线程(英语:thread)是操做系统可以进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运做单位。一条线程指的是进程中一个单一顺序的控制流, 一个进程中能够并发多个线程,每条线程并行执行不一样的任务。在Unix System V及SunOS中也被称为轻量进程(lightweight processes), 但轻量进程更多指内核线程(kernel thread),而把用户线程(user thread)称为线程。
以上拷贝自维基百科java
代码中任务、逻辑操做都依赖于线程,是java运行时最宝贵的资源多线程
多线程必定程度能够增长cpu使用时间,压榨计算机资源提供更好的使用性能,必定程度也增长了资源的消耗如内存的增加、线程上下文数据切换的消耗、cup资源消耗,实际状况中咱们应该根据业务场景合理的使用线程资源并发
ide
java.lang.Thread.State 定义了以下6种线程状态spa
/** * 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;
1线程状态转换操作系统
public class DemonstrateThreadStates2 { static Thread thread1; public static void main(String[] args) { //建立线程1 thread1 = new Thread(new TestThread1()); // thread1 建立后 NEW state. System.out.println("State of thread1 after creating it - " + thread1.getState()); thread1.start(); // thread1 调用start后 变成 Runnable state System.out.println("State of thread1 after calling .start() method on it - " + thread1.getState()); } } class TestThread1 implements Runnable { @Override public void run() { TestThread2 myThread = new TestThread2(); Thread thread2 = new Thread(myThread); // 线程2建立 NEW state. System.out.println("State of thread2 after creating it - " + thread2.getState()); thread2.start(); // 线程2调用start 变成 Runnable state System.out.println("State of thread2 after calling .start() method on it - " + thread2.getState()); // 调用sleep迫使当前线程进入sleep thread2 = timed waiting state try { //moving thread2 to timed waiting state Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("State of thread2 after calling .sleep() method on it - " + thread2.getState()); try { // 调用join迫使线程结束到die thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("State of thread2 when it has finished it's execution - " + thread2.getState()); } } class TestThread2 implements Runnable { @Override public void run() { // moving thread2 to timed waiting state try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("State of thread1 while it called join() method on thread2 -" + DemonstrateThreadStates2.thread1.getState()); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } }
控制台输出:线程
State of thread1 after creating it - NEW State of thread1 after calling .start() method on it - RUNNABLE State of thread2 after creating it - NEW State of thread2 after calling .start() method on it - RUNNABLE State of thread2 after calling .sleep() method on it - TIMED_WAITING State of thread1 while it called join() method on thread2 -WAITING State of thread2 when it has finished it's execution - TERMINATED
线程建立线程变成NEW状态,调用start启动线程变成Runnable,调用sleep阻塞当前线程吧变成Timed Waiting,thread2调用join将等待结束当前线程到父线程thread1,thread2线程将变成die,父线程thread1 等待线程thread2结束变成waitingcode
2模拟blocked场景orm
经过死锁模拟blocked场景
死锁条件
互斥使用:一个资源只能分配给一个线程 不可剥夺:资源只能由占有者释放,申请者不能强制剥夺 请求保持:线程申请资源时,保持对原有资源的占有 循环等待:存在一个进程等待队列:{P1 , P2 , … , Pn}, 其中P1等待P2占有的资源,P2等待P3占有的资源,…,Pn等待P1占有的资源,造成一个进程等待环路 代码
public class TestDeadLock implements Runnable { // flag=1,占有对象o1,等待对象o2 // flag=0,占有对象o2,等待对象o1 public int flag = 1; // 定义两个Object对象,模拟两个线程占有的资源 public static Object o1 = new Object(); public static Object o2 = new Object(); public static void main(String[] args) { TestDeadLock deadLock1 = new TestDeadLock(); TestDeadLock deadLock2 = new TestDeadLock(); deadLock1.flag = 0; deadLock2.flag = 1; Thread thread1 = new Thread(deadLock1); Thread thread2 = new Thread(deadLock2); thread1.start(); thread2.start(); } @Override public void run() { System.out.println("flag: " + flag); // deadLock2占用资源o1,准备获取资源o2 if (flag == 1) { synchronized (o1) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2) { System.out.println("1"); } } } // deadLock1占用资源o2,准备获取资源o1 else if (flag == 0) { synchronized (o2) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o1) { System.out.println("0"); } } } } }
https://www.geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/