gpt4 book ai didi

java - 解释 jstack 跟踪以追踪停滞的锁

转载 作者:行者123 更新时间:2023-12-02 07:08:53 28 4
gpt4 key购买 nike

我使用过 Samurai,我可以看到没有死锁并且有几个线程在等待,但我似乎无法确切地弄清楚哪个锁正在拖延进程。谁能帮我吗?

我不是在寻找人们猜测我的问题是什么,更多关于如何更改代码以使其更容易跟踪问题的建议。我完成了第一次发送和接收,然后程序在应该发送第二条消息时停止了。我是多线程新手,这是我第一次拉 jstack

2013-04-02 23:43:12
Full thread dump OpenJDK Zero VM (22.0-b10 mixed mode):

"Attach Listener" daemon prio=10 tid=0x0037c880 nid=0x105b waiting on condition [0x00000000]
java.lang.Thread.State: RUNNABLE

"Thread-3" prio=10 tid=0x0037c488 nid=0x1041 waiting on condition [0xa7ddd000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0xab770958> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
at org.dnsdojo.ryanhost.GA.MuPlusOne.RobotInterface.evaluate(RobotInterface.java:38)
at org.dnsdojo.ryanhost.GA.MuPlusOne.RobotInterface.run(RobotInterface.java:69)
at java.lang.Thread.run(Thread.java:722)

"Thread-1" prio=10 tid=0x0036ff10 nid=0x1036 waiting on condition [0xa7f5d000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0xab770940> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
at org.dnsdojo.ryanhost.GA.MuPlusOne.TwoWaySerialCommTest$SerialWriter.run(TwoWaySerialCommTest.java:229)
at java.lang.Thread.run(Thread.java:722)

"Thread-0" daemon prio=10 tid=0x0036a1e8 nid=0x1035 runnable [0xa80dd000]
java.lang.Thread.State: RUNNABLE
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1644)

"Service Thread" daemon prio=10 tid=0x00253440 nid=0x102b runnable [0x00000000]
java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x00251988 nid=0x102a runnable [0x00000000]
java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=10 tid=0x0020c880 nid=0x1029 in Object.wait() [0xa8ac1000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0xab718a88> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
- locked <0xab718a88> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:177)

"Reference Handler" daemon prio=10 tid=0x00209f18 nid=0x1028 in Object.wait() [0xa8c41000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0xab718b10> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:503)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
- locked <0xab718b10> (a java.lang.ref.Reference$Lock)

"main" prio=10 tid=0x00180fa8 nid=0x1022 in Object.wait() [0xb6848000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0xa90716a8> (a org.dnsdojo.ryanhost.GA.MuPlusOne.CandidateTest)
at java.lang.Object.wait(Object.java:503)
at org.dnsdojo.ryanhost.GA.MuPlusOne.MuPlusOneAlgorithm.runOnBot(MuPlusOneAlgorithm.java:120)
- locked <0xa90716a8> (a org.dnsdojo.ryanhost.GA.MuPlusOne.CandidateTest)
at org.dnsdojo.ryanhost.GA.MuPlusOne.MuPlusOneAlgorithm.initialFitness(MuPlusOneAlgorithm.java:72)
- locked <0xab718bd0> (a org.dnsdojo.ryanhost.GA.MuPlusOne.MuPlusOneAlgorithm)
at org.dnsdojo.ryanhost.GA.MuPlusOne.MuPlusOneAlgorithm.main(MuPlusOneAlgorithm.java:138)

"VM Thread" prio=10 tid=0x00204910 nid=0x1027 runnable

"VM Periodic Task Thread" prio=10 tid=0x00255130 nid=0x102c waiting on condition

JNI global references: 36

按要求添加代码串行通信类

    package org.dnsdojo.ryanhost.GA.MuPlusOne;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* This version of the TwoWaySerialComm example makes use of the
* SerialPortEventListener to avoid polling.
*
*/
public class TwoWaySerialComm
{
public TwoWaySerialComm()
{
super();
}

void connect ( String portName ) throws Exception
{
listPorts();
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();

(new Thread(new SerialWriter(out))).start();

serialPort.addEventListener(new SerialReader(in));
serialPort.notifyOnDataAvailable(true);

}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}

static void listPorts()
{
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while(portEnum.hasMoreElements())
{
CommPortIdentifier portIdentifier = portEnum.nextElement();
if(portIdentifier == null)
{
System.out.println("No ports");
}
System.out.println("Available - " + portIdentifier.getName());
}
}

/**
* Handles the input coming from the serial port. A new line character
* is treated as the end of a block in this example.
*/
public static class SerialReader implements SerialPortEventListener
{
private InputStream in;
private byte[] buffer = new byte[1024];

public SerialReader ( InputStream in )
{
this.in = in;
}

public void serialEvent(SerialPortEvent arg0) {
int data;

try
{
int len = 0;
while ( ( data = in.read()) > -1 )
{
if ( data == '\n' ) {
break;
}
buffer[len++] = (byte) data;
}
System.out.print(new String(buffer,0,len));
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(-1);
}
}

}

/** */
public static class SerialWriter implements Runnable
{
OutputStream out;

public SerialWriter ( OutputStream out )
{
this.out = out;
}

public void run ()
{
try
{
int c = 0;
while ( ( c = System.in.read()) > -1 )
{
this.out.write(c);
}
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(-1);
}
}
}



public static void main ( String[] args )
{
try
{
(new TwoWaySerialComm()).connect("/dev/ttyS82");
}
catch ( Exception e )
{
TwoWaySerialComm.listPorts();
e.printStackTrace();
}
}


}

RobotInterface 类

package org.dnsdojo.ryanhost.GA.MuPlusOne;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class RobotInterface implements Runnable
{
// create a serial connection
// transmit a string and check for response
// wait for evaluation
// take evaluation
private CandidateTest candidate;
private TwoWaySerialCommTest serialConnection;
//private Random rng = new Random();

protected static Logger logger = Logger.getLogger("Thread" + Thread.currentThread().getName());

public RobotInterface(CandidateTest test, TwoWaySerialCommTest serialConnection)
{
this.candidate = test;
this.serialConnection = serialConnection;
PropertyConfigurator.configure("log4j.properties");
}

public void evaluate (Genome genome)
{
//send to robot and return fitness
//genome.setFitness(rng.nextDouble());
logger.debug("fitness is " + genome.getFitness());

try
{
String s = candidate.getCandidate().toString();
System.out.println(s);
TwoWaySerialCommTest.lock.lock();
System.out.println(s);
serialConnection.put(s);
TwoWaySerialCommTest.inputAvailable.await();
try
{
candidate.getCandidate().setFitness(Float.parseFloat(serialConnection.take()));
}
catch(Exception e)
{
e.printStackTrace();
}


}
catch ( Exception e )
{
TwoWaySerialCommTest.listPorts();
e.printStackTrace();
}
finally
{
TwoWaySerialCommTest.lock.unlock();
}
}




public void run()
{
logger.debug("entering run of Robot Interface");
logger.debug("Send Genome via serial and wait for a response");
Genome testSubject = candidate.getCandidate();
evaluate(testSubject);
candidate.finished();
}
}

总结一下执行过程 - 我有一个 for 循环,它创建 RobotInterface 线程,每个线程对应一个包含位字符串的 Genome。该基因组引用保存在候选测试中,它只是 RobotInterface 的共享类,以及调用线程在完成时发出信号。

RobotInterface 使用 TwoWaySerialCommTest 将位串发送到机器人。机器人对其进行评估并返回一个适应度函数,该函数由 SerialReader 解析为 float 。然后将下一个字符串发送到机器人。

锁定是在第一个字符串返回到机器人并且正在发送第二个字符串之后发生的。我可以看到第一根绳子到达了机器人,但第二根绳子却没有。输出如下 -

initialFitness method
1101010101111111001100000101011100110000100001111111001000001001101101000011100101011000000100000000110110001110
5436 [main] DEBUG org.dnsdojo.ryanhost.GA.MuPlusOne.GeneticAlgorithm - Testing candidate
1101010101111111001100000101011100110000100001111111001000001001101101000011100101011000000100000000110110001110
1101010101111111001100000101011100110000100001111111001000001001101101000011100101011000000100000000110110001110
5853 [Thread-2] DEBUG Threadmain - entering run of Robot Interface
5869 [Thread-2] DEBUG Threadmain - Send Genome via serial and wait for a response
5881 [Thread-2] DEBUG Threadmain - fitness is 0.0
1101010101111111001100000101011100110000100001111111001000001001101101000011100101011000000100000000110110001110
1101010101111111001100000101011100110000100001111111001000001001101101000011100101011000000100000000110110001110
10
Output buffer after put
1101010101111111001100000101011100110000100001111111001000001001101101000011100101011000000100000000110110001110

Buffer isn't empty

initialFitness method
1110110101110000001100101100110001001010010101011110001101010010100100111011111000011101110000001110100111001111
16593 [main] DEBUG org.dnsdojo.ryanhost.GA.MuPlusOne.GeneticAlgorithm - Testing candidate
1110110101110000001100101100110001001010010101011110001101010010100100111011111000011101110000001110100111001111
1110110101110000001100101100110001001010010101011110001101010010100100111011111000011101110000001110100111001111
16944 [Thread-3] DEBUG Threadmain - entering run of Robot Interface
16953 [Thread-3] DEBUG Threadmain - Send Genome via serial and wait for a response
16964 [Thread-3] DEBUG Threadmain - fitness is 0.0
1110110101110000001100101100110001001010010101011110001101010010100100111011111000011101110000001110100111001111
1110110101110000001100101100110001001010010101011110001101010010100100111011111000011101110000001110100111001111
10
Output buffer after put
1110110101110000001100101100110001001010010101011110001101010010100100111011111000011101110000001110100111001111

Buffer isn't empty

最佳答案

我的方法是消除明显不相关的线程。对于您来说,这可能是

  • 附加监听器
  • 服务线程
  • 信号调度程序
  • 终结器
  • 引用处理程序
  • 虚拟机线程
  • VM 定期任务线程

这些是虚拟机管理的线程,即您没有显式或隐式创建它们。

剩下Thread-xMain 线程。

查看堆栈跟踪,您可以根据您对代码的了解来推断为什么WAITING。例如,队列看起来正在等待满足条件

parking to wait for <0xab770958> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)

条件信号唤醒的,因此您可能会想为什么没有发送信号消息。由于此条件包含在队列实现中,因此问题不太可能不是您忘记调用 signal。但是,您可能忘记调用一些会反过来调用它的东西?或者无意中设置了一个永远无法满足条件的情况?

因此,我会查看事件流,以确保锁定的任何内容都已解锁,或者任何可能导致活锁的情况(请记住,JVM/samauri 可以检测死锁,但不能检测活锁) )。

这是我对如何解决这个问题的一般想法。如果您想将代码发布到某个地方,这将有助于实际调试它/提供更多关于人们如何解决它的见解。

祝你好运!

关于java - 解释 jstack 跟踪以追踪停滞的锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15776500/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com