Java 다중 스레드 - 3가지 실현 방식
                                            
 4078 단어  대상 요약Java 다중 스레드
                    
/**
 *     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;
	}
	
}     이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
메인 스레드 제어 서브스레드 운행 완료주 스레드 제어 10개 하위 스레드 운행 완료 후 출력, 주요 지식 포인트countDownLunch 코드는 다음과 같이 간단합니다. 서브스레드:countDownLatch로 전송 효과는 다음과 같습니다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.