Java Concurrent处理并发需求

4
Java IDE C/C++ 18213 次浏览
java5开始,增加了concurrent api,用于并发处理。比如起多个线程并发从网络上下载图片,然后在本地显示。
这里写个简单的代码,来说明如何使用concurrent api提供的线程连接池。
运行结果类似这样:
start do 1 task …      
>>main thread end.      
start do 2 task …      
start do 1 finished.      
start do 3 task …      
start do 2 finished.      
start do 3 finished.
这里的task1到task3,都做的同样的事情,让它所属的线程休眠2000ms:
private static void doSomething(int id) {      
    System.out.println("start do " + id + " task …");      
    try {      
        Thread.sleep(1000 * 2);      
    } catch (InterruptedException e) {      
        e.printStackTrace();      
    }      
    System.out.println("start do " + id + " finished.");      
}

如果没有java自带的这个api,需要自己或者使用不标准的第三方线程池api。用concurrent api写起来很简洁:
public static void main(String[] args) {      
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    executorService.submit(new Runnable() {      
        @Override      
        public void run() {      
            doSomething(1);      
        }      
    });
    executorService.submit(new Runnable() {      
        @Override      
        public void run() {      
            doSomething(2);      
        }      
    });
    executorService.submit(new Runnable() {      
        @Override      
        public void run() {      
            doSomething(3);      
        }      
    });
    executorService.shutdown();      
    System.out.println(">>main thread end.");
}
首先,创建了一个线程池,里面有2个线程:
ExecutorService executorService = Executors.newFixedThreadPool(2);
然后,通过submit()方法,提交一个Runnable的实例,这个实例将交由线程池中空闲的线程执行。
在main线程中直接运行了:
executorService.shutdown();
不必担心,线程池不会直接关闭的,只有当它执行完所有提交的任务后才会关闭。如果不写这行,在本例中,应用将不会停止,因为虽然main线程(就是运行main方法的线程,也叫主线程)退出了,但是线程池中依然有线程运行,因此应用(进程)不会退出。
完整代码:
package com.easymorse.concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo {

        /**
         * @param args
         */
        public static void main(String[] args) {
                ExecutorService executorService = Executors.newFixedThreadPool(2);

                executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                                doSomething(1);
                        }
                });

                executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                                doSomething(2);
                        }
                });

                executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                                doSomething(3);
                        }
                });

                executorService.shutdown();
                System.out.println(">>main thread end.");

        }

        private static void doSomething(int id) {
                System.out.println("start do " + id + " task ...");
                try {
                        Thread.sleep(1000 * 2);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
                System.out.println("start do " + id + " finished.");
        }

}
请尽量让自己的答案能够对别人有帮助

7个答案

默认排序 按投票排序