All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eshore.socketapi.server.GlobWorker Maven / Gradle / Ivy

There is a newer version: 1.2.8
Show newest version
package com.eshore.socketapi.server;

import java.util.ArrayList;
import java.util.List;

import com.eshore.khala.utils.LRUCache;

public class GlobWorker {
	ArrayList clientList= new ArrayList();
	static LRUCache cache=new LRUCache (1000);
	 int changeCount=0;
	 int loop=0;
	 int wokerSize=4;
	 
	 public void addClientWorker(ClientWorker w){
		 clientList.add(w);
		 if(changeCount>10){
				updateWorkerList();
		}
	 }
	 public GlobWorker (){
		 for(int i=0;ilist2 =removeNotavailable(clientList);
				System.out.println("共回收"+(clientList.size()-list2.size())+"个连接!"+changeCount);
				changeCount=0;
				clientList=list2;
			}catch(Exception e){}
		}
		
		private static ArrayList removeNotavailable(List  list){
			ArrayListlist2 = new ArrayList();
			for(ClientWorker w:list){
				if(w.isAvailable()||w.isKeepWhileBreak())list2.add(w);
				else{
					System.out.println("client exit ! ip:"+w.ip+":"+w.port);
				}
			}
			return list2;
		}
		
		private Boolean lock=true;
		
		public ClientWorker getWorker(){
			
			if(clientList==null)return null;
			int size=clientList.size();
			if(size==0)return null;
			int i=0;
			//System.out.println("size:"+size);
			synchronized (lock) {
				if(loop>=size){
					loop=0;
				}
				i=loop;
				loop++;
			}
			ClientWorker w =null;
			try{
				w = clientList.get(i);
			}catch(Exception e){}
			return w;
		}
		
		public static ClientWorker getClientWork(String clientId){
			return cache.get(clientId);
		}
		
		public static void addClient(String clientId,ClientWorker w){
			cache.put(clientId, w);
		}
		
		
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy