public class TestService {
public static void main(String[] args) {
Thread one=new Thread("one"){
public void run() {
synchronized(TestService.class){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (args) {
System.out.println("one ok");
}
}
};
};
Thread two=new Thread("two"){
public void run() {
synchronized (args) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(TestService.class){
System.out.println("two ok");
}
}
};
};
one.start();
two.start();
}
}
引用
C:\Users\Administrator>jps
3104
7060 TestService
6428 Jps
7180
C:\Users\Administrator>jstack -l 7060
2016-12-16 14:02:15
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode):
"DestroyJavaVM" #11 prio=5 os_prio=0 tid=0x0000000001c1e000 nid=0xe24 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"two" #10 prio=5 os_prio=0 tid=0x00000000584ae800 nid=0x1924 waiting for monitor entry [0x0000000058d8f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at testweb.TestService$2.run(TestService.java:35)
- waiting to lock <0x00000000d61578b0> (a java.lang.Class for testweb.TestService)
- locked <0x00000000d615b148> (a [Ljava.lang.String;)
Locked ownable synchronizers:
- None
"one" #9 prio=5 os_prio=0 tid=0x00000000584ad800 nid=0x1d6c waiting for monitor entry [0x0000000058c4f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at testweb.TestService$1.run(TestService.java:19)
- waiting to lock <0x00000000d615b148> (a [Ljava.lang.String;)
- locked <0x00000000d61578b0> (a java.lang.Class for testweb.TestService)
Locked ownable synchronizers:
- None
"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x000000005849a800 nid=0xd8c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C1 CompilerThread1" #7 daemon prio=9 os_prio=2 tid=0x0000000057089800 nid=0x1b18 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x0000000057084800 nid=0x358 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000057078800 nid=0x548 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000000057063000 nid=0x1f14 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000005700c000 nid=0x1e78 in Object.wait() [0x000000005801e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d6006f58> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x00000000d6006f58> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
Locked ownable synchronizers:
- None
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000057003000 nid=0x1fa4 in Object.wait() [0x0000000057e6f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d6006998> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
- locked <0x00000000d6006998> (a java.lang.ref.Reference$Lock)
Locked ownable synchronizers:
- None
"VM Thread" os_prio=2 tid=0x0000000056ffd000 nid=0x17f0 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000023d3800 nid=0x1d9c runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000023d6000 nid=0x1d08 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x00000000584a4800 nid=0x1e7c waiting on condition
JNI global references: 6
Found one Java-level deadlock:
=============================
"two":
waiting to lock monitor 0x000000005700a958 (object 0x00000000d61578b0, a java.lang.Class),
which is held by "one"
"one":
waiting to lock monitor 0x000000005700bdf8 (object 0x00000000d615b148, a [Ljava.lang.String;),
which is held by "two"
Java stack information for the threads listed above:
===================================================
"two":
at testweb.TestService$2.run(TestService.java:35)
- waiting to lock <0x00000000d61578b0> (a java.lang.Class for testweb.TestService)
- locked <0x00000000d615b148> (a [Ljava.lang.String;)
"one":
at testweb.TestService$1.run(TestService.java:19)
- waiting to lock <0x00000000d615b148> (a [Ljava.lang.String;)
- locked <0x00000000d61578b0> (a java.lang.Class for testweb.TestService)
Found 1 deadlock.
分享到:
相关推荐
本源代码是博客《Java 多线程编程之七:死锁(附源代码)》附带的实例性源代码。原博客链接地址:http://blog.csdn.net/defonds/archive/2010/01/26/5258120.aspx。
在"DeadLock.java"文件中,很可能会包含一个模拟死锁的示例代码。通常,这样的代码会创建两个或更多线程,每个线程会尝试获取一组锁,但顺序不同,从而导致死锁。例如: ```java class Resource { private int id;...
4. Java 中的死锁示例:在给定的示例代码中,我们可以看到两个线程,MyThread1 和 MyThread2,它们都需要访问两个共享资源 s1 和 s2。为了模拟死锁,我们使用 synchronized 块来锁住这两个资源,并在锁住的过程中...
下面是一个简单的死锁示例代码: ```java package javasimple; public class DieSynchronized { public static void main(String[] args) { Object o1 = new Object(); Object o2 = new Object(); Thread t1 =...
在数据库管理中,死锁是常见的问题之一,特别是在并发环境中,比如SQLite这样的嵌入式数据库。当两个或多个事务在执行过程中,互相等待对方释放资源,导致无法继续进行时,就发生了死锁。"database is locked"错误是...
在`ThreadDeadLock.java`文件中,可能包含了演示死锁的代码示例,例如两个线程各自持有对方需要的资源,导致双方都无法继续执行。解决死锁的方法包括避免循环等待、设置优先级、超时重试或者使用死锁检测算法,如...
Java死锁问题是一个在多线程编程中常见的复杂情况,它发生在两个或多个线程相互等待对方释放资源,导致它们都无法继续执行的情况。理解并解决死锁是Java开发者必须掌握的关键技能之一,尤其是在构建高并发、多线程...
首先,我们来看标题中的关键点——“Java多线程死锁示例”。死锁是多线程编程中的一种异常状态,通常发生在两个或多个线程之间,每个线程都在等待另一个线程释放它需要的资源,从而形成一个无解的循环等待。在这种...
本示例旨在为初学者提供一个全面理解Java多线程的起点。通过学习这个实例,你可以掌握如何创建和管理线程,理解线程同步与通信的重要性,以及如何处理线程安全问题。 1. **线程的创建** Java提供了两种创建线程的...
本主题将深入探讨死锁的概念、死锁的四个必要条件以及如何在Java中实现死锁检测。 死锁的定义: 死锁是指系统中的多个进程相互等待对方释放资源,从而陷入无法前进的状态。这种状态通常会导致系统停滞不前,影响...
以下是一个简单的Java代码示例,展示了如何使用`ReentrantLock`来预防锁饥饿和死锁: ```java import java.util.concurrent.locks.ReentrantLock; public class LockExample { private final ReentrantLock lock ...
以下是一个简单的Java代码框架示例: ```java class Process { int maxNeed; int currentNeed; int allocatedResources; } class Resource { int total; int available; } // 初始化系统资源和进程信息 List...
以下是一个简单的死锁示例: ```java public class DeadLockExample { public static void main(String[] args) { Resource resource1 = new Resource("Resource1"); Resource resource2 = new Resource(...
"并发编程Java代码示例.zip"这个压缩包很可能是包含了一系列有关Java并发编程的实例代码,帮助开发者理解和掌握如何在Java中有效地管理多线程。 Java并发编程涉及到的关键概念和知识点包括: 1. **线程**:线程是...
本示例着重探讨如何在Java中实现和管理多线程,以及它带来的挑战和解决方案。 一、Java多线程基础 1. 创建线程: - 继承Thread类:创建一个新的类,继承自Thread类,重写其run()方法,然后创建该类的实例并调用...
在示例中,解决死锁的一种方法是将获取口红锁的代码块移到外层,确保先完成照镜子的操作,再尝试获取口红的锁。这样,线程在进入等待状态之前,可以释放已获得的镜子的锁,从而避免死锁。 在main方法中,我们创建了...
- 检测和恢复死锁:定期检查系统状态,检测到死锁后,可以终止某个线程或者回滚事务来打破死锁状态。 4. 线程池 Java提供了ExecutorService和ThreadPoolExecutor类来管理线程,创建线程池可以有效控制并发线程的...
在Java中,死锁的实例可以通过一个简单的代码示例来解释。考虑以下情况: ```java public class DeadThread { private Thread thread1; public DeadThread(Object object1, Object object2){ thread1 = new ...
从上述提供的内容来看,动力节点Java学院提供了一个简单的死锁示例程序,通过该程序我们可以更好地理解死锁的发生情况及其分析方法。程序中定义了三个线程,每个线程都试图分别锁定两个对象资源。因为锁的获取顺序...