게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
자바 질문있습니다. 쓰레드 동기화 관련된 문제입니다.
게시물ID : programmer_5868짧은주소 복사하기
작성자 : 홍기씨
추천 : 0
조회수 : 2645회
댓글수 : 12개
등록시간 : 2014/10/10 01:58:04
class Account{       
 private int tot = 1000000;    
 synchronized boolean Payment(int pay) {  
  if(pay<tot){ 
   tot -= pay; 
   return true;
  }
  else{     
   return false;   
  }
   }
 int gettot() {
  return tot;
  
 }
}
class Customer extends Thread {
 Account acc1;
 
 Customer(Account acc){
  this.acc1 = acc;
 }
 
 public void run() {
  try{
   
  while(acc1.Payment(9800)) {  
   
   System.out.println(getName() + " 9800원 인출, 남은잔액 : " + acc1.gettot());
   sleep(50);
  }
  }catch(Exception e){
   System.out.println(e);
   
 }
 }
}
public class 과제1번 {
 public static void main(String[] args){
  Account acc = new Account();
  Customer[] cust = new Customer[4]; 
  for(int i=0; i<4; i++){        
   cust[i] = new Customer(acc); 
   cust[i].start();   
  }
  for(int i=0; i<4; i++){
   try {
   cust[i].join();
   
   }catch(InterruptedException e) {
    e.printStackTrace();
   }
  }
  System.out.println("잔액부족.남은금액:" + acc.gettot() + ", 인출금액 : 9800");
  System.out.println("총액은 : " + acc.gettot());
  
 }
 
임계영역을 지정해줘서 각 쓰레드가 9800원씩 출금을 하는 내용인데 왜 겹치는 부분이 있는지 궁금합니다. 해결 방법도 궁금합니다.
제목 없음.png
 
 
 
 

 


전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호