Java并发编程两例

一、有一大批数据库的数据需要处理,每次取一千条。处理完后记录下,如果超时也不再等,再取下一个一千。以下给出处理这一千数据的方法 。
public class TestExecutorService {

    public static void main(String[] args) throws InterruptedException {
        //ExecutorService pool = Executors.newCachedThreadPool();
        ExecutorService pool = Executors.newFixedThreadPool(10);
       
        ArrayList<String> srcDatas =new ArrayList<String>();
        for(int i=0;i<1000;i++){
            srcDatas.add(""+i);   
        }       
        ArrayList<String> batchDatas =new ArrayList<String>();
       
        if(srcDatas.size() > 0){
            for(int i=0;i<srcDatas.size();i++){
                if (i % 10 == 0&&i!=0) {                           
                    pool.execute(new WorkThread(batchDatas));
                    batchDatas=new ArrayList<String>();
                }
                batchDatas.add(srcDatas.get(i));
            }
            pool.execute(new WorkThread(batchDatas));
        }               
        pool.shutdown();               
        pool.awaitTermination(100, TimeUnit.SECONDS);       

    }

}
class WorkThread extends Thread{
    private List<String> datas;
    public WorkThread(List<String> datas){
        this.datas=datas;
    }
    public void run(){
        System.out.println("do batchDatas"+datas);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {         
            e.printStackTrace();
        }
    }
}


二、有几个线程,使用一个方法来控制它们的开始动作。也是等到执行完后再执行其它的代码。代码基本来自于jdk.

public class TestCountDownLatch {

    public static void main(String args[]) throws InterruptedException {
        int N = 2;
        CountDownLatch startSignal = new CountDownLatch(1);
        CountDownLatch doneSignal = new CountDownLatch(N);

        for (int i = 0; i < N; ++i) { // create and start threads
            new Thread(new Worker(startSignal, doneSignal)).start();
        }

        doSomethingElse("before"); // don’t let run yet
        startSignal.countDown(); // let all threads proceed
        doSomethingElse("after");
        doneSignal.await(); // wait for all to finish
    }

    public static void doSomethingElse(String key) {
        System.out.println(key+" doSomethingElse");
    }

   

}
 class Worker implements Runnable {
    private final CountDownLatch startSignal;
    private final CountDownLatch doneSignal;

    Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
        this.startSignal = startSignal;
        this.doneSignal = doneSignal;
    }

    public void run() {
        try {
            startSignal.await();
            doWork();
            doneSignal.countDown();
        } catch (InterruptedException ex) {
        } // return;
    }

    void doWork() {
        System.out.println("doWork...");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
} 

文/程忠 浏览次数:0次   2016-10-11 18:39:16

相关阅读

微信扫描-捐赠支持
加入QQ群-技术交流

评论: