java 多线程打印1到75个数

ava线程(使用线程池)交替打印1到75个数的两种解法

  3153人阅读  评论(2)  收藏  举报
  分类:
Java(9) 

解法一:

[java]  view plain  copy
  1. 还有另外一个解法:  
  2. package net.liuyx.test;    
  3.     
  4. import java.util.ArrayList;    
  5. import java.util.List;    
  6. import java.util.concurrent.ExecutorService;    
  7. import java.util.concurrent.Executors;    
  8. import java.util.concurrent.locks.Condition;    
  9. import java.util.concurrent.locks.Lock;    
  10. import java.util.concurrent.locks.ReentrantLock;    
  11.     
  12. public class Test {    
  13.     private static final int TASK_NUM = 3;  
  14.     private static int num = 0;    
  15.     private static int flag = 0;    
  16.     private static Lock lock = new ReentrantLock();    
  17.     private static List list = new ArrayList();    
  18.     private static ExecutorService exec = Executors.newCachedThreadPool();    
  19.     static {    
  20.         for(int i = 0; i < TASK_NUM; i++){  
  21.             list.add(lock.newCondition());  
  22.         }  
  23.     }    
  24.     
  25.     private static void crit() {    
  26.         if (num >= 75) {    
  27.             System.exit(1);    
  28.         }    
  29.     }    
  30.     
  31.     private static void print() {    
  32.         crit();    
  33.         System.out.print(Thread.currentThread());    
  34.         for (int i = 0; i < 5; i++) {    
  35.             System.out.format("%-2d ", ++num);    
  36.         }    
  37.         System.out.println();    
  38.     }    
  39.     
  40.     private static void work(int i) {    
  41.             while (!Thread.interrupted()) {    
  42.                 try{    
  43.                     lock.lock();    
  44.                     if(flag == i){    
  45.                         crit();    
  46.                         print();    
  47.                         flag = (i + 1) % list.size();    
  48.                         list.get((i+1)%list.size()).signal();    
  49.                     }else{    
  50.                         try {    
  51.                             list.get(i%list.size()).await();    
  52.                         } catch (InterruptedException e) {    
  53.                             e.printStackTrace();    
  54.                         }    
  55.                     }    
  56.                 }finally{    
  57.                     lock.unlock();    
  58.                 }    
  59.             }    
  60.     }    
  61.     
  62.     private static class Task implements Runnable {    
  63.         private final int i;    
  64.     
  65.         public Task(int i) {    
  66.             this.i = i;    
  67.         }    
  68.     
  69.         @Override    
  70.         public void run() {    
  71.             work(i);    
  72.         }  
  73.   
  74.     }    
  75.     
  76.     /**  
  77.      * @param args  
  78.      */    
  79.     public static void main(String[] args) {    
  80.         for (int i = 0; i < list.size(); i++)    
  81.             exec.execute(new Task(i));    
  82.     }    
  83.     
  84. }    

你可能感兴趣的:(java 多线程打印1到75个数)