<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ゞ沉默是金ゞ

    魚離不開水,但是沒有說不離開哪滴水.
    posts - 98,comments - 104,trackbacks - 0
    1. use ThreadPoolExecutor to do some data work, the thread number is solid. for example:storing data into cache, process large mount of data.
    import java.util.Properties;
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;

    public class Controller1 {

        
    public static void main(String[] args) {
            
    // initial parameters
            int iNumberOfThreads = 5;
            Properties prop 
    = new Properties();

            
    // start thread running
            BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(100,
                    
    true);
            ThreadPoolExecutor executor 
    = new ThreadPoolExecutor(
                    iNumberOfThreads, 
    // core size
                    iNumberOfThreads, // max size
                    1// keep alive time
                    TimeUnit.MINUTES, // keep alive time units
                    queue);
            
    for (int k = 0; k < iNumberOfThreads; k++{
                SingleThread1 ct 
    = new SingleThread1(k, prop);
                executor.execute(ct);
            }

            
            System.out.println(
    "Expired Active threads = " + executor.getActiveCount());
            executor.shutdown();
            
    try {
                
    boolean isEnd = false;
                
    while (!isEnd) {
                    isEnd 
    = executor.awaitTermination(10000,
                            TimeUnit.SECONDS);
                }


            }
     catch (InterruptedException e) {
                e.printStackTrace();
            }

        }


    }


    import java.util.Properties;

    public class SingleThread1 implements Runnable {
        
    int threadIdx;
        Properties prop;
        
        
    public SingleThread1(int threadIdx, Properties prop) {
            
    super();
            
    this.threadIdx = threadIdx;
            
    this.prop = prop;
        }


        @Override
        
    public void run() {
            
    int i = 0;
            
            
    while(i < 10){
                System.out.println(
    "Thread " + threadIdx + " output number " + i + ".");
                i 
    ++;
            }

        }


    }


    2. use ThreadPoolExecutor do some work in limited time, for example: load test.
    import java.util.Properties;
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;

    public class Controller2 {
        
    static long testStartTimer = 0;
        
    static int iDurationSecs = 0;

        
    /**
         * 
    @param args
         
    */

        
    public static void main(String[] args) {
            
    // initial parameters
            int iNumberOfThreads = 10;
            Properties prop 
    = new Properties();
            testStartTimer 
    = System.currentTimeMillis();
            iDurationSecs 
    = 10;
            
            
    // start thread running
            BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(100,
                    
    true);
            ThreadPoolExecutor executor 
    = new ThreadPoolExecutor(
                    iNumberOfThreads, 
    // core size
                    iNumberOfThreads, // max size
                    1// keep alive time
                    TimeUnit.MINUTES, // keep alive time units
                    queue);
            
    int i = 0;
            
    while(!timerExpired()){
                
    if (executor.getActiveCount() >= iNumberOfThreads) {
                    
    // build the system command we want to run
                    try {
                        Thread.sleep(
    100);
                        
    continue;
                    }
     catch (InterruptedException e) {
                        
    // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

                SingleThread2 ct 
    = new SingleThread2(i, prop);
                executor.execute(ct);
                i 
    ++;
            }

            
            System.out.println(
    "Expired Active threads = " + executor.getActiveCount());
            executor.shutdown();
            
    try {
                
    int waittime = iNumberOfThreads + 10;
                executor.awaitTermination(waittime, TimeUnit.SECONDS);
            }
     catch (InterruptedException e) {
                e.printStackTrace();
            }

        }


        
    public static boolean timerExpired() {
            Long now 
    = System.currentTimeMillis();
            
    if ((now - testStartTimer) > (iDurationSecs * 1000))
                
    return true;
            
    else
                
    return false;
        }

    }

    import java.util.Properties;

    public class SingleThread2 implements Runnable {
        Properties prop;
        
    int threadId;
        
        
    public SingleThread2(int threadId, Properties prop) {
            
    super();
            
    this.threadId = threadId;
            
    this.prop = prop;
        }


        
    int i = 0;
        @Override
        
    public void run() {
            System.out.println(
    "SingleThread2 " + threadId + " output number " + i + ".");
            i
    ++;
        }


    }








    posted on 2012-11-27 13:43 ゞ沉默是金ゞ 閱讀(313) 評論(0)  編輯  收藏 所屬分類: Java SE
    主站蜘蛛池模板: 国产成人精品免费视频动漫 | 成人免费视频软件网站| 亚洲美女又黄又爽在线观看| 老外毛片免费视频播放| 免费高清小黄站在线观看 | 国产精品99久久免费| 亚洲乱码日产精品一二三| 欧美大尺寸SUV免费| 亚洲欧美国产欧美色欲| 国产精品深夜福利免费观看| 欧美亚洲精品一区二区| 又粗又大又猛又爽免费视频| 免费观看又污又黄在线观看| 亚洲情a成黄在线观看| 中文字幕看片在线a免费| 亚洲精品无码MV在线观看| 麻豆精品不卡国产免费看| 亚洲最大视频网站| 成人无遮挡裸免费视频在线观看| 亚洲精品无码mⅴ在线观看| 日本午夜免费福利视频| h视频免费高清在线观看| 亚洲精品无码永久中文字幕| 免费无码一区二区三区| 亚洲偷偷自拍高清| 日韩亚洲国产二区| 国产免费爽爽视频在线观看| 亚洲精品中文字幕无码AV| 在线观着免费观看国产黄| 美女网站在线观看视频免费的| 亚洲视频.com| 在线免费观看污网站| 成人A毛片免费观看网站| 亚洲综合小说久久另类区| 国产男女猛烈无遮档免费视频网站| 人禽伦免费交视频播放| 久久久婷婷五月亚洲97号色| 免费鲁丝片一级在线观看| 一个人看的www免费视频在线观看| 亚洲一区二区三区深夜天堂| 亚洲人成人网站在线观看|