0%
java 多线程提交任务模板
介绍
java 中多线程批量处理任务的代码模板。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;
public class ThreadPoolExecutorDemo {
public static void main(String[] args) {
int threadCount = 0;
ThreadPoolExecutor executor = new ThreadPoolExecutor(threadCount <= 50 ? threadCount : 50, 50, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(2000));
try { String param = ""; ExecutorTask urlTask = new ExecutorTask(param); executor.execute(urlTask); } catch (Exception e) {
} finally { executor.shutdown(); }
}
}
class ExecutorTask implements Runnable {
private String param;
public ExecutorTask(String param) { this.param = param; }
@Override public void run() { System.out.println(param); }
}
|