获取调用多线程的list数据:
public String simulatePwdLogin2(String clientId,String rownum,String type){
//获取传过来的系统id,转化成对应的rela_cd
//String outXml = null;
CommConfig config=this.commConfigDaoImpl.getCommConfigByType(EamConstants.THREAD_LIMIT_COUNT);
String code = config.getCode();
//获得配置表中的线程并发数量
ThreadLimitCount = Integer.parseInt(code);
System.out.println("-----配置表中的线程并发数量 =="+ThreadLimitCount);
//Semaphore初始化,限制并发数量
Semaphore semaphore = new Semaphore(ThreadLimitCount);
try{
try{
//判断请求的client_id不能为空
if(StrUtil.isEmpty(clientId) || StrUtil.isEmpty(type)){
throw new ReqBodyValidationException();
}
} catch (Exception e) {
e.printStackTrace();
}
//获取传过来的系统id,转化成对应的rela_cd
OAuthClientSysVo clientSyscond = new OAuthClientSysVo();
clientSyscond.setSysClientCode(clientId);
clientSyscond = oAuthClientSysDao.loadClientSysBySysVo(clientSyscond);
String sysCode = "";
if(clientSyscond!=null){
//获得系统的rela_cd
sysCode = clientSyscond.getSysRelaCode();
}
//通过rela_cd查询数据库,获得对应系统的人员数据,rownum限制获得人员的数量
List<LdapStaffVo> staffVo = ldapService.queryStaffVo(sysCode, rownum);
if(!StrUtil.isNullOrEmpty(staffVo)){
for(LdapStaffVo vo:staffVo){
if(!StrUtil.isNullOrEmpty(rownum)){
new WorkingThread(clientId,rownum,type,vo,oAuthClientSysDao,accountPasswdAuthHandler,logConsumer,intfLogManager,semaphore).start();
}else{
new WorkingThread(clientId,type,vo,oAuthClientSysDao,accountPasswdAuthHandler,logConsumer,intfLogManager,semaphore).start();
}
//System.out.println("----Thread.currentThread() ="+Thread.currentThread());
//Thread.sleep(10000);
}
}
}catch(Exception e){
e.printStackTrace();
}
return "ok";
}
多线程的代码:
private static class WorkingThread extends Thread {
private String clientId;
private String rownum;
private String type;
private LdapStaffVo vo;
private JdbcOAuthClientSysDao oAuthClientSysDao;
private AccountPasswdAuthHandler accountPasswdAuthHandler;
private LogConsumer logConsumer;
private IntfLogManager intfLogManager;
private Semaphore semaphore;
private WorkingThread(String clientId,String rownum,String type,LdapStaffVo vo,JdbcOAuthClientSysDao oAuthClientSysDao,
AccountPasswdAuthHandler accountPasswdAuthHandler,LogConsumer logConsumer,IntfLogManager intfLogManager,Semaphore semaphore){
super();
this.clientId = clientId;
this.rownum = rownum;
this.type = type;
this.vo = vo;
this.oAuthClientSysDao = oAuthClientSysDao;
this.accountPasswdAuthHandler = accountPasswdAuthHandler;
this.logConsumer = logConsumer;
this.intfLogManager = intfLogManager;
this.semaphore = semaphore ;
}
private WorkingThread(String clientId,String type,LdapStaffVo vo,JdbcOAuthClientSysDao oAuthClientSysDao,
AccountPasswdAuthHandler accountPasswdAuthHandler,LogConsumer logConsumer,IntfLogManager intfLogManager ,Semaphore semaphore){
super();
this.clientId = clientId;
this.type = type;
this.vo = vo;
this.oAuthClientSysDao = oAuthClientSysDao;
this.accountPasswdAuthHandler = accountPasswdAuthHandler;
this.logConsumer = logConsumer;
this.intfLogManager = intfLogManager;
this.semaphore = semaphore ;
}
public void run(){
//申请一个线程请求
try {
semaphore.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("------当前线程 :" + Thread.currentThread().getName() +"进入,当前已有" + (ThreadLimitCount - semaphore.availablePermits()) + "线程并发");
String outXml = null;
//获得随机的4位数
String randomCode = PubUtil.createRandom(4);
//获取当前的系统时间
String format = new SimpleDateFormat("yyyyMMddhhmmssSSS").format(new Date());
semaphore.release();
System.out.println("------当前线程 :" + Thread.currentThread().getName() +"离开,当前已有" + (ThreadLimitCount - semaphore.availablePermits()) + "线程并发");
}