// -----------------below methods using only by main callers.begin----------------- public MsgManagement(String strGroupName,Object pLock) { //Initialization the attributes super(strGroupName); lockMain=pLock; }
/** * After send processing request,will be return soon. * @param msg * Message * @param nParam * First Parameter * @param secondParam * Second Parameter * @return */ public synchronized String invoke(String pMethodName,Class pClass[],Object pObject[]) throws Exception { return invoke(pMethodName,pClass,pObject,lockMain); }
/** * Get result by thread name * @param pThreadName * @return */ public Object getResult(String pThreadName) { return mapResults.get(pThreadName); } // -----------------above methods using only by main callers.end-----------------
//-----------------below methods using only by threads.begin----------------- public void setNResult(String pName,Object pObj) { mapResults.put(pName, pObj); } // -----------------above methods using only by threads.begin----------------- } package kagula.multithread;
// -----------------below methods using only by main callers.begin----------------- public MsgManagement(String strGroupName,Object pLock) { //Initialization the attributes super(strGroupName); lockMain=pLock; }
/** * After send processing request,will be return soon. * @param msg * Message * @param nParam * First Parameter * @param secondParam * Second Parameter * @return */ public synchronized String invoke(String pMethodName,Class pClass[],Object pObject[]) throws Exception { return invoke(pMethodName,pClass,pObject,lockMain); }
/** * Get result by thread name * @param pThreadName * @return */ public Object getResult(String pThreadName) { return mapResults.get(pThreadName); } // -----------------above methods using only by main callers.end-----------------
//-----------------below methods using only by threads.begin----------------- public void setNResult(String pName,Object pObj) { mapResults.put(pName, pObj); } // -----------------above methods using only by threads.begin----------------- }
view plaincopy to clipboardprint? package kagula.multithread;
/** * Your object need derived from it to implement asynchronous method! * @author Lijun * */ public abstract class OriginalThread extends Thread{ protected static Log log = LogFactory.getLog(OriginalThread.class); protected String MethodName=new String("default"); protected Object objParams[]; protected Class classParams[]; protected Object lockMain; public OriginalThread(ThreadGroup tg,String name) { super(tg,name); }
public void run() { log.debug("["+getName()+"] instance of OriginalThread: Before Invoke the method ["+MethodName+"]"); Method method=null; Object objR=null; try { method=this.getClass().getMethod(MethodName, classParams); }catch(NoSuchMethodException e) { log.error(e.toString()); return; } if(method!=null) { try { objR=method.invoke(this, objParams); }catch(IllegalaccessException e) { log.error(e.toString()); return; }catch(InvocationTargetException e) { log.error(e.toString()); return; }catch(IllegalArgumentException e) { log.error(e); return; } catch(Exception e) { log.error(e); return; } } log.debug("["+getName()+"] instance of OriginalThread: After Invoke the method ["+MethodName+"] return value is ["+objR+"]");
setNResult(objR); //put setNResult before setNFlag,because assure after nFlag==SUCCESS,caller will get proper result! synchronized(lockMain) { lockMain.notifyAll(); } }
//---------------------below methods only should be used by MsgManagement--------------------- private void setNResult(Object pObj) { MsgManagement mm=(MsgManagement)this.getThreadGroup(); mm.setNResult(this.getName(), pObj); }
public void setMethodName(String pMethodName) { this.MethodName = pMethodName; }
public void setObjParams(Object[] objParams) { this.objParams = objParams; }
public void setClassParams(Class[] classParams) { this.classParams = classParams; }
public void setLockMain(Object pLock) { lockMain=pLock; } } package kagula.multithread;
/** * Your object need derived from it to implement asynchronous method! * @author Lijun * */ public abstract class OriginalThread extends Thread{ protected static Log log = LogFactory.getLog(OriginalThread.class); protected String MethodName=new String("default"); protected Object objParams[]; protected Class classParams[]; protected Object lockMain; public OriginalThread(ThreadGroup tg,String name) { super(tg,name); }
public void run() { log.debug("["+getName()+"] instance of OriginalThread: Before Invoke the method ["+MethodName+"]"); Method method=null; Object objR=null; try { method=this.getClass().getMethod(MethodName, classParams); }catch(NoSuchMethodException e) { log.error(e.toString()); return; } if(method!=null) { try { objR=method.invoke(this, objParams); }catch(IllegalAccessException e) { log.error(e.toString()); return; }catch(InvocationTargetException e) { log.error(e.toString()); return; }catch(IllegalArgumentException e) { log.error(e); return; } catch(Exception e) { log.error(e); return; } } log.debug("["+getName()+"] instance of OriginalThread: After Invoke the method ["+MethodName+"] return value is ["+objR+"]");
setNResult(objR); //put setNResult before setNFlag,because assure after nFlag==SUCCESS,caller will get proper result! synchronized(lockMain) { lockMain.notifyAll(); } }
//---------------------below methods only should be used by MsgManagement--------------------- private void setNResult(Object pObj) { MsgManagement mm=(MsgManagement)this.getThreadGroup(); mm.setNResult(this.getName(), pObj); }
public void setMethodName(String pMethodName) { this.MethodName = pMethodName; }
public void setObjParams(Object[] objParams) { this.objParams = objParams; }
public void setClassParams(Class[] classParams) { this.classParams = classParams; }
public void setLockMain(Object pLock) { lockMain=pLock; } }
MyThread.java是,你同步代码存放的地方
view plaincopy to clipboardprint? package kagula.multithread;
import org.apache.commons.logging.LogFactory;
public class MyThread extends OriginalThread{ public MyThread(ThreadGroup tg,String name) { super(tg,name);
log=LogFactory.getLog(this.getClass()); }
public String t_main(String sT) { final long nWaitTime=1*1*5*1000; //H*M*S,once,more than nWaitTime hours