summaryrefslogtreecommitdiffstats
path: root/simple/simple-common/src/test/java/org/simpleframework/common/thread/TransientApplication.java
blob: 69941b2e9201af1d2f7cad95a71e2fe2cf6e4b57 (plain)
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
45
46
47
48
49
50
51
52
53
54
package org.simpleframework.common.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.simpleframework.common.thread.ConcurrentExecutor;

public class TransientApplication {

   public static void main(String[] list) throws Exception {
      BlockingQueue queue = new LinkedBlockingQueue();
      ConcurrentExecutor pool = new ConcurrentExecutor(TerminateTask.class, 10);      
      
      for(int i = 0; i < 50; i++) {
         pool.execute(new LongTask(queue, String.valueOf(i)));
      }
      pool.execute(new TerminateTask(pool));      
   }
   
   private static class TerminateTask implements Runnable {
      
      private ConcurrentExecutor pool;
      
      public TerminateTask(ConcurrentExecutor pool) {
         this.pool = pool;
      }
      
      public void run() {
         pool.stop();
      }
   }   
   
   private static class LongTask implements Runnable {
      
      private BlockingQueue queue;
      
      private String name;
      
      public LongTask(BlockingQueue queue, String name) {
         this.queue = queue;
         this.name = name;
      }
      
      public void run() {         
         try {            
            Thread.sleep(1000);
         } catch(Exception e) {
            e.printStackTrace();
         }         
         System.err.println(name);
         queue.offer(name);
      }
   }
}