<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
    主站蜘蛛池模板: 亚洲国产第一站精品蜜芽| 久久久久久久亚洲精品| av成人免费电影| 亚洲伊人久久大香线蕉在观| 激情综合色五月丁香六月亚洲| 无码人妻久久一区二区三区免费丨| 9i9精品国产免费久久| 女bbbbxxxx另类亚洲| 在线综合亚洲中文精品| 婷婷精品国产亚洲AV麻豆不片| 99久久99久久精品免费看蜜桃 | 日韩精品无码免费专区午夜| 国产精品亚洲а∨天堂2021| 亚洲日本久久一区二区va| 亚洲久本草在线中文字幕| 国产亚洲AV手机在线观看| 国产zzjjzzjj视频全免费| 欧美男同gv免费网站观看| 亚洲成年人免费网站| 亚洲精品中文字幕| 亚洲av乱码一区二区三区香蕉| 国产jizzjizz视频全部免费| 9久9久女女免费精品视频在线观看| 久久免费精彩视频| 在线观看特色大片免费网站| a级毛片免费高清视频| www免费插插视频| 四虎国产精品成人免费久久| 在线视频亚洲一区| 鲁啊鲁在线视频免费播放| 大桥未久亚洲无av码在线| 亚洲国产午夜精品理论片在线播放 | 亚洲综合一区二区精品导航| 亚洲av无码一区二区三区网站| 精品国产亚洲一区二区在线观看| 日韩免费视频一区| 四虎永久免费地址在线网站| 在线观着免费观看国产黄| 国产区卡一卡二卡三乱码免费| 国产免费人视频在线观看免费| 国产免费卡一卡三卡乱码|