不死的小三儿:关于死锁的问题
程序代码:
public class Lock1 extends Thread {
private String str1;
private String str2;
public Lock1(String str1,String str2) {
super();
this.str1 = str1;
this.str2 = str2;
}
public void run(){
synchronized(str1){
System.out.println();
System.out.println(this.getName()+":"+"人质"+str1+"在我手上!把钱拿过来!记得别报警!!"+str2+"万!一分都不能少!");
System.out.println("不给钱就撕票!!!");
if(Police.rdm()==0){
System.out.println();
System.out.println(this.getName()+":"+"你敢报警!!撕票!啊!我死啦~~~");
System.out.println("旁白:"+str1+"被警察叔叔救了!");
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
synchronized(str2){
// this.notify();
System.out.println();
System.out.println(this.getName()+":"+"钱以拿到,放人!");
}
}
}
}
第2个类private String str1;
private String str2;
public Lock1(String str1,String str2) {
super();
this.str1 = str1;
this.str2 = str2;
}
public void run(){
synchronized(str1){
System.out.println();
System.out.println(this.getName()+":"+"人质"+str1+"在我手上!把钱拿过来!记得别报警!!"+str2+"万!一分都不能少!");
System.out.println("不给钱就撕票!!!");
if(Police.rdm()==0){
System.out.println();
System.out.println(this.getName()+":"+"你敢报警!!撕票!啊!我死啦~~~");
System.out.println("旁白:"+str1+"被警察叔叔救了!");
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
synchronized(str2){
// this.notify();
System.out.println();
System.out.println(this.getName()+":"+"钱以拿到,放人!");
}
}
}
}
程序代码:
public class Lock2 extends Thread {
private String str1;
private String str2;
public Lock2(String str1,String str2) {
super();
this.str1 = str1;
this.str2 = str2;
}
public void pay(){
}
public void run(){
synchronized (str2) {
System.out.println();
System.out.println(this.getName()+":"+str2+"万,我准备好了!放人!");
System.out.println("再不放人就报警了!");
if(Police.rdm()==1){
System.out.println();
System.out.println(this.getName()+":"+"我真的没有报警啊!给你双倍钱!把人放了把");
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
synchronized (str1) {
System.out.println();
System.out.println(this.getName()+":"+"钱没啦,人回来!悲剧啊!");
}
}
}
}
第3个类private String str1;
private String str2;
public Lock2(String str1,String str2) {
super();
this.str1 = str1;
this.str2 = str2;
}
public void pay(){
}
public void run(){
synchronized (str2) {
System.out.println();
System.out.println(this.getName()+":"+str2+"万,我准备好了!放人!");
System.out.println("再不放人就报警了!");
if(Police.rdm()==1){
System.out.println();
System.out.println(this.getName()+":"+"我真的没有报警啊!给你双倍钱!把人放了把");
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
synchronized (str1) {
System.out.println();
System.out.println(this.getName()+":"+"钱没啦,人回来!悲剧啊!");
}
}
}
}
程序代码:
public class Police {
public static int rdm(){
double r=Math.random()*10;
int rd=0;
if(r<5){
rd=0;
}else {
rd=1;
}
return rd;
}
}public class LockTest {
public static void main(String[] args) {
Lock1 l1=new Lock1("三儿","1");
Lock2 l2=new Lock2("三儿","1");
l1.setName("劫匪");
l2.setName("被害者");
l1.start();
l2.start();
}
}
public static int rdm(){
double r=Math.random()*10;
int rd=0;
if(r<5){
rd=0;
}else {
rd=1;
}
return rd;
}
}public class LockTest {
public static void main(String[] args) {
Lock1 l1=new Lock1("三儿","1");
Lock2 l2=new Lock2("三儿","1");
l1.setName("劫匪");
l2.setName("被害者");
l1.start();
l2.start();
}
}
this.wait();这个让人很纠结啊~
老是报错
怎么解决死锁啊~~
搜索更多相关的解决方案:
小三儿
----------------解决方案--------------------------------------------------------
synchronized 锁的对象不对!
----------------解决方案--------------------------------------------------------
要怎么生成锁呢?
----------------解决方案--------------------------------------------------------