KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > util > threadpool > ThreadPool


1 package org.jacorb.util.threadpool;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.util.*;
25
26 /**
27  * ThreadPool.java
28  *
29  *
30  * Created: Fri Jun 9 15:09:01 2000
31  *
32  * @author Nicolas Noffke
33  * $Id: ThreadPool.java,v 1.13 2004/12/11 21:12:13 andre.spiegel Exp $
34  */

35 public class ThreadPool
36 {
37     private int max_threads = 0;
38     private int max_idle_threads = 0;
39
40     private int total_threads = 0;
41     private int idle_threads = 0;
42
43     private LinkedList job_queue = null;
44     private ConsumerFactory factory = null;
45
46     public ThreadPool( ConsumerFactory factory )
47     {
48         this( new LinkedList (),
49               factory,
50               10,
51               10 );
52     }
53
54     public ThreadPool( ConsumerFactory factory,
55                        int max_threads,
56                        int max_idle_threads)
57     {
58         this
59         (
60             new LinkedList (),
61             factory,
62             max_threads,
63             max_idle_threads
64         );
65     }
66
67     private ThreadPool( LinkedList job_queue,
68                         ConsumerFactory factory,
69                         int max_threads,
70                         int max_idle_threads)
71     {
72         this.job_queue = job_queue;
73         this.factory = factory;
74         this.max_threads = max_threads;
75         this.max_idle_threads = max_idle_threads;
76     }
77
78     protected synchronized Object JavaDoc getJob()
79     {
80         /*
81          * This tells the newly idle thread to exit,
82          * because there are already too much idle
83          * threads.
84          */

85         if (idle_threads >= max_idle_threads)
86         {
87             total_threads--;
88             return null;
89         }
90
91         idle_threads++;
92
93         while( job_queue.isEmpty() )
94         {
95             try
96             {
97                 wait();
98             }
99             catch( InterruptedException JavaDoc e )
100             {
101             }
102         }
103
104         return job_queue.removeFirst();
105     }
106
107     public synchronized void putJob( Object JavaDoc job )
108     {
109         job_queue.add(job);
110         notifyAll();
111
112         // yes it is possible to have a negative value, look further
113
if(idle_threads <= 0)
114         {
115             if(total_threads < max_threads)
116             {
117                 createNewThread();
118
119                 // reserve the new thread for this job, not idle anymore
120
// can become negative, will be immediately reincremented in getJob()
121
idle_threads--;
122             }
123         }
124         else
125         {
126             // reserve a thread for this job, not idle anymore
127
// can become negative, will be immediately reincremented in getJob()
128
idle_threads--;
129         }
130     }
131
132     private void createNewThread()
133     {
134         Thread JavaDoc t =
135             new Thread JavaDoc( new ConsumerTie( this, factory.create() ));
136         t.setDaemon( true );
137         t.start();
138
139         total_threads++;
140     }
141 } // ThreadPool
142
Popular Tags