1 高并发限流处理 这样会不会有问题?请大侠指点!
public class LimitProcessTest { //最大处理条数 static int maxLine = 10; // 当前处理条数 static int line = 0; public synchronized int getToken() { if(line > maxLine){ return -1; } return line++; } public synchronized int giveUpToken() { return line--; } public static void main(String[] args) throws InterruptedException { LimitProcessTest lpt = new LimitProcessTest(); for (int i = 0; i <500; i++) { Thread.sleep(25); new Thread(lpt.new Process(lpt, i)).start(); } } public class Process implements Runnable{ private LimitProcessTest lpt; private int index ; Process(LimitProcessTest lpt1,int i){ this.lpt=lpt1; this.index=i; } @Override public void run() { try { int token = lpt.getToken(); if(token==-1){ System.out.println("第 "+index+" 个人访问发现很多人,等下来..."); return; }else{ System.out.println("第 "+index+" 个人访问, 当前处理人数 "+token); } Thread.sleep(753); System.out.println("第 "+index+" 个人访问"); } catch (InterruptedException e) { }finally{ System.out.println("当前处理人数: "+lpt.giveUpToken()); } } }}
2