线程死锁 java
Deadlock of threads java
我是线程的新手,想出了一个死锁示例。
我试图重现死锁场景,但代码运行良好,没有任何问题。
请指导我哪里错了。
下面是代码片段
package Practice;
public class Deadlock {
public static void main(String[] args) {
Deadlock a = new Deadlock();
Threadslock first = new Threadslock(a);
Threadslock second = new Threadslock(a);
first.setName("First");
second.setName("Second");
first.start();
second.start();
}
}
class Threadslock extends Thread
{
Deadlock lock ;
private String anotherLock = "";
Threadslock(Deadlock lo)
{
lock = lo;
}
public void run()
{
if(getName().equals("First"))
{
synchronized(lock)
{
synchronized(anotherLock)
{
try
{
Thread.sleep (2000);
}
catch(InterruptedException r)
{
}
System.out.println("First Thread");
System.out.println("Next Step in First");
}
}
}
else
{
synchronized(anotherLock)
{
synchronized(lock)
{
try{
Thread.sleep (2000);
}
catch(Exception e)
{
}
System.out.println("Second Thread");
System.out.println("Next Step in Second");
}
}
}
}
}
输出是这样的:
第一个话题
下一步
第二个线程
第二步下一步
两个锁都需要共享才能产生死锁。试试这个
package Practice;
public class Deadlock {
public static void main(String[] args) {
Deadlock l1 = new Deadlock();
Deadlock l2 = new Deadlock();
Threadslock first = new Threadslock("First", l1, l2);
Threadslock second = new Threadslock("Second", l2, l1);
first.start();
second.start();
}
}
class Threadslock extends Thread
{
Deadlock first;
Deadlock second;
String name;
Threadslock(String name, Deadlock first, Deadlock second)
{
this.name = name;
this.first = first;
this.second = second;
}
public void run()
{
synchronized(first)
{
try
{
Thread.sleep (2000);
}
catch(InterruptedException r)
{
}
synchronized(second)
{
System.out.println(name + " Thread");
System.out.println("Next Step in " + name);
}
}
}
}
编辑:在获取锁之间添加休眠
正如@Sean Bright 所建议的,您在错误的地方添加了睡眠。
此外,您在两个线程中都有两个 anotherLock
实例,它永远不会死锁,因为线程第一线程和第二线程都可以获得自己的 anotherLock
。所以必须让两个线程共享同一个anotherLock。
请检查下面的代码,希望对您有所帮助。
public class Deadlock {
public static void main(String[] args) {
Deadlock a = new Deadlock();
String anotherLock = "";
Threadslock first = new Threadslock(a,anotherLock);
Threadslock second = new Threadslock(a,anotherLock);
first.setName("First");
second.setName("Second");
first.start();
second.start();
}
}
class Threadslock extends Thread
{
Deadlock lock ;
String anotherLock;
Threadslock(Deadlock lo, String anotherLock)
{
lock = lo;
this.anotherLock = anotherLock;
}
public void run()
{
if(getName().equals("First"))
{
synchronized(lock)
{
System.out.println("First Thread");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(anotherLock)
{
try
{
Thread.sleep (2000);
}
catch(InterruptedException r)
{
}
System.out.println("Next Step in First");
}
}
}
else
{
synchronized(anotherLock)
{
System.out.println("Second Thread");
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
synchronized(lock)
{
try{
Thread.sleep (2000);
}
catch(Exception e)
{
}
System.out.println("Next Step in Second");
}
}
}
}
}
我是线程的新手,想出了一个死锁示例。 我试图重现死锁场景,但代码运行良好,没有任何问题。
请指导我哪里错了。 下面是代码片段
package Practice;
public class Deadlock {
public static void main(String[] args) {
Deadlock a = new Deadlock();
Threadslock first = new Threadslock(a);
Threadslock second = new Threadslock(a);
first.setName("First");
second.setName("Second");
first.start();
second.start();
}
}
class Threadslock extends Thread
{
Deadlock lock ;
private String anotherLock = "";
Threadslock(Deadlock lo)
{
lock = lo;
}
public void run()
{
if(getName().equals("First"))
{
synchronized(lock)
{
synchronized(anotherLock)
{
try
{
Thread.sleep (2000);
}
catch(InterruptedException r)
{
}
System.out.println("First Thread");
System.out.println("Next Step in First");
}
}
}
else
{
synchronized(anotherLock)
{
synchronized(lock)
{
try{
Thread.sleep (2000);
}
catch(Exception e)
{
}
System.out.println("Second Thread");
System.out.println("Next Step in Second");
}
}
}
}
}
输出是这样的:
第一个话题
下一步
第二个线程
第二步下一步
两个锁都需要共享才能产生死锁。试试这个
package Practice;
public class Deadlock {
public static void main(String[] args) {
Deadlock l1 = new Deadlock();
Deadlock l2 = new Deadlock();
Threadslock first = new Threadslock("First", l1, l2);
Threadslock second = new Threadslock("Second", l2, l1);
first.start();
second.start();
}
}
class Threadslock extends Thread
{
Deadlock first;
Deadlock second;
String name;
Threadslock(String name, Deadlock first, Deadlock second)
{
this.name = name;
this.first = first;
this.second = second;
}
public void run()
{
synchronized(first)
{
try
{
Thread.sleep (2000);
}
catch(InterruptedException r)
{
}
synchronized(second)
{
System.out.println(name + " Thread");
System.out.println("Next Step in " + name);
}
}
}
}
编辑:在获取锁之间添加休眠
正如@Sean Bright 所建议的,您在错误的地方添加了睡眠。
此外,您在两个线程中都有两个 anotherLock
实例,它永远不会死锁,因为线程第一线程和第二线程都可以获得自己的 anotherLock
。所以必须让两个线程共享同一个anotherLock。
请检查下面的代码,希望对您有所帮助。
public class Deadlock {
public static void main(String[] args) {
Deadlock a = new Deadlock();
String anotherLock = "";
Threadslock first = new Threadslock(a,anotherLock);
Threadslock second = new Threadslock(a,anotherLock);
first.setName("First");
second.setName("Second");
first.start();
second.start();
}
}
class Threadslock extends Thread
{
Deadlock lock ;
String anotherLock;
Threadslock(Deadlock lo, String anotherLock)
{
lock = lo;
this.anotherLock = anotherLock;
}
public void run()
{
if(getName().equals("First"))
{
synchronized(lock)
{
System.out.println("First Thread");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(anotherLock)
{
try
{
Thread.sleep (2000);
}
catch(InterruptedException r)
{
}
System.out.println("Next Step in First");
}
}
}
else
{
synchronized(anotherLock)
{
System.out.println("Second Thread");
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
synchronized(lock)
{
try{
Thread.sleep (2000);
}
catch(Exception e)
{
}
System.out.println("Next Step in Second");
}
}
}
}
}