赞
踩
//①线程死锁查看 package com.chapter12; public class ThreadDeadLock { public static void main(String[] args) { StringBuilder s1 = new StringBuilder(); StringBuilder s2 = new StringBuilder(); new Thread() { @Override public void run() { synchronized (s1) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (s2) { s1.append("b"); s2.append("2"); System.out.println(s1); System.out.println(s2); } } } }.start(); new Thread() { @Override public void run() { synchronized (s2) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (s1) { s1.append("d"); s2.append("4"); System.out.println(s1); System.out.println(s2); } } } }.start(); } } C:\Users\Administrator\IdeaProjects\jvm>jstack 12840 执行结果: Found one Java-level deadlock: ============================= "Thread-1": waiting to lock monitor 0x0000000002d0b288 (object 0x000000071649ee08, a java.lang.StringBuilder), which is held by "Thread-0" "Thread-0": waiting to lock monitor 0x0000000002d0c728 (object 0x000000071649ee50, a java.lang.StringBuilder), which is held by "Thread-1" Java stack information for the threads listed above: =================================================== "Thread-1": at com.chapter12.ThreadDeadLock$2.run(ThreadDeadLock.java:40) - waiting to lock <0x000000071649ee08> (a java.lang.StringBuilder) - locked <0x000000071649ee50> (a java.lang.StringBuilder) "Thread-0": at com.chapter12.ThreadDeadLock$1.run(ThreadDeadLock.java:22) - waiting to lock <0x000000071649ee50> (a java.lang.StringBuilder) - locked <0x000000071649ee08> (a java.lang.StringBuilder) Found 1 deadlock. //①线程Sleep查看 public class ThreadSleepTest { public static void main(String[] args) { System.out.println("hello - 1"); try { Thread.sleep(1000 * 60 * 10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("hello - 2"); } } //执行结果 >> jstack 920 "main" #1 prio=5 os_prio=0 tid=0x00000000036d2800 nid=0x1cb0 waiting on condition [0x000000000320f000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at com.chapter12.ThreadSleepTest.main(ThreadSleepTest.java:12) //③线程同步测试 class Number implements Runnable { private int number = 1; @Override public void run() { while (true) { synchronized (this) { if (number <= 100) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + number); number++; } else { break; } } } } } public class ThreadSyncTest { public static void main(String[] args) { Number number = new Number(); Thread t1 = new Thread(number); Thread t2 = new Thread(number); t1.setName("线程1"); t2.setName("线程2"); t1.start(); t2.start(); } } //执行结果 "线程2" #12 prio=5 os_prio=0 tid=0x0000000027b56800 nid=0x3380 waiting on condition [0x000000002879f000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at com.chapter12.Number.run(ThreadSyncTest.java:15) - locked <0x00000007164a1960> (a com.chapter12.Number) at java.lang.Thread.run(Thread.java:748) "线程1" #11 prio=5 os_prio=0 tid=0x0000000027b4d800 nid=0x1d18 waiting for monitor entry [0x000000002869f000] java.lang.Thread.State: BLOCKED (on object monitor) at com.chapter12.Number.run(ThreadSyncTest.java:13) - waiting to lock <0x00000007164a1960> (a com.chapter12.Number) at java.lang.Thread.run(Thread.java:748)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。