这个程序产生了死锁,现在要用synchronized/wait(notify)和lock/await(signal)两种方式完成,求教

MBT_Steven 2018-04-15 12:29:57
public class bank implements Runnable{

private static Object actors = new Object();
private static Object directors = new Object();
public int x;

public void run() {
System.out.println("进行转账");
if(x==1) {
synchronized(directors) {
System.out.println("演员开始转账");
try {
Thread.sleep(500);
}catch(Exception e) {
e.printStackTrace();
}
synchronized(actors) {
System.out.println("演员转账成功");
}
}
}
if(x==0) {
synchronized(actors) {
System.out.println("导演开始转账");
try {
Thread.sleep(500);
}catch(Exception e) {
e.printStackTrace();
}
synchronized(directors) {
System.out.println("导演转账成功");
}
}
}
}


public static void main(String[] args) {
bank actor =new bank();
bank director = new bank();

actor.x=1;
director.x=0;

new Thread(actor).start();
new Thread(director).start();
}
}
...全文
1058 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
flamen087 2018-04-16
  • 打赏
  • 举报
回复

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class test implements Runnable {
 
    private static Object actors = new Object();
    private static Object directors = new Object();
    public int x;
    private static Lock lock = new ReentrantLock();
 
    public void run() {
        System.out.println("进行转账");
        if (x == 1) {
        	lock.lock();
            synchronized (directors) {
                System.out.println("演员开始转账");
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronized (actors) {
                    System.out.println("演员转账成功");
                }
            }
            lock.unlock();
        }
        if (x == 0) {
        	lock.lock();
            synchronized (actors) {
                System.out.println("导演开始转账");
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronized (directors) {
                    System.out.println("导演转账成功");
                }
            }
            lock.unlock();
        }
    }
 
    public static void main(String[] args) {
        test actor = new test();
        test director = new test();
 
        actor.x = 1;
        director.x = 0;
 
        new Thread(actor).start();
        new Thread(director).start();
    }
}
大概就是这意思,先看看这些方法怎么用吧。
flamen087 2018-04-16
  • 打赏
  • 举报
回复

public class test implements Runnable {

	private static Object actors = new Object();
	private static Object directors = new Object();
	public int x;

	public void run() {
		System.out.println("进行转账");
		if (x == 1) {
			synchronized (directors) {
				System.out.println("演员开始转账");
				try {
					Thread.sleep(500);
					directors.wait();
					actors.notify();
				} catch (Exception e) {
					e.printStackTrace();
				}
				synchronized (actors) {
					System.out.println("演员转账成功");
				}
			}
		}
		if (x == 0) {
			synchronized (actors) {
				System.out.println("导演开始转账");
				try {
					Thread.sleep(500);
					actors.wait();
					directors.notify();
				} catch (Exception e) {
					e.printStackTrace();
				}
				synchronized (directors) {
					System.out.println("导演转账成功");
				}
			}
		}
	}

	public static void main(String[] args) {
		test actor = new test();
		test director = new test();

		actor.x = 1;
		director.x = 0;

		new Thread(actor).start();
		new Thread(director).start();
	}
}
这是wait的
oyljerry 2018-04-15
  • 打赏
  • 举报
回复
两个加锁顺序要一致。directors,actors
MBT_Steven 2018-04-15
  • 打赏
  • 举报
回复
引用 1 楼 oyljerry 的回复:
两个加锁顺序要一致。directors,actors
要怎么一致,请问能写一下吗

50,639

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧