Java 다중 스레드 - 3가지 실현 방식

1. Thread 클래스 계승
/**
 *     Thread      
 * 	  Thread  run(),run        ,          run  
 * 	        。start()
 * @author Administrator
 *
 */
public class Rabbit extends Thread{
	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println("    "+i+" ");
		}
	}
}
class Tortoise extends Thread{
	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println("    "+i+" ");
		}
	}
}
public class RabbitApp {
	public static void main(String[] args) {
		Rabbit r = new Rabbit();
		Tortoise tor = new Tortoise();
		r.start();//    1
		tor.start();//    2
	}
}

2. Runnable 인터페이스 구현
1) 정적 프록시 모드
/**
 *   Runnable           
 * 	        
 * 		1.     
 * 		2.     :         
 * 		3.        
 * 	       
 * 		     --->     
 * 		     ---> Thread 
 * @author Administrator
 *
 */
public class StaticProxy {
	public static void main(String[] args) {
		You you = new You();//    
		MarryCompany mc = new MarryCompany(you);//    +      
		mc.marry();
	}
}
//    
interface Marry{
	public abstract void marry();
}
//    
class You implements Marry{

	@Override
	public void marry() {
		System.out.println("You and Your Lover Marrying");
	}
}
//    
class MarryCompany implements Marry{
	private Marry you;
	public MarryCompany() {

	}
	public MarryCompany(Marry you) {
		this.you = you;
	}
	private void before(){
		System.out.println("    ");
	}
	private void after(){
		System.out.println("    ");
	}
	@Override
	public void marry() {
		before();
		you.marry();
		after();
	}
	
}

2) 실현
/**
 *   Runnable        --->  Java              
 * 	     Runnable   +  run()  
 * 	            
 * 		      
 * 		      +       Thread 
 * 		  。start()
 * 	
 * @author Administrator
 *
 */
public class Programmer implements Runnable {

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println("   1:"+i);
		}
	}

}
public class ProgrammerApp {
	public static void main(String[] args) {
//		      
		Programmer pro = new Programmer();
//		       +      
		Thread porxy = new Thread(pro);
//		   。start()
		porxy.start();//  1
		//  2
		for (int i = 0; i < 10; i++) {
			System.out.println("   2:"+i);
		}
	}
}

3. Callable 인터페이스 구현
/**
 *     Callable       
 * 	1.      
 * 	2.      
 * @author Administrator
 *
 */
public class Callable01 {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		
		//        2
		ExecutorService ser = Executors.newFixedThreadPool(2);
		
		//  Callable   
		Race tortoise = new Race("  ",100L);//      
		Race rabbit = new Race("  ",50L);
		
		//   
		Future result1 = ser.submit(tortoise);
		Future result2 = ser.submit(rabbit);
		Thread.sleep(1000);
		tortoise.setFlag(false);
		rabbit.setFlag(false);
		
		int num1 = result1.get();
		int num2 = result2.get();
		System.out.println("    "+num1);
		System.out.println("    "+num2);
		
		
		
		//    
		ser.shutdownNow();
		
	}
}
//      call()      
class Race implements Callable{
	private String name;
	private long time;//  
	private int step;
	private boolean flag = true;
	public Race() {
		super();
	}

	public Race(String name,Long time) {
		super();
		this.name = name;
		this.time = time;
	}

	@Override
	public Integer call() throws Exception {
		while(flag){
			Thread.sleep(time);
			step++;
		}
		return step;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
		
	}

	public long getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	
}

좋은 웹페이지 즐겨찾기