随笔 - 8, 文章 - 0, 评论 - 6, 引用 - 0
数据加载中……

一个简化的java线程池示例

//以前写在blogger上的一篇老文了
曾经很好奇线程池是怎么实现的,.net中有现成的线程池可以使用,但是java中没有。还有就是Servlet的service方法是怎么样为每一个 Request在不同的线程中独立服务的,由于Servlet接口没有继承自Runnable接口,因此无法直接由一个Servlet对象生成多个线程。后来在网上找到了一个java版本的线程池的例子(http://www.informit.com/articles/article.asp?p= 30483&seqNum=1&rl=1)在该例子的基础上简化得到了下面这个版本的java线程池,记录在这里。
*******************
ThreadPool.java
*******************

package threadPool;

import java.util.ArrayList;
import java.util.Collection;

public class ThreadPool
{
    Thread[] threadArray;
    Collection
<Runnable> jobs = new ArrayList<Runnable>();
 
    
public ThreadPool(int threadNum)
    
{
        threadArray 
= new WorkerThread[threadNum];
        
for (Thread thread : threadArray)
        
{
            thread 
= new WorkerThread();
            thread.start();
        }

    }

 
    
public synchronized void addJob(Runnable job)
    
{
        jobs.add(job);
        notify();
    }

 
    
private synchronized Runnable getJob()
    
{
        
while(jobs.isEmpty())
        
{
            
try
            
{
                wait();
            }
 catch (InterruptedException e)
            
{
                e.printStackTrace();
            }

        }

        Runnable job 
=  jobs.iterator().next();
        jobs.remove(job);
        
return job;
    }

    
    
private class WorkerThread extends Thread
    
{
        
public void run()
        
{
            Runnable job 
= null;
            
while(job == null)
            
{
                job 
= getJob();
                
if(job != null)
                
{
                    job.run();
                }

                job 
= null;
            }

       }

    }

}


 

*******************
ThreadPoolTest.java
*******************

package threadPool;

public class ThreadTest
{
    
private static class PrintClass implements Runnable
    
{
        
private int threadNo;
  
        
public PrintClass(int threadNo)
        
{
            
this.threadNo = threadNo;
        }

        
        
public void run()
        
{
            
for(int i = 0; i < 10; i++)
            
{
                
synchronized (System.out)
                
{
                    System.out.println(
"Thread "+threadNo+""+i);
                }

                
try
                
{
                    Thread.sleep(
1000);
                }
 catch (InterruptedException e)
                
{
                    e.printStackTrace();
                }

            }

        }
 
    }

 
    
public static void main(String[] args)
    
{
        ThreadPool tp 
= new ThreadPool(3);
        
for(int i=0; i <10; i++)
        
{
            tp.addJob(
new PrintClass(i));
        }

        
synchronized (System.out)
        
{
            System.out.println(
"Job adding finished");
        }

    }

}

posted on 2007-05-15 12:53 Job Hu 阅读(946) 评论(3)  编辑  收藏

评论

# re: 一个简化的java线程池示例   

2007-05-16 18:20 | klvt

# re: 一个简化的java线程池示例  回复  更多评论   

扁担长板凳宽扁担想绑在板凳板凳不让扁担绑在板凳上扁担偏要绑在板凳板凳偏偏不让扁担绑在那板凳

2007-05-18 16:39 | klvt

# re: 一个简化的java线程池示例  回复  更多评论   

JDK1.5中已经提供线程池的支持了 java.util.concurrent 包
2007-06-15 15:41 | jasson

只有注册用户登录后才能发表评论。


网站导航: