Home | History | Annotate | Download | only in support
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package tests.support;
     18 
     19 import java.util.LinkedList;
     20 import java.util.logging.Logger;
     21 
     22 public class ThreadPool extends ThreadGroup {
     23 
     24     private boolean isAlive;
     25 
     26     private LinkedList<Runnable> taskQueue;
     27 
     28     private int threadID;
     29 
     30     private static int threadPoolID;
     31 
     32     public ThreadPool(int numThreads) {
     33         super("ThreadPool-" + (threadPoolID++));
     34         setDaemon(true);
     35 
     36         isAlive = true;
     37 
     38         taskQueue = new LinkedList<Runnable>();
     39         for (int i = 0; i < numThreads; i++) {
     40             new PooledThread().start();
     41         }
     42     }
     43 
     44     public synchronized void runTask(Runnable task) {
     45         if (!isAlive) {
     46             throw new IllegalStateException();
     47         }
     48         if (task != null) {
     49             taskQueue.add(task);
     50             notify();
     51         }
     52     }
     53 
     54     protected synchronized Runnable getTask() throws InterruptedException {
     55         while (taskQueue.size() == 0) {
     56             if (!isAlive) {
     57                 return null;
     58             }
     59             wait();
     60         }
     61         Logger.global.info("1 Task is removed");
     62         return (Runnable) taskQueue.removeFirst();
     63     }
     64 
     65     public synchronized void close() {
     66         if (isAlive) {
     67             isAlive = false;
     68             taskQueue.clear();
     69             interrupt();
     70         }
     71     }
     72 
     73     public void join() {
     74         synchronized (this) {
     75             isAlive = false;
     76             notifyAll();
     77         }
     78 
     79         Thread[] threads = new Thread[activeCount()];
     80         int count = enumerate(threads);
     81         for (int i = 0; i < count; i++) {
     82             try {
     83                 threads[i].join();
     84             } catch (InterruptedException ex) {
     85                 System.err.println(ex.getMessage());
     86             }
     87         }
     88     }
     89 
     90     private class PooledThread extends Thread {
     91 
     92         public PooledThread() {
     93             super(ThreadPool.this, "PooledThread-" + (threadID++));
     94         }
     95 
     96         public void run() {
     97             while (!isInterrupted()) {
     98 
     99                 Runnable task = null;
    100                 try {
    101                     task = getTask();
    102                 } catch (InterruptedException ex) {
    103                     System.err.println(ex.getMessage());
    104                 }
    105 
    106                 if (task == null) {
    107                     return;
    108                 }
    109 
    110                 try {
    111                     Logger.global.info("Task is run");
    112                     task.run();
    113 
    114                 } catch (Throwable t) {
    115                     System.err.println(t.getMessage());
    116                 }
    117             }
    118         }
    119     }
    120 }
    121