🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #20746

[��]threadһ��

#20746
From: apollo
Date: Mon, 15 Dec 2003 14:02
124 lines
4005 bytes
��һ��threadpool�Ĵ���
import java.util.LinkedList;

public class ThreadPool
{
   static final long IDLE_TIMEOUT = 60000L;

   private String name;
   private int minsize;
   private int maxsize;
   private int nextWorkerId = 0;
   private LinkedList pool = new LinkedList();

   public ThreadPool() {
       this("PooledThread");
   }

   public ThreadPool(String name) {
       this(name, 0, 20);
   }

   public ThreadPool(String name, int minsize, int maxsize) {
       this.name = name;
       this.minsize = minsize;
       this.maxsize = maxsize;
   }

   public synchronized void run(Runnable runner) {
       Worker worker;

       if (runner == null) {
           throw new NullPointerException();
       }

       // get a worker from free list...
       if (!pool.isEmpty()) {
           worker = (Worker) pool.removeFirst();
       } else {
           // ...no free worker available, create new one...
           worker = new Worker(name + "-" + ++nextWorkerId);
           worker.start();
       }

       // ...and wake up worker to service incoming runner
       worker.wakeup(runner);
   }

   // Notified when a worker has idled timeout
   // @return true if worker should die, false otherwise
   synchronized boolean notifyTimeout(Worker worker) {
       if (worker.runner != null) {
           return false;
       }
       if (pool.size() > minsize) {
           // Remove from free list
           pool.remove(worker);
           return true; // die
       }
       return false; // continue
   }

   // Notified when a worker has finished his work and
   // free to service next runner
   // @return true if worker should die, false otherwise
   synchronized boolean notifyFree(Worker worker) {
       if (pool.size() < maxsize) {
           // Add to free list
           pool.addLast(worker);
           return false; // continue
       }
       return true; // die
   }

   // The inner class that implement worker thread
   class Worker extends Thread {
       Runnable runner = null;

       public Worker(String name) {
           super(name);
       }

       synchronized void wakeup(Runnable runner) {
           this.runner = runner;
           notify();
       }

       public void run() {
           for (;;) {
               synchronized (this) {
                   if (runner == null) {
                       try {
                           wait(IDLE_TIMEOUT);
                       } catch (InterruptedException e) {}
                   }
               }

               // idle timed out, die or put into free list
               if (runner == null) {
                    if (notifyTimeout(this))
                        return;
                    else
                        continue;
               }

               try {
                   runner.run();
               } finally {
                   runner = null;
                   if (notifyFree(this))
                       return;
               }
           }
       }
   }
}

����runner��ʲô

--
/configure
make
make install

�� ��Դ:��Ʈ��ˮ�Ƽ� Freecity.dhs.org��[FROM: apollow]
Technical Details
Message-ID: <49eZIh$AKT@Freecity.dhs.org>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-09!supernews.com!38.144.126.76.MISMATCH!feed2.newsreader.com!newsreader.com!maily.cic.tsinghua.edu.cn!news.ccie.net.cn!Freecity

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,GB
X-Google-Thread: f6cb0,8a2d9b31c68adff9,start
X-Google-Attributes: gidf6cb0,public
X-Google-ArrivalTime: 2003-12-15 06:02:27 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-09!supernews.com!38.144.126.76.MISMATCH!feed2.newsreader.com!newsreader.com!maily.cic.tsinghua.edu.cn!news.ccie.net.cn!Freecity
From: apollow.bbs@Freecity.dhs.org (apollo)
Newsgroups: cn.bbs.comp.lang.java
Subject: [��]threadһ��
Date: 15 Dec 2003 14:02:00 GMT
Organization: Ʈ��ˮ�Ƽ�
Message-ID: <49eZIh$AKT@Freecity.dhs.org>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: Java/M.1071496843.A