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("프로그램 종료"); } }
'공부 > JAVA' 카테고리의 다른 글
run()과 start()의 차이 - 멀티 쓰레드(Multi Thread) (0) | 2014.04.29 |
---|---|
자바 쓰레드 정리 (0) | 2014.04.28 |
자바 Thread 쓰레드 개념 예제 소스 (0) | 2014.04.28 |
자바에서 쓰레드(Thread)를 사용하는 방법 (0) | 2014.04.28 |
이클립스 text font 크기 키우기 (이클립스 글자 키우기) (0) | 2014.04.28 |