ExecutorService,另一种服务,线程

http://heipark.iteye.com/blog/1393847

Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得      

    博客分类:
  • Java
 

newFixedThreadPool使用范例:

  1. import java.io.IOException;
  2. import java.util.concurrent.ExecutorService;
  3. import java.util.concurrent.Executors;
  4. public class Test {
  5. public static void main(String[] args) throws IOException, InterruptedException {
  6. ExecutorService service = Executors.newFixedThreadPool(2);
  7. for (int i = 0; i < 6; i++) {
  8. final int index = i;
  9. System.out.println("task: " + (i+1));
  10. Runnable run = new Runnable() {
  11. @Override
  12. public void run() {
  13. System.out.println("thread start" + index);
  14. try {
  15. Thread.sleep(Long.MAX_VALUE);
  16. } catch (InterruptedException e) {
  17. e.printStackTrace();
  18. }
  19. System.out.println("thread end" + index);
  20. }
  21. };
  22. service.execute(run);
  23. }
  24. }
  25. }
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class Test { public static void main(String[] args) throws IOException, InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(2);
for (int i = 0; i < 6; i++) {
final int index = i;
System.out.println("task: " + (i+1));
Runnable run = new Runnable() {
@Override
public void run() {
System.out.println("thread start" + index);
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread end" + index);
}
};
service.execute(run);
}
}
}
 输出:
task: 1

task: 2

thread start0

task: 3

task: 4

task: 5

task: 6

task: 7

thread start1

task: 8

task: 9

task: 10

task: 11

task: 12

task: 13

task: 14

task: 15

从实例可以看到for循环并没有被固定的线程池阻塞住,也就是说所有的线程task都被提交到了ExecutorService中,查看 Executors.newFixedThreadPool()如下:

public static ExecutorService newFixedThreadPool(int nThreads) {

        return new ThreadPoolExecutor(nThreads, nThreads,

                                      0L, TimeUnit.MILLISECONDS,

                                      new LinkedBlockingQueue<Runnable>());

    }

可以看到task被提交都了LinkedBlockingQueue中。这里有个问题,如果任务列表很大,一定会把内存撑爆,如何解决?看下面:

  1. import java.io.IOException;
  2. import java.util.concurrent.ArrayBlockingQueue;
  3. import java.util.concurrent.BlockingQueue;
  4. import java.util.concurrent.ThreadPoolExecutor;
  5. import java.util.concurrent.TimeUnit;
  6. public class Test {
  7. public static void main(String[] args) throws IOException, InterruptedException {
  8. BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(3);
  9. ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 3, 1, TimeUnit.HOURS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
  10. for (int i = 0; i < 10; i++) {
  11. final int index = i;
  12. System.out.println("task: " + (index+1));
  13. Runnable run = new Runnable() {
  14. @Override
  15. public void run() {
  16. System.out.println("thread start" + (index+1));
  17. try {
  18. Thread.sleep(Long.MAX_VALUE);
  19. } catch (InterruptedException e) {
  20. e.printStackTrace();
  21. }
  22. System.out.println("thread end" + (index+1));
  23. }
  24. };
  25. executor.execute(run);
  26. }
  27. }
  28. }
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; public class Test { public static void main(String[] args) throws IOException, InterruptedException { BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(3); ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 3, 1, TimeUnit.HOURS, queue, new ThreadPoolExecutor.CallerRunsPolicy()); for (int i = 0; i < 10; i++) {
final int index = i;
System.out.println("task: " + (index+1));
Runnable run = new Runnable() {
@Override
public void run() {
System.out.println("thread start" + (index+1));
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread end" + (index+1));
}
};
executor.execute(run);
}
}
}
 输出:
task: 1

task: 2

thread start1

task: 3

task: 4

task: 5

task: 6

task: 7

thread start2

thread start7

thread start6

线程池最大值为4(??这里我不明白为什么是设置值+1,即3+1,而不是3),准备执行的任务队列为3。可以看到for循环先处理4个task,然后把3个放到队列。这样就实现了自动阻塞队列的效果。记得要使用ArrayBlockingQueue这个队列,然后设置容量就OK了。

--heipark

上一篇:Eclipse修改Tomcat发布路径以及的配置多个Tomcat方法


下一篇:eclipse配置tomcat后修改server.xml文件(如编码等)无效问题