자바 병렬 프로 그래 밍 (15) 스 레 드 탱크

3543 단어 JavaConcurrency
계속...
package com.dason.juc2;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/*
 *  、   :         ,               。            ,        。
 * 
 *  、        :
 * 	java.util.concurrent.Executor :               
 * 		|--**ExecutorService    :         
 * 			|--ThreadPoolExecutor        
 * 			|--ScheduledExecutorService    :       
 * 				|--ScheduledThreadPoolExecutor :   ThreadPoolExecutor,    ScheduledExecutorService
 * 
 *  、    : Executors 
 * ExecutorService newFixedThreadPool() :           
 * ExecutorService newCachedThreadPool() :      ,         ,             。
 * ExecutorService newSingleThreadExecutor() :        。          
 * 
 * ScheduledExecutorService newScheduledThreadPool() :          ,            。
 */
public class TestThreadPool {
	
	public static void main(String[] args) throws Exception {
//		//1.      
		ExecutorService pool = Executors.newFixedThreadPool(5);
//		ThreadPoolDemo tpd = new ThreadPoolDemo();
//		
//		//2.             
//		for (int i = 0; i < 10; i++) {
//			pool.submit(tpd);
//		}
//		
//		//3.      
//		pool.shutdown();
		
		List> list = new ArrayList<>();
		
		for (int i = 0; i < 10; i++) {
			Future future = pool.submit(new Callable(){

				@Override
				public Integer call() throws Exception {
					int sum = 0;
					
					for (int i = 0; i <= 100; i++) {
						sum += i;
					}
					
					return sum;
				}
				
			});

			list.add(future);
		}
		
		pool.shutdown();
		
		for (Future future : list) {
			System.out.println(future.get());
		}
		
	}
	

}

class ThreadPoolDemo implements Runnable{

	private int i = 0;
	
	@Override
	public void run() {
		while(i <= 100){
			System.out.println(Thread.currentThread().getName() + " : " + i++);
		}
	}
	
}
package com.dason.juc2;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/*
 *  、   :         ,               。            ,        。
 * 
 *  、        :
 * 	java.util.concurrent.Executor :               
 * 		|--**ExecutorService    :         
 * 			|--ThreadPoolExecutor        
 * 			|--ScheduledExecutorService    :       
 * 				|--ScheduledThreadPoolExecutor :   ThreadPoolExecutor,    ScheduledExecutorService
 * 
 *  、    : Executors 
 * ExecutorService newFixedThreadPool() :           
 * ExecutorService newCachedThreadPool() :      ,         ,             。
 * ExecutorService newSingleThreadExecutor() :        。          
 * 
 * ScheduledExecutorService newScheduledThreadPool() :          ,            。
 */
public class TestScheduledThreadPool {

	public static void main(String[] args) throws Exception {
		ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
		
		for (int i = 0; i < 5; i++) {
			Future result = pool.schedule(new Callable(){

				@Override
				public Integer call() throws Exception {
					int num = new Random().nextInt(100);//     
					System.out.println(Thread.currentThread().getName() + " : " + num);
					return num;
				}
				
			}, 1, TimeUnit.SECONDS);
			
			System.out.println(result.get());
		}
		
		pool.shutdown();
	}
	
}

좋은 웹페이지 즐겨찾기