Java线程状态以及在JVM中的实现原理
2018-09-21 19:10:08 0 举报
Java线程状态切换
作者其他创作
大纲/内容
park实现
等待队列release lockor monitor
native isInterrupted(false)
获取时间片obtain my timeslice
LockSupport.park()
阻塞状态dosen't releaseany lock or monitor
cpu
ParkEvent * const slp = thread-_SleepEvent ;slp-park(millis);
setPriority()实现
KLT
start
死亡dead
LWP
OSThread* osthread = thread-osthread(); bool interrupted = osthread-interrupted(); if (interrupted && clear_interrupted) { osthread-set_interrupted(false); }
RUNNING运行中
P:进程
synchronized
(re-)entering a synchronization block
把线程放进双向循环等待链表
wait()
运行状态Running
new Thread()
新建new Thread
Thread
锁池lock pool
IN_OBJECT_WAIT
BLOCKED_ON_MONITOR_ENTER
isInterrupted()实现
SLEEPING
void os::yield()
LockSupport.park(long)
Wait Set非线程状态
void Thread::interrupt(Thread* thread)
READY就绪
notifyAll把所有线程放进去
1.Object.wait(long)2.Thread.join(kibg)3.LockSupport.parkNanos(long)4.LockSupport.parkUntil(long)5.Thread.sleep(long)
Parker* p = NULL;p-unpark();
系统调度获取到时间片
Java Thread Priority
Windows Thread Priority
Linux Thread Priority
0
THREAD_PRIORITY_IDLE
19
1
THREAD_PRIORITY_LOWEST
-4
2
-3
3
THREAD_PRIORITY_BELOW_NORMAL
-2
4
-1
5
THREAD_PRIORITY_NORMAL
6
7
THREAD_PRIORITY_ABOVE_NORMAL
8
9
THREAD_PRIORITY_HIGHEST
10
11
public static boolean interrupted()
可运行状态Runnable
Thread.start()
可运行Runable
1.run()执行结束2.main()执行结束3.抛出异常
public boolean isInterrupted()
等待队列Waiting
o.wait()
被其他线程唤醒notify()或者notifyall()
用户输入结束sleep结束join的线程结束
WAITING等待
TERMINATED结束
RUNNABLE包括就绪和运行中运行中
interrupt()实现
sched_yield();
NEW新建
1.用户输入完成2.sleep时间结束3.join线程结束
BLOCKED堵塞
void Parker::unpark()
1.Thread.yield()2.时间片用完
KLT:内核线程Kernel Thread
拿到对象的锁标记
waitmonitorexit
o.notify()o.notifyall()wait时间到
1.等待用户输入2.Thread.sleep()3.join()
interrupted()实现
RUNNABLE
会释放锁
LWP:轻量级进程(进程中线程)
void os::interrupt(Thread* thread)
初始化状态NEW
1.Object.notify()2.Object.notifyAll()3.LockSupport.unpark(Thread)4.超时时间到
表示jvm层的ThreadStatus
Object.wait(long)
time !=0
pthread_mutex_unlock(_mutex);
到时间
1.Object.notify()2.Object.notifyAll()3.LockSupport.unpark(Thread)
os::yield();
CPU
P
OS调度
public void interrupt()
运行中Runing
时间片用完Thread.yield()
sleep实现
结束Terminated
获取到锁
拿到锁标识
等待用户输入Thread.sleept2.join
unpark()实现
锁池的状态
EntryList非线程状态
IN_OBJECT_WAIT_TIMED
native currentThread().isInterrupted(true)
yield()实现
PARKED
1.Object.wait()2.Thread.join()3.LockSupport.park()
1.时间片用完2.Thread.yield()
TERMINATED
Thread.sleep()
1.等待进入synchronized代码块2.等待进入synchronized方法
unpark到时间
osthread-set_interrupted(true);ParkEvent * const slp = thread-_SleepEvent ; if (slp != NULL) slp-unpark() ;((JavaThread*)thread)-parker()-unpark();ParkEvent * ev = thread-_ParkEvent ; if (ev != NULL) ev-unpark() ;
不会释放锁
notify把第一个线程放进等待锁的链表
PARKED_TIMED
os::interrupt(thread);
native interrupt0();
JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));Thread::interrupt(thr);
void os::PlatformEvent::park()
Thread Scheduler
TIMED_WAITING超时等待
Object.wait()
NEW
unpark
阻塞状态Blocked
run()结束main()方法结束
收藏
收藏
0 条评论
下一页