java 分析线程_Java运行状态分析1:线程及线程状态

线程

线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。在Unix System V及SunOS中也被称为轻量进程(lightweight processes),但轻量进程更多指内核线程(kernel thread),而把用户线程(user thread)称为线程。

以上拷贝自维基百科

代码中任务、逻辑操作都依赖于线程,是java运行时最宝贵的资源

多线程一定程度可以增加cpu使用时间,压榨计算机资源提供更好的使用性能,一定程度也增加了资源的消耗如内存的增长、线程上下文数据切换的消耗、cup资源消耗,实际情况中我们应该根据业务场景合理的使用线程资源

Java线程生命周期

cf949519f523b2273f88dd50c59bae98.png ![image-20190712155311451](/Users/yugj/Library/Application Support/typora-user-images/image-20190712155311451.png)

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:

*

*

{@link Object#wait() Object.wait} with no timeout

*

{@link #join() Thread.join} with no timeout

*

{@link LockSupport#park() LockSupport.park}

*

*

*

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:

*

*

{@link #sleep Thread.sleep}

*

{@link Object#wait(long) Object.wait} with timeout

*

{@link #join(long) Thread.join} with timeout

*

{@link LockSupport#parkNanos LockSupport.parkNanos}

*

{@link LockSupport#parkUntil LockSupport.parkUntil}

*

*/

TIMED_WAITING,

/**

* Thread state for a terminated thread.

* The thread has completed execution.

*/

TERMINATED;

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 - " + 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结束变成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");

}

}

}

}

}

参考文献

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值