我正在做一个池应用程序,其中有3个变量,a为初始连接,它是20,b为递增大小,这意味着当池达到20时,它将增加池大小的5乘5,y为最大连接数,它是50,我有问题实现b变量在一个有条件的方式,这是代码
public class ThreadHolder extends Thread {
ArrayList<Client> pool;
public static int a = 20;
public static int b = 5;
public static int y = 50;
public static long ftime, stime, time;
public Client c = null;
public ThreadHolder(){
pool = new ArrayList<Client>();
}
public void run(){
stime = System.currentTimeMillis();
try {
while(pool.size()<= y){
if(pool.size() < a){
poolHelper();
Thread.sleep(600);
}
else if(pool.size()> a){
a = a+b;
poolHelper();
Thread.sleep(600);
}
else {
System.out.println("----------------------------------");
System.out.println("Database is busy, please try again in a second.");
deleteClient(c);
System.out.println("Pool -> "+pool.size() + ".");
Thread.sleep(300);
}
ftime = System.currentTimeMillis();
time = (ftime - stime)/1000 ;
System.out.println("Time established -> " + time + " sec");
}
}catch(Exception e) {e.printStackTrace();}
}
public void poolHelper(){
obtainClient(c);
addClient(c);
System.out.println("Pool -> "+pool.size() + ".");
}
public synchronized void obtainClient(Client c){
Client a = new Client(this);
a.start();
}
public synchronized void addClient(Client c){
pool.add(c);
}
public synchronized void deleteClient(Client c){
pool.remove(c);
}
}
当我的池大小达到20个连接以上时,如何实现使用b来不断向池中添加客户端
暂无答案!
目前还没有任何答案,快来回答吧!