解决过程:
最终通过检索关键字“spring bean 线程安全”找到了答案:
http://www.cnblogs.com/doit8791/p/4093808.html
经过测试,ThreadLocal 是最高效的解决线程安全问题的方法,使用方法如下:
@Component public class ProcessInstanceQueryImpl implements ProcessInstanceSampleQuery { /** 这种不是线程安全的对象不要放在service的成员变量中 private Mapfiltrations = new HashMap (); private Map boMap = new HashMap (); private Map basicInfoMap = new HashMap (); private List businessKeyList = new ArrayList ();**/ //创建一个ThreadLocal的类Context来缓存那些每次查询链需要用到的成员变量 private static ThreadLocal context = new ThreadLocal (); private static enum RelativeCode { RELATIVE, RELATIVED, MERGE, MERGED, SPLIT, SPLITED }; @Resource private UniversalManager universalManager; //Context类用来缓存那些每次查询链需要用到的成员变量 private static class Context { private BpQuery, ?> query; private List boQuerys; private String boFilterHql; private String bpInfoHql; private StringBuilder boHql; private String orderHql; private String sortType; private String queryType; private Context() { boQuerys = new ArrayList (); boFilterHql = "select new com.cayenne.bpm.workbench.model.BpBasicInfoAndBo(bpInfo1,bo) from ${boName} as bo ,BpBasicInfo as bpInfo1 where bo.id=bpInfo1.boId "; bpInfoHql = "and bo.id in(select bpInfo.boId from BpBasicInfo as bpInfo where 1=1"; orderHql = ""; sortType = ""; queryType = ""; boHql = new StringBuilder(" "); } } //每次调用该service进行链式查询时,先初始化查询链,创建一个Context线程安全对象 @Override public ProcessInstanceSampleQuery initProcessInstanceSampleQuery() { //set线程安全的查询对象 signal.set(new Context()); return this; } @Override public ProcessInstanceSampleQuery queryTask(String processDefinitionKey) { //获取线程安全对象,并赋值 signal.get().query = createCandiatesQuery(processDefinitionKey); }
结论:
需要在同一个线程中调用的对象,
定义时,
ThreadLocalcontext = new ThreadLocal ();
初始化时,
signal.set(new Context());
获取时,
signal.get();(在未调用set方法之前,都是同一个线程之前set的signal值)
如果想要每次
signal.get();获取的是初始化后的结果,那么定义时写成:
ThreadLocalcontext = new ThreadLocal (){ @Override protected Context initialValue() { return new Context(); } };
-*-&-*-&-*-&-*-&-*-&-*-&-*-&-*-&-*后知后觉的分割线-&-*-&-*-&-*-&-*-&-*-&-*-&-*-&-*-&-*
今天解决别的问题,突然发现,这个Context就是俗称的上下文类,并且在Activiti,spring等源码中也看到了相关的应用,这东西专门用来处理线程安全问题的,哈哈,终于明白了