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

解法一:

还有另外一个解法:
package net.liuyx.test;  
  
import java.util.ArrayList;  
import java.util.List;  
import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
import java.util.concurrent.locks.Condition;  
import java.util.concurrent.locks.Lock;  
import java.util.concurrent.locks.ReentrantLock;  
  
public class Test {  
	private static final int TASK_NUM = 3;
    private static int num = 0;  
    private static int flag = 0;  
    private static Lock lock = new ReentrantLock();  
    private static List list = new ArrayList();  
    private static ExecutorService exec = Executors.newCachedThreadPool();  
    static {  
    	for(int i = 0; i < TASK_NUM; i++){
    		list.add(lock.newCondition());
    	}
    }  
  
    private static void crit() {  
        if (num >= 75) {  
            System.exit(1);  
        }  
    }  
  
    private static void print() {  
        crit();  
        System.out.print(Thread.currentThread());  
        for (int i = 0; i < 5; i++) {  
            System.out.format("%-2d ", ++num);  
        }  
        System.out.println();  
    }  
  
    private static void work(int i) {  
            while (!Thread.interrupted()) {  
                try{  
                    lock.lock();  
                    if(flag == i){  
                        crit();  
                        print();  
                        flag = (i + 1) % list.size();  
                        list.get((i+1)%list.size()).signal();  
                    }else{  
                        try {  
                            list.get(i%list.size()).await();  
                        } catch (InterruptedException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                }finally{  
                    lock.unlock();  
                }  
            }  
    }  
  
    private static class Task implements Runnable {  
        private final int i;  
  
        public Task(int i) {  
            this.i = i;  
        }  
  
        @Override  
        public void run() {  
            work(i);  
        }

    }  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        for (int i = 0; i < list.size(); i++)  
            exec.execute(new Task(i));  
    }  
  
}  

解法二:

http://blog.csdn.net/lyx2007825/article/details/7767408

你可能感兴趣的:(Java)