问题提出,由于业务会出现多人同时操作,或者业务人员反复的操作,因此在业务流程中,需要对业务操作数据进行保护,由于使用数据库锁可能会引起一些难以预料的问题,因此考虑使用内存锁,设计思想:在内存中使用一个静态的链表,即时的对业务操作进行响应,类似于中断机制。在这个过程中需要考虑使用单例。具体看代码:
1 import java.util.LinkedList; 2 3 /** 4 * 5 * Description:<p></p> 6 * @author: JanneLeMac@gmail.com 7 * @version: 2013-9-14 下午10:14:47 8 */ 9 public class SendHelper { 10 private static LinkedList<String> sendItem=null; 11 private static volatile SendHelper M_INSTANCE = null; 12 /** 13 * Description: <p>use private make sure the SendHelper is unique. </p> 14 */ 15 private SendHelper(){}; 16 17 public static SendHelper getInstance(){ 18 if(null == M_INSTANCE){//if null 19 synchronized (SendHelper.class) {//if more than two threads run into the first null check same time 20 //to avoid instanced more than one time, it needs to be checked again. 21 if(null == M_INSTANCE){ 22 M_INSTANCE = new SendHelper(); 23 sendItem=new LinkedList<String>(); 24 } 25 } 26 } 27 return M_INSTANCE; 28 } 29 public LinkedList<String> getSendItem() { 30 return sendItem; 31 } 32 public void setSendItem(LinkedList<String> sendItem) { 33 SendHelper.sendItem = sendItem; 34 } 35 public void addItem(String LSH){ 36 sendItem.addLast(LSH); 37 } 38 public void removeItem(String LSH){ 39 sendItem.remove(LSH); 40 } 41 42 public static void main(String []args){ 43 System.out.println("---------test beagin-----------"); 44 //LinkedList<String> l = new LinkedList<String>(); 45 //SendHelper.getInstance().setSendItem(l); 46 SendHelper.getInstance().addItem("1"); 47 SendHelper.getInstance().addItem("2"); 48 SendHelper.getInstance().addItem("3"); 49 SendHelper.getInstance().addItem("4"); 50 for(int i=0;i<SendHelper.getInstance().getSendItem().size();i++){ 51 System.out.println(SendHelper.getInstance().getSendItem().get(i)); 52 if("2".equals(SendHelper.getInstance().getSendItem().get(i))){ 53 SendHelper.getInstance().removeItem("4"); 54 } 55 } 56 System.out.println("size:"+SendHelper.getInstance().getSendItem().size()); 57 System.out.println("---------test end-----------"); 58 } 59 60 //1.业务判断,有锁,返回提示信息 61 //2.加锁 62 //3.业务操作 63 //4.移除锁 64 }
问题解决,具体使用还得看业务系统的使用,思路就这样了,还得看系统的运行情况^_^。