fork(1) download
  1. import java.util.concurrent.Callable;
  2. import java.util.concurrent.CountDownLatch;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.Future;
  6.  
  7. public class Main {
  8.  
  9. static enum Some {
  10. FOO;
  11. }
  12.  
  13. static abstract class Foo {
  14. public abstract Some getType();
  15. }
  16.  
  17. static class FooExt extends Foo {
  18. @Override
  19. public Some getType() {
  20. return Some.FOO;
  21. }
  22. }
  23.  
  24. public static void main(String[] args) {
  25.  
  26. ExecutorService service = Executors.newFixedThreadPool(2);
  27. final CountDownLatch start = new CountDownLatch(1);
  28. Future<Integer> f1 = service.submit(new Callable<Integer>() {
  29. @Override
  30. public Integer call() {
  31. try {
  32. start.await();
  33. } catch (InterruptedException e) {
  34. e.printStackTrace();
  35. }
  36. System.out.println("Task started...");
  37. int a = 0;
  38. Foo foo = new FooExt();
  39. while (!Thread.currentThread().isInterrupted()) {
  40. if (foo instanceof FooExt) {
  41. a++;
  42. }
  43. }
  44. System.out.println("Task ended...");
  45. return a;
  46. }
  47. });
  48.  
  49. Future<Integer> f2 = service.submit(new Callable<Integer>() {
  50. @Override
  51. public Integer call() {
  52. try {
  53. start.await();
  54. } catch (InterruptedException e) {
  55. e.printStackTrace();
  56. }
  57. System.out.println("Task started...");
  58. int a = 0;
  59. Foo foo = new FooExt();
  60. while (!Thread.currentThread().isInterrupted()) {
  61. if (foo.getType() == Some.FOO) {
  62. a++;
  63. }
  64. }
  65. System.out.println("Task ended...");
  66. return a;
  67. }
  68. });
  69. start.countDown();
  70. try {
  71. Thread.sleep(100);
  72. } catch (InterruptedException e) {
  73. }
  74. service.shutdownNow();
  75. System.out.println("service is shutdowned...");
  76. try {
  77. System.out.println("instanceof: "+f1.get());
  78. System.out.println("enum: "+f2.get());
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82. }
  83.  
  84. }
Success #stdin #stdout 0.2s 380992KB
stdin
Standard input is empty
stdout
Task started...
Task started...
service is shutdowned...
Task ended...
Task ended...
instanceof: 710866
enum: 780185