Java手写死锁并用jps和jstack查看
1 package com.test; 2 3 public class DeadLockTest { 4 public static void main(String[] args) { 5 new Thread(new DeadLock("TESTA","TESTB"),"ThreadAAAA").start(); 6 new Thread(new DeadLock("TESTB","TESTA"),"ThreadBBBB").start(); 7 } 8 } 9 10 class DeadLock implements Runnable{ 11 String a; 12 String b; 13 14 public DeadLock(String str1, String str2){ 15 this.a = str1; 16 this.b = str2; 17 } 18 19 @Override 20 public void run() { 21 synchronized (a){ 22 System.out.println(Thread.currentThread().getName()+"已经获得"+a+"\t尝试获得"+b); 23 synchronized (this){ 24 System.out.println(Thread.currentThread().getName()+"已经获得========="); 25 } 26 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } 27 synchronized (b){ 28 System.out.println(Thread.currentThread().getName()+"已经获得"+a+"\t尝试获得"+b); 29 } 30 } 31 } 32 }
在terminal里输入tps -l 查看当前系统运行的java进程,根据包名类名找到当前运行的进程以及pid,jstack pid查看详细信息