package com.lee2;

public class Test2 {

	public static void main(String[] args) {

		start();
	
	}
	public static synchronized void start(){
		
		Alba alba1=new Alba("어서오세요");
		Alba alba2=new Alba("주문은?");
		Alba alba3=new Alba("또오세요");
		
		synchronized(Alba.class){
		alba1.start();	//10분	
		alba2.start();	//10분
		alba3.start();  //20분
		}
	}
}


package com.lee2;

public class Alba extends Thread{
	String msg;

	public Alba(String msg){
		this.msg=msg;
	}

	@Override
	public void run() {

		work();
	}

	private synchronized void work() {
		for( int i=0 ; i<10 ; i++){
			System.out.println(msg);

			try {
				Thread.sleep(1000);//1000->1초
			} catch (Exception e) {
				System.out.println("스레드 에러 발생");
				System.out.println(e.getMessage());
			}
		}
		System.out.println("사장님 안녕히계세요~");
	}
}




package com.park2;

public class Alba implements Runnable{
	String msg;
	
	public Alba(String msg){
		this.msg=msg;
	}
	
	@Override
	public void run() {
		work();
	}
	
	public synchronized void work(){
		for( int i=0 ; i<100 ; i++){
			System.out.println(msg);
			try{
				Thread.sleep(300); //1000 = 1초
			}catch(Exception e){
				System.out.println("스레드 에러발생");
				System.out.println(e.getMessage());
			}
		}
		System.out.println("사장님 안녕히계세요");
	}
}


package com.park2;

public class Test2 {

	public static void main(String[] args) {
		
		//run() - run메서드 전부 실행
		//start() -  background에서 실행
		//순서가 random
		
		start();
	}
	
	public static synchronized void start(){
		Alba alba1=new Alba("어서오세요");
		Thread a1=new Thread(alba1);
		// runnable로 스레드 클래스 만들었을 때
		
		Alba alba2=new Alba("주문은?");
		Thread a2=new Thread(alba2);
		
		Alba alba3=new Alba("또오세요");
		Thread a3=new Thread(alba3);
		
		System.out.println(Thread.currentThread().getName());
		System.out.println(a1.currentThread().getName());
		System.out.println(a2.getName());
		System.out.println(a3.getName());
		/*synchronized(Alba.class){
			alba1.start();	//10분	
			alba2.start();	//10분
			alba3.start();  //20분
		}*/		
	}
}


'Java > 2012.04 강좌' 카테고리의 다른 글

13일차 hashcode  (0) 2012.04.20
13일차 복습(thread,synchronized)  (0) 2012.04.20
12일차 Thread2  (0) 2012.04.19
12일차 Thread  (0) 2012.04.19
12일차 Exception(예외처리 미루기)  (0) 2012.04.19

+ Recent posts