Java multithreading deadlock problem

Just used jstack to solve a process deadlock problem.

The process is roughly as follows:
(0) Environmental requirements, JDK1.6 and above
(1) First find the PID of the process, under Windows, open the process manager, sort by name, you can find the process called javaw.exe (java virtual machine processes are all It is called javaw.exe), to find out which is your process, remember the current process list, and then restart your process, the one whose PID has been refreshed is your process.
(2) Run under CMD: jstack pid, jstack will print a series of information on the console
(3) Analyze the above information

"startQuertz_Worker-2" prio=6 tid=0x000000006201f000 nid=0x14b0 runnable [0x0000000068b9e000]
   java.lang.Thread.State: RUNNABLE
	at java.net.SocketInputStream.socketRead0(Native Method)
	at java.net.SocketInputStream.read(SocketInputStream.java:129)
	at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:97)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
	- locked <0x0000005896b7a708> (a java.io.BufferedInputStream)
	at com.sun.mail.iap.ResponseInputStream.read0(ResponseInputStream.java:81)
	at com.sun.mail.iap.ResponseInputStream.readResponse(ResponseInputStream.java:67)
	at com.sun.mail.iap.Response.<init>(Response.java:83)
	at com.sun.mail.imap.protocol.IMAPResponse.<init>(IMAPResponse.java:48)
	at com.sun.mail.imap.protocol.IMAPResponse.readResponse(IMAPResponse.java:122)
	at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:230)
	at com.sun.mail.iap.Protocol.command(Protocol.java:263)
	- locked <0x0000005896b74cb8> (a com.sun.mail.imap.protocol.IMAPProtocol)
	at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1234)
	at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1226)
	at com.sun.mail.imap.protocol.IMAPProtocol.fetchBody(IMAPProtocol.java:1028)
	at com.sun.mail.imap.protocol.IMAPProtocol.fetchBody(IMAPProtocol.java:1023)
	at com.sun.mail.imap.IMAPInputStream.fill(IMAPInputStream.java:101)
	- locked <0x0000005896bb77d0> (a java.lang.Object)
	at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:158)
	- locked <0x0000005897da6540> (a com.sun.mail.imap.IMAPInputStream)
	at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:184)
	at com.sun.mail.util.BASE64DecoderStream.getByte(BASE64DecoderStream.java:337)
	at com.sun.mail.util.BASE64DecoderStream.decode(BASE64DecoderStream.java:230)
	at com.sun.mail.util.BASE64DecoderStream.read(BASE64DecoderStream.java:135)
	at java.io.FilterInputStream.read(FilterInputStream.java:90)
	at javax.mail.internet.MimeBodyPart.saveFile(MimeBodyPart.java:805)
	at com.detao.dtma.email.EmailReciver.processAttachment(EmailReciver.java:244)
	at com.detao.dtma.email.EmailReciver.processMsg(EmailReciver.java:140)
	at com.detao.dtma.email.EmailReciver.processMsg(EmailReciver.java:93)
	at com.detao.dtma.email.EmailReciver.processMessages(EmailReciver.java:69)
	at com.detao.dtma.email.EmailReciver.processMessages(EmailReciver.java:50)
	at com.detao.dtma.email.EmailReciverMgr.run(EmailReciverMgr.java:31)
	at sun.reflect.GeneratedMethodAccessor2511.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
	at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:264)
	at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)

"startQuertz_Worker-1" prio=6 tid=0x0000000064022800 nid=0x3a4 waiting for monitor entry [0x0000000068a9f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x0000005872f40648> (a java.lang.Object)
	at org.quartz.simpl.SimpleThreadPool.getNextRunnable(SimpleThreadPool.java:428)
	- locked <0x0000005872f40648> (a java.lang.Object)
	at org.quartz.simpl.SimpleThreadPool.access$000(SimpleThreadPool.java:47)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:518)

"startQuertz_Worker-0" prio=6 tid=0x0000000060f15000 nid=0x13e8 waiting for monitor entry [0x000000006899f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x0000005872f40648> (a java.lang.Object)
	at org.quartz.simpl.SimpleThreadPool.getNextRunnable(SimpleThreadPool.java:428)
	- locked <0x0000005872f40648> (a java.lang.Object)
	at org.quartz.simpl.SimpleThreadPool.access$000(SimpleThreadPool.java:47)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:518)

"Store userCache Spool Thread" daemon prio=6 tid=0x000000006194f000 nid=0x1354 waiting on condition [0x000000006889f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at net.sf.ehcache.store.DiskStore.spoolAndExpiryThreadMain(DiskStore.java:634)
	at net.sf.ehcache.store.DiskStore.access$900(DiskStore.java:68)
	at net.sf.ehcache.store.DiskStore$SpoolAndExpiryThread.run(DiskStore.java:1110)


Finally locate the error line
at com.detao.dtma.email.EmailReciverMgr.run(EmailReciverMgr.java:31)

find the corresponding code
public class EmailReciverMgr extends TimerTask {
	/* Mail receiver A mail receiver corresponds to a mailbox */
	private java.util.List<EmailReciver> recivers;
	/* Whether the program is running */
	private boolean running = false;

	public void setRecivers(java.util.List<EmailReciver> recivers) {
		this.recivers = recivers;
	}

	@Override
	public void run() {
		// return if running, otherwise run
		if (running) {
			return;
		}
		try {
			running = true;
			// loop mail receiver
			for (EmailReciver reciver : recivers) {
				// handle email
				reciver.processMessages();
			}
		} catch (Exception e) {
			e.printStackTrace ();
		} finally {
			// Set to false after running for the next run
			running = false;
		}

	}
}

There is no problem with the program itself, but the problem occurs in the call. The two timers have their own timing strategies, and they are called twice, causing each other to wait and form a deadlock.

The difficulty of this problem lies in the positioning of the problem. The occurrence of the problem is uncertain. Sometimes it does not occur for half a month, sometimes it occurs several times a day, the problem cannot be located, and there is no error in the background. Finally, jstack finally locates the problem and finds the problem. After that, the solution is fine.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326800957&siteId=291194637