Java 入门 多线程 之 多线程返回用户ID(二)

产生随机码 数据栈

package synPack;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.Callable;

public class RandomStack implements Callable<List<Integer>>{

	// st : random stack
	// thrnum,less than thrnum then refresh stack
	// reqnum,request number for id
	// bunnum,the num of st created once
	// bits,the digits(wei`shu) of root which can calculate maxnum
	// times,the num refresh st per day
	// apdstr : the appended for uniform str
	private static int times;	
	private static Stack<Integer> st;
	private static Calendar date;	
	private static int day;
	
	private List<Integer> res;
	private int thrNum,reqNum,bunNum,maxNum,root,bits;
	private String apdstr;

	public List<Integer> getRes() {
		return res;
	}	
	public int getReqNum() {
		return reqNum;
	}
	public static Stack<Integer> getRTObj() {
		return st;
	}
	
	static{
		if(st == null){
			st = new Stack<Integer>();
			// if(day == this.Calendar.DAY) then time != and root readd
			times = 0;
			date = Calendar.getInstance();
			day = date.get(Calendar.DAY_OF_MONTH);
		}
	}
	
	RandomStack(int thrNum,int reqNum,int bunNum,int bits){		
		this.root = -1;
		this.bits = bits;
		this.thrNum = thrNum;
		this.reqNum = reqNum;
		this.bunNum = bunNum;
		this.res = new ArrayList<Integer>();
		
		int i=0;
		while(i++<bits){
			apdstr = "0"+apdstr;
		} 
	}

	public List<Integer> call(){
		return getRandomNum();
	}
	
	
	public void getRoot(){
		if (times == 0){
			times++;
			maxNum = (int) Math.pow(10, bits);
			root = (int)((maxNum-1)*Math.random()+0);
		}
		else{
			times++;
			root = root+bunNum+1;
			if(root >= maxNum)
				root = root-maxNum;
		}
	}
	
	public void getRandomStack(){
		this.getRoot();
		while(!st.isEmpty()){
			st.pop();
		}
		int upp=0;
		int low=0;
		st.push(root);
		for(int i=0; i<bunNum/2; i++){
			upp = root+i+1;
			low = root-i-1;
			if(upp >= maxNum)
				st.push(upp-maxNum);
			else{
				if(upp < 0)
					st.push(upp+maxNum);
				else
					st.push(upp);
			}
			if(low >= maxNum)
				st.push(low-maxNum);
			else{
				if(low < 0)
					st.push(low+maxNum);
				else
					st.push(low);
			}
		}
	}
	
	public synchronized List<Integer> getRandomNum(){
		if(bunNum < reqNum){
			System.err.println("Your request number is much more than we can offer !");
			return null;
		}
		if(st.size()<reqNum || st.size()<thrNum || st.isEmpty()){
			this.getRandomStack();			
		}
		Calendar cur = Calendar.getInstance();
		if(cur.get(Calendar.DAY_OF_MONTH) != day){
			times = 0;
			this.getRandomStack();			
		}
//		System.out.println(st);
		int i = reqNum;
		while(i-- > 0){
			res.add(st.get(i));
			st.pop();
		}
//		System.out.println(res);
		return res;
	}
}

 根据数据栈,实现 用户 ID 的拼接 

package synPack;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.FutureTask;

import jdk.nashorn.internal.ir.RuntimeNode.Request;

public class UIDProducer {
	
	private RandomStack randomStack;
	private String usrInfA,usrInfB;
	private List<String> lStrings;
	private List<Integer> resList;
	private String stry,strm,strd;
	private static Calendar date;
	private Thread thread;
	private FutureTask<List<Integer>> futureTask;
	
	public List<Integer> getResList() {
		return resList;
	}
	public List<String> getlStrings() {
		return lStrings;
	}
	
	public UIDProducer(int thrNum,int reqNum,int bunNum,
			int bits, String usrInfA, String usrInfB) {
		// TODO Auto-generated constructor stub
//		FutureTask<List<Integer>> f0 = new FutureTask<List<Integer>>(new RandomStack(3000, 13000, 15000, 6));
//		Thread t0 = new Thread(f0);
		randomStack = new RandomStack(thrNum, reqNum, bunNum, bits);
		futureTask = new FutureTask<List<Integer>>(randomStack);
		thread = new Thread(futureTask);
		
		thread.start();
		
		lStrings = new ArrayList<String>();
		this.usrInfA = usrInfA;
		this.usrInfB = usrInfB;	
		
		date = Calendar.getInstance(); 
		
		stry = String.valueOf(date.get(Calendar.YEAR)).substring(2, 4);
		strm = String.valueOf(date.get(Calendar.MONTH)+1);
		strd = String.valueOf(date.get(Calendar.DAY_OF_MONTH));
		if(date.get(Calendar.DAY_OF_MONTH)<10)
			strd = "0"+strd;		
		if(date.get(Calendar.MONTH)+1<10)
			strm = strm+"0";
	}
	
	public List<String> getList(){
		if(resList == null){
			return null;
		}
		int randNum;
		String str;
		for(int i=0; i<randomStack.getReqNum(); i++){
			randNum = resList.get(i);
			str = String.valueOf(randNum);
			if((randNum&1) == 0){
				lStrings.add(strd+usrInfA+stry+str);
			}
			else{
				lStrings.add(str+stry+usrInfB+strm);
			}
		}
		return lStrings;
	}
}

 多线程实现 用户 ID 获取 

package synPack;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
//		RandomStack r1 = new RandomStack(3, 1, 5, 2);
//		r1.run();		
//		try {
//			Thread.sleep(1000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println(r1.res);
//		RandomStack r2 = new RandomStack(3, 1, 5, 2);
//		r2.run();
//		System.out.println(r2.res);
		
//		// UIDProducer(thrNum, reqNum, bunNum, bits, usrInfA, usrInfB)
//		RandomStack r0 = new RandomStack(3000, 13000, 15000, 6);
//		FutureTask<List<Integer>> f0 = new FutureTask<List<Integer>>(r0);
//		Thread t0 = new Thread(f0);
//		RandomStack r1 = new RandomStack(3000, 13000, 15000, 6);
//		FutureTask<List<Integer>> f1 = new FutureTask<List<Integer>>(r1);
//		Thread t1 = new Thread(f1);
//		Thread t2 = new Thread(new FutureTask<List<Integer>>
//		(new RandomStack(3000, 14000, 15000, 6)));
//		Thread t3 = new Thread(new FutureTask<List<Integer>>
//		(new RandomStack(3000, 12000, 15000, 6)));
//
		System.out.println(r0.call());
//		System.out.println(r0.getRTObj().size());
//		t0.start();
//		try {
//			Thread.sleep(300);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println(r0.getRTObj().size());
//		System.out.println(r1.getRTObj().size());
//		System.err.println(System.currentTimeMillis());
		System.out.println(r1.call());
//		t1.start();
//		System.err.println(System.currentTimeMillis());
		System.out.println(r2.call());
//		t2.start();
//		System.err.println(System.currentTimeMillis());
		System.out.println(r3.call());
//		t3.start();
//		System.err.println(System.currentTimeMillis());
		
//		try {
//			System.out.println(f0.get());
//		} catch (InterruptedException | ExecutionException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
//		UIDProducer uidProducer = new UIDProducer(thrNum, reqNum, bunNum, bits, usrInfA, usrInfB);
		UIDProducer uidPa = new UIDProducer(30, 77705, 77710, 10, "a", "b");
//		System.out.println(uidPa.getList());
		uidPa.getList();
		System.err.println(System.currentTimeMillis());
		UIDProducer uidPb = new UIDProducer(30, 77705, 77710, 10, "a", "b");
//		System.out.println(uidPb.getList());
		uidPb.getList();
		System.err.println(System.currentTimeMillis());
		UIDProducer uidPc = new UIDProducer(30, 77705, 77710, 10, "a", "b");
//		System.out.println(uidPc.getList());
		uidPc.getList();
		System.err.println(System.currentTimeMillis());
		UIDProducer uidPd = new UIDProducer(30, 77705, 77710, 10, "a", "b");
//		System.out.println(uidPd.getList());
		uidPd.getList();
		System.err.println(System.currentTimeMillis());
	}
}

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值