批量存储信息
import java.util.Collections;import java.util.LinkedList;import java.util.List;import java.util.Properties;import java.util.concurrent.locks.ReentrantLock;import org.apache.log4j.Logger;public class MonitorStoreService5 extends Thread{ static final Logger logger = Logger.getLogger(MonitorStoreService5.class); boolean isRun = true; Listqueue = (List ) Collections.synchronizedList(new LinkedList ()); ReentrantLock lock = new ReentrantLock(); Object obj = new Object(); static int storeOvertime = 1000*3; static int cacheMaxSize = 1000; @Override public void run() { IMonitorService dao = new IMonitorService(); while(isRun){ synchronized(obj){ logger.debug("run() wait"); try{ obj.wait(storeOvertime); }catch(Exception ex){} } lock.lock(); try{ logger.debug("run() queue.save."); if(!queue.isEmpty() && dao.saveMonitor(queue)){ queue.clear(); } logger.debug("run() queue.clear."); if(queue.size()>=cacheMaxSize){ queue.clear(); } }finally{ lock.unlock(); } } } public boolean append(Properties data){ if(!isRun) return false; if(queue.size()>=cacheMaxSize){ logger.debug("append() queue.size>=cacheMaxSize"); synchronized(obj){ obj.notifyAll(); } logger.debug("append() wait"); synchronized(obj){ try{ obj.wait(100); }catch(Exception ex){} } } while(lock.isLocked()){ logger.debug("append() lock.isLocked wait"); synchronized(obj){ try{ obj.wait(100); }catch(Exception ex){} } }// try{ Thread.sleep(1); }catch(Exception ex){}// logger.debug("append() queue.add"); return queue.add(data); } public void shutdown(){ isRun = false; }}
static void t6(){ int ac = 12333; MonitorStoreService5 ser = new MonitorStoreService5(); ser.start(); T6Thread t1 = new T6Thread(); T6Thread t2 = new T6Thread(); T6Thread t3 = new T6Thread(); t1.ser = ser; t1.ac = ac; t2.ser = ser; t2.ac = ac; t3.ser = ser; t3.ac = ac; t1.start(); t2.start(); t3.start(); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } ser.shutdown(); } static class T6Thread extends Thread{ MonitorStoreService5 ser = null; int ac = 11333; public void run(){// MonitorStoreService5 ser = new MonitorStoreService5();// ser.start(); for(int i=0; i
import java.util.LinkedList;import java.util.List;import java.util.Properties;import org.apache.log4j.Logger;public class MonitorStoreService4 extends Thread{ static final Logger logger = Logger.getLogger(MonitorStoreService4.class); LinkedList
> storeQueue = new LinkedList
>(); List current = new LinkedList (); boolean isRun = true; Object lock = new Object(); static int waitOvertime = 1000 * 30; static int currntMaxSize = 500; public void run(){ MonitorDao dao = new MonitorDao(); while(isRun){ synchronized(lock){ logger.debug("run() wait"); try{ lock.wait(waitOvertime); }catch(Exception ex){} } while(!storeQueue.isEmpty()){ List item = storeQueue.removeFirst(); logger.debug("run() storeQueue.save."); if(!dao.save(item)){ storeQueue.addLast(item); } } } if(!storeQueue.isEmpty()){ List item = storeQueue.removeFirst(); logger.debug("run() storeQueue.save."); if(!dao.save(item)){ } } if(!current.isEmpty()){ List item = current; logger.debug("run() current.save."); if(!dao.save(item)){ } } logger.debug("run() shutdown."); } // synchronized public boolean append(Properties data){ if(!isRun) return false; if(current.size()>=currntMaxSize){ logger.debug("append() storeQueue.addLast"); storeQueue.addLast(current); current = new LinkedList (); synchronized(lock){ lock.notifyAll(); } } // try{ Thread.sleep(1); }catch(Exception ex){} logger.debug("append() current.add"); return current.add(data); } public void shutdown(){ isRun = false; }}
fdkljfdljf
import java.util.List;import java.util.Properties;import org.apache.log4j.Logger;public class MonitorDao { static final Logger logger = Logger.getLogger(MonitorDao.class); public boolean save(Listls){ logger.debug("save size:"+ls.size()); for(Properties i:ls){ } try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } return true; }}
测试
static void t5(){ MonitorStoreService4 ser = new MonitorStoreService4(); ser.start(); for(int i=0; i<100333; i++){ ser.append(new Properties()); } try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } ser.shutdown(); }