十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇内容主要讲解“Java中线程及线程状态是什么意思”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java中线程及线程状态是什么意思”吧!
双辽ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流, 一个进程中可以并发多个线程,每条线程并行执行不同的任务。在Unix System V及SunOS中也被称为轻量进程(lightweight processes), 但轻量进程更多指内核线程(kernel thread),而把用户线程(user thread)称为线程。
以上拷贝自维基百科
代码中任务、逻辑操作都依赖于线程,是java运行时最宝贵的资源
多线程一定程度可以增加cpu使用时间,压榨计算机资源提供更好的使用性能,一定程度也增加了资源的消耗如内存的增长、线程上下文数据切换的消耗、cup资源消耗,实际情况中我们应该根据业务场景合理的使用线程资源
![image-20190712155311451](/Users/yugj/Library/Application Support/typora-user-images/image-20190712155311451.png)
https://www.geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/contribute.geeksforgeeks.org/wp-content/uploads/threadLifeCycle.jpg
java.lang.Thread.State 定义了如下6种线程状态
/** * 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: *
A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called Object.wait() * on an object is waiting for another thread to call * Object.notify() or Object.notifyAll() on * that object. A thread that has called 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: *
New:刚创建,可被执行,并且未开始执行
Runnable:正在执行或随时准备执行,例如多线程程序分配特定时间片给特定线程,特定线程执行短暂时间并暂停放弃cpu时间给其他线程,其他线程因此可以执行,这种场景线程是准备执行等待CPU时间,这种状态即Runnable
Blocked:waiting for a monitor lock,处于需要获取其他线程锁定的同步资源,如等待io结束,这种状态在转变为Runnable之前无法执行,无法消耗cup时间片
Waiting:等待其他线程执行特定操作,和Blocked类似
Timed Waiting:线程调用等待执行场景,特定时间后执行,比较sleep,或者一些条件等待场景,如定时任务
Terminated:正常或异常结束线程,将不分配CPU时间
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 - ">
控制台输出:
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结束变成waiting
2模拟blocked场景
通过死锁模拟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/
https://www.jianshu.com/p/8cf78bf94f9d
到此,相信大家对“Java中线程及线程状态是什么意思”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!