PolicyTest.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.sf.threadpool;
  2. import java.util.concurrent.Callable;
  3. import java.util.concurrent.LinkedBlockingDeque;
  4. import java.util.concurrent.ThreadPoolExecutor;
  5. import java.util.concurrent.TimeUnit;
  6. public class PolicyTest {
  7. public static void main(String[] args) {
  8. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
  9. 1, 3, 1024, TimeUnit.SECONDS,
  10. new LinkedBlockingDeque<>(1), new MyRejectedPolicy());
  11. threadPoolExecutor.setRejectedExecutionHandler(new MyRejectedPolicy());
  12. for (int i = 0; i < 5; i++) {
  13. threadPoolExecutor.execute(() -> {
  14. try {
  15. Thread.sleep(100000000L);
  16. } catch (InterruptedException e) {
  17. throw new RuntimeException(e);
  18. }
  19. });
  20. }
  21. // for (int i = 0; i < 5; i++) {
  22. // threadPoolExecutor.execute(new Runnable() {
  23. // @Override
  24. // public void run() {
  25. // try {
  26. // Thread.sleep(100000000L);
  27. // } catch (InterruptedException e) {
  28. // throw new RuntimeException(e);
  29. // }
  30. // }
  31. // });
  32. //
  33. // threadPoolExecutor.submit(new Callable<String>() {
  34. // @Override
  35. // public String call() throws Exception {
  36. // return "";
  37. // }
  38. // });
  39. // }
  40. }
  41. }