学习笔记―java线程池模拟

一、知识点
1、线程常用的方法
synchronized 同步代码块,用于同步操作,java5提供也了新的方法,等后面再说
wait 线程主动释放对象锁,所以wait在synchronized内部使用,当前线程阻塞,知道notify方法唤醒
sleep 则不会释放锁
2、锁对象
如果是静态方法则是类的字节码对象,只有同一个对象才能锁住
3、线程池的实现
以下代码并未使用Java提供的线程池,而是自行组装的
首先提供一个线程类,用于主线程创建固定的线程数,然后巧妙的使用Verctor作为队列
线程类中提供构造方法使其传入队列,主线程通过notifyAll方法唤醒中因wait而休眠的线程
线程类中定义静态方法,记录已完成的数目
线程类中有两部分需要同步:
完成队列数,用于判断是否全部任务已经完成
从队列中取数据时需要线程安全,因为所以线程操作的是同一个队列
二、部分参考代码
 
 
  
  
  
  
  1. public class SimplePoolThread extends Thread{ 
  2.      
  3.     // 线程中需要处理的队列,通过构造方法传入 
  4.     private List pool; 
  5.     public SimplePoolThread(List pool){ 
  6.         this.pool = pool; 
  7.     } 
  8.      
  9.     // 标记已经完成的 
  10.     // 这个是静态的,所以线程池中共用此数据 
  11.     private static int finish = 0
  12.     // 此处加锁的对象是类的字节码对象 
  13.     private static synchronized void addFinish(){ 
  14.         finish ++; 
  15.     } 
  16.      
  17.     @Override 
  18.     public void run() { 
  19.         // 如果没有完成,那么run方法一直运行 
  20.         while(finish != SimpleDemo.getNumber()){ 
  21.             File input = null
  22.              
  23.             // 此处为去队列部分,所以需要线程安全 
  24.             synchronized(pool){ 
  25.                 // 处理的数据为空 
  26.                 while(pool.isEmpty()){ 
  27.                     // 为空且相等,说明还未有队列进入线程池或队列已经处理完毕 
  28.                     if(finish == SimpleDemo.getNumber()){ 
  29.                         System.out.println("Thread ending"); 
  30.                         return
  31.                     } 
  32.                     // 如果不是,那么等待 
  33.                     try { 
  34.                         pool.wait(); 
  35.                     } catch (InterruptedException e) { 
  36.                     } 
  37.                 } 
  38.                  
  39.                 // 队列不为空,那么从后面开始处理 
  40.                 input = (File) pool.remove(pool.size()-1); 
  41.                 addFinish(); 
  42.             } 
  43.              
  44.             // 业务处理部分,不会受多线程影响了 
  45.             if(!input.getName().endsWith(".gz")){ 
  46.                 try { 
  47.                     InputStream in = new FileInputStream(input); 
  48.                     in = new BufferedInputStream(in); 
  49.                     File output = new File(input.getParent(),input.getName()+".gz"); 
  50.                     if(!output.exists()){ 
  51.                         OutputStream out = new FileOutputStream(output); 
  52.                         out = new BufferedOutputStream(new GZIPOutputStream(out)); 
  53.                         int b; 
  54.                         while((b = in.read())!=-1){ 
  55.                             out.write(b); 
  56.                         } 
  57.                         out.flush(); 
  58.                         out.close(); 
  59.                         in.close(); 
  60.                     } 
  61.                 } catch (Exception e) { 
  62.                     e.printStackTrace(); 
  63.                 } 
  64.             } 
  65.         } 
  66.     } 
  67.      
 
  
  
  
  
  1. public final static int count = 4
  2.     // 需要处理的队列数 
  3.     private static int number = -1
  4.     public static int getNumber() { 
  5.         return number; 
  6.     } 
  7.      
  8.     public static void main(String[] args) { 
  9.         Vector pool = new Vector(); 
  10.         // 创建线程池 
  11.         SimplePoolThread[] threads = new SimplePoolThread[count]; 
  12.          
  13.         for(int i=0;i<threads.length;i++){ 
  14.             threads[i] = new SimplePoolThread(pool); 
  15.             threads[i].start(); 
  16.         } 
  17.          
  18.         // 临时计数 
  19.         int totalFiles = 0
  20.         File f = new File("."); 
  21.         if(f.exists()){ 
  22.             if(f.isDirectory()){ 
  23.                 File[] files = f.listFiles(); 
  24.                 for(int j=0;j<files.length;j++){ 
  25.                     if(!files[j].isDirectory()){ 
  26.                         totalFiles++; 
  27.                         // 往线程池队列中添加数据,并更新线程池 
  28.                         synchronized (pool){ 
  29.                             pool.add(0,files[j]); 
  30.                             pool.notifyAll(); 
  31.                         } 
  32.                     } 
  33.                 } 
  34.             }else
  35.                 totalFiles++; 
  36.                 synchronized(pool){ 
  37.                     pool.add(0,f); 
  38.                     pool.notifyAll(); 
  39.                 } 
  40.             } 
  41.         } 
  42.          
  43.         // 队列添加结束 
  44.         number = totalFiles; 
  45.          
  46.         // 关闭空闲的进程 
  47.         for(int i=0;i<threads.length;i++){ 
  48.             threads[i].interrupt(); 
  49.         } 
  50.     } 
 

你可能感兴趣的:(java,线程池)