반응형
Notice
Recent Posts
Recent Comments
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Do Something IT

쓰레드의 Wait()와 notify()사용 본문

OtherLanguage/JAVA

쓰레드의 Wait()와 notify()사용

아낙시만더 2010. 6. 29. 17:13
반응형
출처 : http://blog.daum.net/bifrost0076/5882414 

/** 쓰레드의 Wait()와 notify()사용

import! static java.lang.System.out;

class ATM implements Runnable{
 private long deposit_Money = 10000; //잔액
 private boolean flag = false;
 public void run(){
  synchronized(this){
   for(int i = 0 ; i < 10 ; i++){
    try{
     Thread.sleep(500);
    }catch(Exception e){
     e.printStackTrace();
    }
    if(getDepositMoney() <= 0)
     break;
    withDraw(1000);
   }
  }
 }
 public void withDraw(long lv_howmuch){
  if(getDepositMoney() > 0){
   deposit_Money -= lv_howmuch;
   out.println(Thread.currentThread().getName() + ",");
   System.out.printf("잔액 : %, d 원 %n",getDepositMoney());
  }else{
   System.out.print(Thread.currentThread().getName() + "," );
   out.println("잔액이 부족합니다");
  }
 }

 public long getDepositMoney(){
  return deposit_Money;
 } 
}

public class SynchronizedEx{
 public static void main(String[] ar){
  ATM atm = new ATM();
  Thread mother = new Thread(atm,"mother");
  Thread son = new Thread(atm,"son");
  
  mother.start();
  son.start();
  
 }

-->위에 프로그램은 동기화기법으로 작성한 프로그램이다.
쓰레드 객체인 mother와 som이 sychronized 영역 안으로 들어간다.
mother 객체가 먼저 들어가면 mother객체가 프로그램이 종료가 될 때까지 계속 실행이 된다.
또한, son 객체가 먼저 들어가면 son객체가 프로그램이 종료가 될 때까지 계속 실행이 된다.

실행 결과는 mother , 잔액 9000원
      mother , 잔액 8000원
      mother , 잔액 7000원 ...
만약에 mother son이 서로 서로 인출을 하려면 wait()와 notify()를 사용하면 된다.

wait()메소드는 다른 쓰레드 객체가 notify()나 notifyAll() 메소드를 실행시키기 전까지 현재 쓰레드 객체를 기다리게 한다.
nofity()메소드는 현재 대기하고 있는 쓰레드 객체 하나를 깨운다.

그래서 wait()메소드와 nofity()메소드를 사용해서 수정한 소스는 아래와 같다.
*/
class ATM implements Runnable{
 private long deposit_Money = 10000; //예금액
 private boolean flag = false;
 public void run(){
  synchronized(this){
   for(int i = 0 ; i < 10 ; i++){
    try{
     Thread.sleep(500);
    if(flag == false){
     flag = true;  
     wait();
    }

    }catch(Exception e){
     e.printStackTrace();
    }
    notifyAll();
    flag = false;

    if(getDepositMoney() <= 0)
     break;
    withDraw(1000);


   }
  }
 }
 public void withDraw(long lv_howmuch){
  if(getDepositMoney() > 0){
   deposit_Money -= lv_howmuch;
   out.println(Thread.currentThread().getName() + ",");
   System.out.printf("잔액 : %, d 원 %n",getDepositMoney());
  }else{
   System.out.print(Thread.currentThread().getName() + "," );
   out.println("잔액이 부족합니다");
  }
 }

 public long getDepositMoney(){
  return deposit_Money;
 } 
}

public class SynchronizedEx{
 public static void main(String[] ar){
  ATM atm = new ATM();
  Thread mother = new Thread(atm,"mother");
  Thread son = new Thread(atm,"son");
  
  mother.start();
  son.start();
  
 }
}

 

 실행결과

 /**

 mother객체가 2000원씩 인출하면 son객체도 이어서 2000원씩 뽑아 보도록 변경해보았다.
*/
class ATM implements Runnable{
 private long deposit_Money = 10000; //예금액
 private boolean flag = false;
 public void run(){
  synchronized(this){
   for(int i = 0 ; i < 10 ; i++){ 
    try{
     withDraw(1000);
     if(getDepositMoney() <= 0)
      break;
     if( getDepositMoney() == 2000 ||
      getDepositMoney() == 4000 ||
      getDepositMoney() == 6000 ||
      getDepositMoney() == 8000 )
      this.wait();
     Thread.sleep(200);
    }catch(Exception e){
     e.printStackTrace();
    }

    notifyAll(); 
   
   }
  }
 }
 public void withDraw(long lv_howmuch){
  if(getDepositMoney() > 0){
   deposit_Money -= lv_howmuch;
   out.println(Thread.currentThread().getName() + ",");
   System.out.printf("잔액 : %, d 원 %n",getDepositMoney());
  }else{
   System.out.print(Thread.currentThread().getName() + "," );
   out.println("잔액이 부족합니다");
  }
 }

 public long getDepositMoney(){
  return deposit_Money;
 } 
}

public class SynchronizedEx{
 public static void main(String[] ar){
  ATM atm = new ATM();
  Thread mother = new Thread(atm,"mother");
  Thread son = new Thread(atm,"son");
  
  mother.start();
  son.start();
  
 }
}

반응형
Comments