ゞ沉默是金ゞ

鱼离不开水,但是没有说不离开哪滴水.
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 ゞ沉默是金ゞ 阅读(293) 评论(0)  编辑  收藏 所属分类: Java SE

只有注册用户登录后才能发表评论。


网站导航: