本文主要是介绍openjdk11 jvm视角查看java线程异常退出synchronized锁自动释放,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
##synchronized锁异常 java代码demo
public class ThreadTest {public static void main(String[] args) {ThreadTest threadTest = new ThreadTest();Thread yym_user_thread1 = new Thread() {public void run() {System.out.println("yym_user_thread1");threadTest.resources(0);}};yym_user_thread1.setName("yym_user_thread1");yym_user_thread1.start();////Thread.yield();Thread.yield();Thread.yield();Thread.yield();Thread.yield();Thread.yield();Thread yym_user_thread2 = new Thread() {public void run() {System.out.println("yym_user_thread2");threadTest.resources(2);}};yym_user_thread2.setName("yym_user_thread2");yym_user_thread2.start();}public synchronized void resources(int number) {int i = 10/number;System.out.println("resources"+i);}}
##gdb线程栈
(gdb) c
Continuing.
@@@@-yym-Thread PID: 4160
@@@@-yym-Thread NAME: yym_user_thread2
yym_user_thread2
[Switching to Thread 0x7fffc9d6c700 (LWP 4157)]Thread 18 "C2 CompilerThre" hit Breakpoint 4, JavaThread::exit (this=0x7fffa4220800, destroy_vm=false, exit_type=JavaThread::normal_exit)at /home/yym/code/jdk11u-master/src/hotspot/share/runtime/thread.cpp:1917
1917 this->clear_pending_exception();
(gdb) c
Continuing.
[Switching to Thread 0x7fffc9b6a700 (LWP 4159)]Thread 20 "yym_user_thread" hit Breakpoint 4, JavaThread::exit (this=0x7ffff07bd800, destroy_vm=false, exit_type=JavaThread::normal_exit)at /home/yym/code/jdk11u-master/src/hotspot/share/runtime/thread.cpp:1917
1917 this->clear_pending_exception();
(gdb) s
ThreadShadow::clear_pending_exception (this=0x7ffff07bd800) at /home/yym/code/jdk11u-master/src/hotspot/share/utilities/exceptions.cpp:59
59 void ThreadShadow::clear_pending_exception() {
(gdb) list
54 _pending_exception = exception;
55 _exception_file = file;
56 _exception_line = line;
57 }
58
59 void ThreadShadow::clear_pending_exception() {
60 LogTarget(Debug, exceptions) lt;
61 if (_pending_exception != NULL && lt.is_enabled()) {
62 ResourceMark rm;
63 LogStream ls(lt);
(gdb) n
60 LogTarget(Debug, exceptions) lt;
(gdb) p exception
No symbol "exception" in current context.
(gdb) n
61 if (_pending_exception != NULL && lt.is_enabled()) {
(gdb) b thread.cpp:1917(gdb) p *_pending_exception
$8 = {_mark = 0x7ffff07bd805, _metadata = {_klass = 0xe294f72300014610, _compressed_klass = 83472}}
(gdb) call _pending_exception->print_on(tty)
java.lang.ArithmeticException
{0x0000000714a7b8f0} - klass: 'java/lang/ArithmeticException'- ---- fields (total size 5 words):- private transient 'backtrace' 'Ljava/lang/Object;' @12 a 'java/lang/Object'[5] {0x0000000714a7b918} (e294f723)- private 'detailMessage' 'Ljava/lang/String;' @16 "/ by zero"{0x0000000714a7b8b8} (e294f717)- private 'cause' 'Ljava/lang/Throwable;' @20 a 'java/lang/ArithmeticException'{0x0000000714a7b8f0} (e294f71e)- private 'stackTrace' '[Ljava/lang/StackTraceElement;' @24 a 'java/lang/StackTraceElement'[0] {0x0000000715120a70} (e2a2414e)- private strict 'suppressedExceptions' 'Ljava/util/List;' @28 a 'java/util/Collections$EmptyList'{0x000000071510bb90} (e2a21772)- private transient 'depth' 'I' @32 2
(gdb) bt
#0 ThreadShadow::clear_pending_exception (this=0x7ffff07bd800) at /home/yym/code/jdk11u-master/src/hotspot/share/utilities/exceptions.cpp:61
#1 0x00007ffff6173b7c in JavaThread::exit (this=0x7ffff07bd800, destroy_vm=false, exit_type=JavaThread::normal_exit)at /home/yym/code/jdk11u-master/src/hotspot/share/runtime/thread.cpp:1917
#2 0x00007ffff61738ef in JavaThread::thread_main_inner (this=0x7ffff07bd800) at /home/yym/code/jdk11u-master/src/hotspot/share/runtime/thread.cpp:1875
#3 0x00007ffff617374a in JavaThread::run (this=0x7ffff07bd800) at /home/yym/code/jdk11u-master/src/hotspot/share/runtime/thread.cpp:1850
#4 0x00007ffff616f8eb in Thread::call_run (this=0x7ffff07bd800) at /home/yym/code/jdk11u-master/src/hotspot/share/runtime/thread.cpp:387
#5 0x00007ffff5f703fd in thread_native_entry (thread=0x7ffff07bd800) at /home/yym/code/jdk11u-master/src/hotspot/os/linux/os_linux.cpp:827
#6 0x00007ffff71c56ba in start_thread (arg=0x7fffc9b6a700) at pthread_create.c:333
#7 0x00007ffff790051d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
(gdb)
##释放锁
(gdb) b thread.cpp:2040
这篇关于openjdk11 jvm视角查看java线程异常退出synchronized锁自动释放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!