分享
 
 
 

Java同步代码转异步代码

王朝学院·作者佚名  2009-11-19
窄屏简体版  字體: |||超大  

Java同步代码转异步代码

Kagula

2009-11-18

摘要

Thread Thread Group

概要

利用Java源码,介绍同步代码转异步代码框架的使用,下半部份附框架源代码,阅读本文之前可以参考《Java多线程_编程模型研究》http://blog.csdn.net/lee353086/archive/2008/01/10/2033587.aspx

正文

TestThreads.java源码 用来示范异步调用

view plaincopy to clipboardprint?

import kagula.multithread.MsgManagement;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class testThreads {

private static Log log = LogFactory.getLog(testThreads.class);

static public void main(String args[])

{

log.debug("-Test thread.begin!");

try

{

Object lockMain=new Object(); //用来锁住主线程,可以用来等待branch thread返回运算结果。

MsgManagement mm=new MsgManagement("MsgProc1",lockMain); //MsgManagement的实例化

String sTN =mm.invoke("t_main",new Class[] {String.class},new Object[] {new String("FirstT")});

String sTN2 =mm.invoke("t_main",new Class[] {String.class},new Object[] {new String("SecondT")});

log.debug("sTN="+sTN+" sTN2="+sTN2);

if(mm.activeCount()>0) {

mm.setMaxPriority(Thread.MAX_PRIORITY);

synchronized(lockMain)

{

lockMain.wait(100);

}

mm.interrupt();

}

log.debug("mm.getResult(sTN) "+mm.getResult(sTN));

log.debug("mm.getResult(sTN2) "+mm.getResult(sTN2));

}catch(Exception ex)

{

ex.printStackTrace();

}

log.debug("-Test thread.end!");

}

}

import kagula.multithread.MsgManagement;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class testThreads {

private static Log log = LogFactory.getLog(testThreads.class);

static public void main(String args[])

{

log.debug("-Test thread.begin!");

try

{

Object lockMain=new Object(); //用来锁住主线程,可以用来等待branch thread返回运算结果。

MsgManagement mm=new MsgManagement("MsgProc1",lockMain); //MsgManagement的实例化

String sTN =mm.invoke("t_main",new Class[] {String.class},new Object[] {new String("FirstT")});

String sTN2 =mm.invoke("t_main",new Class[] {String.class},new Object[] {new String("SecondT")});

log.debug("sTN="+sTN+" sTN2="+sTN2);

if(mm.activeCount()>0) {

mm.setMaxPriority(Thread.MAX_PRIORITY);

synchronized(lockMain)

{

lockMain.wait(100);

}

mm.interrupt();

}

log.debug("mm.getResult(sTN) "+mm.getResult(sTN));

log.debug("mm.getResult(sTN2) "+mm.getResult(sTN2));

}catch(Exception ex)

{

ex.printStackTrace();

}

log.debug("-Test thread.end!");

}

}

MsgManagement.java Original Thread.java源码 ,是异步框架

view plaincopy to clipboardprint?

package kagula.multithread;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

/**

* 线程管理器

*

* Requirement: JDK1.6.x or above

*

* @author Jun Li

*

*/

public class MsgManagement extends ThreadGroup{

protected static Log log = LogFactory.getLog(MsgManagement.class);

private int nID=1; //auxiliary thread counter flag

private Map mapResults=Collections.synchronizedMap(new HashMap()); //store threads processing result.

private Object lockMain;

// -----------------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);

}

public synchronized String invoke(String pMethodName,Class pClass[],Object pObject[],Object pLock) throws Exception

{

String strThreadName=new String("MsgManagement's No."+nID+" thread");

MyThread thread=new MyThread(this,strThreadName);

thread.setLockMain(pLock);

thread.setMethodName(pMethodName);

thread.setClassParams(pClass);

thread.setObjParams(pObject);

thread.start();

nID++;

return strThreadName;

}

/**

* 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;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

/**

* 线程管理器

*

* Requirement: JDK1.6.x or above

*

* @author Jun Li

*

*/

public class MsgManagement extends ThreadGroup{

protected static Log log = LogFactory.getLog(MsgManagement.class);

private int nID=1; //auxiliary thread counter flag

private Map mapResults=Collections.synchronizedMap(new HashMap()); //store threads processing result.

private Object lockMain;

// -----------------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);

}

public synchronized String invoke(String pMethodName,Class pClass[],Object pObject[],Object pLock) throws Exception

{

String strThreadName=new String("MsgManagement's No."+nID+" thread");

MyThread thread=new MyThread(this,strThreadName);

thread.setLockMain(pLock);

thread.setMethodName(pMethodName);

thread.setClassParams(pClass);

thread.setObjParams(pObject);

thread.start();

nID++;

return strThreadName;

}

/**

* 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;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

/**

* 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;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

/**

* 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

String s=null;;

try{

log.debug("before sleep "+sT);

sleep(nWaitTime);

log.debug("after sleep "+sT);

s="I will return. "+sT;

return s;

}catch(InterruptedException e)

{

log.error("kagula("+getId()+"):"+e.toString());

}

return s;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有