<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 ゞ沉默是金ゞ 閱讀(314) 評論(0)  編輯  收藏 所屬分類: Java SE
    主站蜘蛛池模板: 久久久久se色偷偷亚洲精品av| 美女视频黄的全免费视频| 国产啪精品视频网免费| 久久精品国产亚洲av四虎| 免费无码婬片aaa直播表情| 国产亚洲成AV人片在线观黄桃| 永久在线免费观看| 美女视频黄a视频全免费网站色 | 免费a级毛片在线观看| aa在线免费观看| 亚洲二区在线视频| 亚洲日韩VA无码中文字幕| 日韩精品亚洲专区在线影视| 亚洲日韩精品无码专区网址| a国产成人免费视频| 亚洲精品国自产拍在线观看| 永久免费无码网站在线观看个| 亚洲AV无码一区二三区| 水蜜桃视频在线观看免费| 亚洲丝袜美腿视频| 亚洲XX00视频| 黄瓜视频影院在线观看免费| 成年女人A毛片免费视频| 亚洲中文字幕无码亚洲成A人片| 国产亚洲av人片在线观看| 午夜神器成在线人成在线人免费| 国产免费拔擦拔擦8X高清在线人 | 哒哒哒免费视频观看在线www| 无码精品国产一区二区三区免费 | 成人AV免费网址在线观看| 91视频精品全国免费观看| 亚洲AV无码一区二区二三区软件| 热久久精品免费视频| 国产精品无码亚洲一区二区三区| 亚洲专区在线视频| 中文字幕中韩乱码亚洲大片| 宅男666在线永久免费观看| 免费人人潮人人爽一区二区| 亚洲av永久无码精品天堂久久 | 免费av欧美国产在钱| 久久午夜夜伦鲁鲁片免费无码 |