生成ID的线程池

    自己写的一个生成ID的线程池,池里有30个ID,可以通过getID不断的取ID,通过SetID来不断的生成ID

    Main.java
    
package com.paincupid.springmvc.poolv2;

import java.util.ArrayDeque;
import java.util.Deque;

public class Main {

	public static void main(String[] args) {
		Deque<Integer> deque = new ArrayDeque<Integer>();
		deque.add(new Integer(1));
		GenPool gp = new GenPool(deque);
		
		Thread getIdThread = new Thread(new GetID(gp));
		Thread setIdThread = new Thread(new SetID(gp));
		//设置生成Id的为守护线程,当GetID到1000后,守护线程结束。
		setIdThread.setDaemon(true);
		setIdThread.start();
		getIdThread.start();
		
		

	}

}


    GenPool.java
    
package com.paincupid.springmvc.poolv2;

import java.util.Date;
import java.util.Deque;
import java.util.LinkedList;

public class GenPool {

	private Deque<Integer> deque;

	public GenPool(Deque<Integer> deque) {
		this.deque = deque;
	}

	public synchronized void set() {
		while (deque.size() == 30) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		deque.add(deque.getLast().intValue() + 1);
		System.out.printf("Set: %d\n", deque.getLast().intValue());
		notify();
	}

	public synchronized Integer get() {
		while (deque.size() == 1) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		//System.out.printf("Get: %d: %s %s\n",deque.getFirst(), deque.size(),deque.poll());
		int result = deque.getFirst().intValue();
		System.out.println("------------Get: "+deque.getFirst()+", size: "+deque.size());
		deque.poll();
		notify();
		return result;
	}

}


    GetID.java
    
package com.paincupid.springmvc.poolv2;

import java.util.Deque;

public class GetID  implements Runnable {
	
	private GenPool pool;
	public GetID(GenPool pool){
		this.pool = pool;
	}
	
	@Override
	public void run() {
		for (int i=0; i<1000; i++){
			Integer it = pool.get();
//			try {
//				Thread.sleep(1000);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
		}
	}

}


    SetID.java
    
package com.paincupid.springmvc.poolv2;


public class SetID implements Runnable {
	private GenPool pool;
	public SetID(GenPool pool){
		this.pool = pool;
	}
	
	@Override
	public void run() {
		//for (int i=0; i<100; i++){   //只set生成100个Id
		while(true){ 
			pool.set();
		}
	}

}


    结果:

Set: 2
Set: 3
Set: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Set: 11
Set: 12
Set: 13
Set: 14
Set: 15
Set: 16
Set: 17
Set: 18
Set: 19
Set: 20
Set: 21
Set: 22
Set: 23
Set: 24
Set: 25
Set: 26
Set: 27
Set: 28
Set: 29
Set: 30
------------Get: 1, size: 30
------------Get: 2, size: 29
------------Get: 3, size: 28
------------Get: 4, size: 27
------------Get: 5, size: 26
------------Get: 6, size: 25
------------Get: 7, size: 24
------------Get: 8, size: 23
------------Get: 9, size: 22
------------Get: 10, size: 21
------------Get: 11, size: 20
------------Get: 12, size: 19
------------Get: 13, size: 18
------------Get: 14, size: 17
------------Get: 15, size: 16
------------Get: 16, size: 15
------------Get: 17, size: 14
------------Get: 18, size: 13
------------Get: 19, size: 12
------------Get: 20, size: 11
------------Get: 21, size: 10
------------Get: 22, size: 9
------------Get: 23, size: 8
------------Get: 24, size: 7
------------Get: 25, size: 6
------------Get: 26, size: 5
------------Get: 27, size: 4
------------Get: 28, size: 3
------------Get: 29, size: 2
Set: 31
Set: 32
Set: 33
Set: 34
Set: 35
Set: 36
Set: 37
Set: 38
Set: 39
Set: 40
Set: 41
Set: 42
Set: 43
Set: 44
Set: 45
Set: 46
Set: 47
Set: 48
Set: 49
Set: 50
Set: 51
Set: 52
Set: 53
Set: 54
Set: 55
Set: 56
Set: 57
Set: 58
Set: 59
------------Get: 30, size: 30
------------Get: 31, size: 29
------------Get: 32, size: 28
------------Get: 33, size: 27
------------Get: 34, size: 26
------------Get: 35, size: 25
------------Get: 36, size: 24
------------Get: 37, size: 23
------------Get: 38, size: 22
------------Get: 39, size: 21
------------Get: 40, size: 20
------------Get: 41, size: 19
------------Get: 42, size: 18
------------Get: 43, size: 17
------------Get: 44, size: 16
------------Get: 45, size: 15
------------Get: 46, size: 14
------------Get: 47, size: 13
------------Get: 48, size: 12
------------Get: 49, size: 11
------------Get: 50, size: 10
Set: 60
Set: 61
Set: 62
Set: 63
Set: 64
...........................................................................//此处省略
------------Get: 983, size: 24
------------Get: 984, size: 23
------------Get: 985, size: 22
------------Get: 986, size: 21
------------Get: 987, size: 20
------------Get: 988, size: 19
------------Get: 989, size: 18
------------Get: 990, size: 17
------------Get: 991, size: 16
------------Get: 992, size: 15
------------Get: 993, size: 14
------------Get: 994, size: 13
------------Get: 995, size: 12
------------Get: 996, size: 11
------------Get: 997, size: 10
------------Get: 998, size: 9
------------Get: 999, size: 8
------------Get: 1000, size: 7


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