데드락 상황 만들기 & 해결

from JAVA 2013. 5. 7. 22:18

 

java.zip

class GameCharater
{
String name;
int hp;
int atk;
GameCharater(String n, int h, int a){
name = n;
hp = h;
atk = a;
}
}
-------------------------------------------------------

class Share
{
GameCharater ch1;
GameCharater ch2;
boolean flag;
}
-------------------------------------------------------

class Battle extends Thread
{
Share share;
Battle(Share s){
share=s;
}

void win(){
while(share.flag==true) continue;
System.out.println(share.ch1.name + "가 승리했다.");
share.flag = true;
}

public void run(){
while(share.ch2.hp>0){
try{
Thread.sleep(1000);
share.ch2.hp-=share.ch1.atk;
System.out.println(share.ch1.name + "가 " + share.ch2.name + "에게 " + share.ch1.atk + "의 대미지를 주었다.");
System.out.println(share.ch1.name + "의 체력이 " + share.ch1.hp + "남았다.");

}
catch(InterruptedException e){
System.out.println(e.getMessage());
}
}//while
win();
}//run
}

-------------------------------------------------------

class Battle2 extends Thread
{
Share share;
Battle2(Share s){
share=s;
}

void win(){
while(share.flag==true) continue;
System.out.println(share.ch2.name + "가 승리했다.");
share.flag = true;

}

public void run(){
while(share.ch1.hp>0){
try{
Thread.sleep(2000);
share.ch1.hp-=share.ch2.atk;
System.out.println(share.ch2.name + "가 " + share.ch1.name + "에게 " + share.ch2.atk + "의 대미지를 주었다.");
System.out.println(share.ch2.name + "의 체력이 " + share.ch2.hp + "남았다.");
if(share.flag==true) break;

}
catch(InterruptedException e){
System.out.println(e.getMessage());
}

}//while
win();
}//run
}

-------------------------------------------------------

class MainClass
{
public static void main(String[] args)
{
Share share = new Share();
share.ch1 = new GameCharater("용사", 100, 100);
share.ch2 = new GameCharater("마왕", 300, 30);
Battle battle1 = new Battle(share);
Battle2 battle2 = new Battle2(share);
battle1.start();
battle2.start();
}
}

 

데드락 해결

class GameCharater
{
 String name;
 int hp;
 int atk;
 GameCharater(String n, int h, int a){
  name = n;
  hp = h;
  atk = a;
 }
}
-------------------------------------------------------

class Share
{
 GameCharater ch1;
 GameCharater ch2;
 boolean flag; 
}
-------------------------------------------------------

class Battle extends Thread
{
 Share share;
 Battle(Share s){
  share=s;
 } 

 void win(){  
   if(share.flag==true){
    System.out.println(share.ch1.name + "가 승리했다~~");
   }
 }

  void perfomance(int hp){  
  }

 public void run(){
 while(share.ch2.hp>0){  
  
  try{
   Thread.sleep(1000);
   share.ch2.hp-=share.ch1.atk;
   System.out.println(share.ch1.name + "가 " + share.ch2.name + "에게 " + share.ch1.atk + "의 대미지를 주었다.");
   System.out.println(share.ch1.name + "의 체력이 " + share.ch1.hp + "남았다.");
   perfomance(share.ch2.hp);
   
   
  }
  catch(InterruptedException e){
   System.out.println(e.getMessage());
  }  
 }//while
 share.flag=true;
 win();
 }//run
}

-------------------------------------------------------

class Battle2 extends Thread
{
 Share share;
 Battle2(Share s){
  share=s;
 }
 
 void win(){   
   if(share.flag!=true){
    System.out.println(share.ch1.name + "가 승리했다~~");
   } 
 }

 public void run(){
 while(share.ch1.hp>0){
  try{
   Thread.sleep(2000);
   share.ch1.hp-=share.ch2.atk;
   System.out.println(share.ch2.name + "가 " + share.ch1.name + "에게 " + share.ch2.atk + "의 대미지를 주었다.");
   System.out.println(share.ch2.name + "의 체력이 " + share.ch2.hp + "남았다.");
   if(share.flag==true) break;
   
  }
  catch(InterruptedException e){
   System.out.println(e.getMessage());
  }
  
 }//while
 share.flag=false;
 win();
 }//run
}

-------------------------------------------------------


 

class MainClass
{
 public static void main(String[] args)
 {
  Share share = new Share();
  share.ch1 = new GameCharater("용사", 50, 100);
  share.ch2 = new GameCharater("마왕", 300, 30);
  Battle battle1 = new Battle(share);
  Battle2 battle2 = new Battle2(share);
  battle1.start();
  battle2.start();
 }
}
,