晚上学习线程同步的时候,一个关于死锁的问题,不知道该怎么解决
程序源码:
package com.TextDeadLock;public class TextDeadLock { public static void main(String[] args) {
Lock tdl1 = new Lock();
Lock tdl2 = new Lock();
tdl1.flag = 1;
tdl2.flag = 0;
Thread thr1 = new Thread(tdl1);
Thread thr2 = new Thread(tdl2);
thr1.start();
thr2.start();
}
}class Lock implements Runnable{
static Object o1 = new Object();
static Object o2 = new Object();
static int flag = 1;
public void run(){
System.out.println("Flag = " + flag);
if(flag == 1){
synchronized (o1) {
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized (o2) {
System.out.println("1");
}
}
if(flag == 0){
synchronized (o2) {
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized (o1) {
System.out.println("0");
}
}
}
}理论上程序的运行结果应该是
输出
Flag = 1
Flag = 0
后程序卡死在这个地方了,可是的的输出结果为什么是:
Flag = 0
Flag = 0
0
0求帮助啊,谢谢
程序源码:
package com.TextDeadLock;public class TextDeadLock { public static void main(String[] args) {
Lock tdl1 = new Lock();
Lock tdl2 = new Lock();
tdl1.flag = 1;
tdl2.flag = 0;
Thread thr1 = new Thread(tdl1);
Thread thr2 = new Thread(tdl2);
thr1.start();
thr2.start();
}
}class Lock implements Runnable{
static Object o1 = new Object();
static Object o2 = new Object();
static int flag = 1;
public void run(){
System.out.println("Flag = " + flag);
if(flag == 1){
synchronized (o1) {
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized (o2) {
System.out.println("1");
}
}
if(flag == 0){
synchronized (o2) {
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized (o1) {
System.out.println("0");
}
}
}
}理论上程序的运行结果应该是
输出
Flag = 1
Flag = 0
后程序卡死在这个地方了,可是的的输出结果为什么是:
Flag = 0
Flag = 0
0
0求帮助啊,谢谢
这个地方既然是静态的,那么两个线程初始化的时候:
tdl1.flag = 1;
tdl2.flag = 0;
然后线程启动了:
thr1.start();
thr2.start();
要记住,在判断flag值的时候,你让一个锁定对象的时候休眠了,不休眠的话还好,休眠了对象锁死了。然后先一个一旦是另外一个线程运行,那么休眠时锁定的对象没有释放,就挂了。
这个是死锁的基本情形,两个线程相互等待对方锁定的资源。
package concurrency;class Demo1 {
public void get() {
System.out.println("已拥有 thread1 的锁");
}
}class Demo2 {
public void get() {
System.out.println("已拥有 thread2 的锁");
}
}class thread extends Thread {
/* 此处必须使用 public static final 原因未知*/
public static Demo1 t1 = new Demo1();
public static Demo2 t2 = new Demo2();
/*Demo1 t1 = new Demo1();
Demo2 t2 = new Demo2();*/
public boolean flag = false;
public thread(boolean b){
flag = b;
}
public void run() {
if (flag) {
synchronized (t1) {
t1.get();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
synchronized (t2) {
t2.get();
}
}
} else {
synchronized (t2) {
t2.get();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
synchronized (t1) {
t1.get();
}
}
}
}
}public class A_Synchronized {
public static void main(String[] args) {
thread t1 = new thread(false);
thread t2 = new thread(true);
t1.start();
t2.start();
}
}
package concurrency;public class TextDeadLock { public static void main(String[] args) {
Lock tdl1 = new Lock();
Lock tdl2 = new Lock();
tdl1.flag = 1;
tdl2.flag = 0;
Thread thr1 = new Thread(tdl1);
Thread thr2 = new Thread(tdl2);
thr1.start();
thr2.start();
}
}class Lock implements Runnable {
static Object o1 = new Object();
static Object o2 = new Object();
//1.楼主的 flag 不能设为 static,要不然 main() 方法里面针对对象改 flag 就没效果
//static int flag = 1;
int flag = 1; public void run() {
System.out.println("Flag = " + flag);
if (flag == 1) {
//2.楼主的原来代码没有在锁住 o1 之中 再锁住 o2,而是在锁住 o1 后才锁住 o2
synchronized (o1) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2) {
System.out.println("1");
}
} }
if (flag == 0) {
synchronized (o2) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o1) {
System.out.println("0");
}
} }
}
}