package com.zk.netty.user;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.*;
import com.alibaba.fastjson.JSONObject;
import com.zk.netty.enums.Code;
import io.netty.util.ThreadDeathWatcher;
import io.netty.util.Version;
import io.netty.util.concurrent.DefaultThreadFactory;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang.time.StopWatch;
public class CodeMsgUtil {
public static void main(String args[]){
try {
System.out.println(getProcessID());
ExecutorService executorService = Executors.newFixedThreadPool(2);
byte[] i = new byte[0];
byte[] j = new byte[0];
final CountDownLatch countDownLatch = new CountDownLatch(2);
executorService.execute(new DeadThread1(i,j,countDownLatch));
executorService.execute(new DeadThread2(i,j,countDownLatch));
countDownLatch.await();
System.out.println("done 1");
}catch (Exception e){
}
}
public static class DeadThread1 implements Runnable{
private byte[] i;
private byte[] j;
private CountDownLatch countDownLatch;
public DeadThread1(byte[] i,byte[] j ,CountDownLatch countDownLatch){
this.i=i;
this.j=j;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
synchronized (i){
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
synchronized (j){
System.out.println(Thread.currentThread().getName()+" is running");
countDownLatch.countDown();
}
}
}
}
public static class DeadThread2 implements Runnable{
private byte[] i;
private byte[] j;
private CountDownLatch countDownLatch;
public DeadThread2(byte[] i,byte[] j ,CountDownLatch countDownLatch){
this.i=i;
this.j=j;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
synchronized (j){
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
synchronized (i){
System.out.println(Thread.currentThread().getName()+" is running");
countDownLatch.countDown();
}
}
}
}
public static final int getProcessID() {
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
System.out.println(runtimeMXBean.getName());
return Integer.valueOf(runtimeMXBean.getName().split("@")[0])
.intValue();
}
}
上面的代码运行会出现死循环,所以打开cmd,输入jps命令,jps很简单可以直接显示java进程的pid,如下为7732:
输入jstack -l 7732
最后输出
找到了死循环,具体也输出了死循环的地方。接下来自己解决死循环