import java.util.*;
class VideoShop{
private Vector buffer = new Vector();
public VideoShop(){
buffer.addElement("Test01");
buffer.addElement("Test02");
buffer.addElement("Test03");
buffer.addElement("Test04");
buffer.addElement("Test05");
}
public synchronized String lendVideo(){
if(buffer.size() > 0){
String v= (String)this.buffer.remove(buffer.size() - 1);
return v;
} else {
return null;
}
}
public synchronized void returnVideo(String video){
this.buffer.addElement(video);
}
}
class Person extends Thread{
public void run(){
// synchronized (VideoShopMain.vshop) {
// 5초 동안 VideoShopMain.vshop은 Lock에 걸리게 됨
String v = VideoShopMain.vshop.lendVideo();
if(v == null){
System.out.println(this.getName() + " 비디오가 없음");
return;
}
try{
System.out.println(this.getName() + " : " + v + "대여");
//System.out.println(this.getName() + " : " + v + "보는중");
this.sleep(5000);
//System.out.println(this.getName() + " : " + v + "반납");
VideoShopMain.vshop.returnVideo(v);
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
// }
}
}
public class VideoShopMain {
public static VideoShop vshop = new VideoShop();
public static void main(String[] args){
System.out.println("프로그램 시작");
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
Person p4 = new Person();
Person p5 = new Person();
Person p6 = new Person();
Person p7 = new Person();
p1.start();
p2.start();
p3.start();
p4.start();
p5.start();
p6.start();
p7.start();
//System.out.println("프로그램 종료");
}
}