java模拟高并发请求

来源:互联网 发布:netcat 连接指定端口 编辑:程序博客网 时间:2024/05/16 06:12


实现高并发请求,即同一个进程开辟出多个线程请求相同的资源 ,再同时对一个资源进行访问操作。 

我们都知道 要实现一个多线程的并发服务可以有两种方式,一种是继承 Theard  类 ,另一种是实现Runnable 接口  在java.lang.Theard 包中,可以看到Theard类也是实现了 Runnable 接口 的实现。

下文中 是通过实现Runnable 接口来完成多线程并发。


我这里是实现一个模拟多用户投标的一个model (投标多用于 p2p 网站中,借款人在平台发起借款标,投资人可以对该借款标发起投标。)


本地配置的项目spring mvc 模式, web 服务器为tomcat服务器 ,

在web.xml 中配置监听服务,所以已经启动单一线程,在下例子中采用spring 注解, 在当前线程中启动定时服务Scheduled,



/**

 * 
 */
package com.byzk.p2p.thread;


import java.util.HashMap;
import java.util.List;


import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.byzk.p2p.admin.base.controller.BaseController;
import com.byzk.p2p.admin.base.util.MD5;
import com.byzk.p2p.admin.user.dao.UserAccountDao;
import com.byzk.p2p.admin.user.dto.User;
import com.byzk.p2p.admin.user.service.UserService;


/***
 * <p>
 * Title:CurrentTime
 * </p>
 * <p>
 * Description:定时执行
 * </p>
 * <p>
 * Company:xbd
 * </p>
 * 
 * @author:Administrator
 * @date:2016年11月2日下午4:29:07
 */
@Component
public class CurrentTime extends BaseController {


@Autowired
private UserService userService;
@Autowired
private UserAccountDao userAccount;


private static Logger logger = Logger.getLogger(CurrentTime.class);


@Scheduled(cron = "0 59 14 * * ? ")
public void execute() {
// 查询所有用户
Integer index =0;
List<User> userList = userService.listAll1(new HashMap<String, Object>());
try {
if (userList != null && userList.size() > 0) {
for (User user : userList) {
TestThread runnable = new TestThread();
if (StringUtils.isNotBlank(user.getFuionAccount()) && "N".equals(user.getFuionAccount())) {
continue;
}
   if(index>=15){
    break;
   }
runnable.setUserId(user.getId());
runnable.setBorrowId(12230);
runnable.setAmount(100.00);
runnable.setThread(runnable);
if(StringUtils.isNotBlank(user.getDealPassword()) && MD5.GetMD5Code("jx123456").equals(user.getDealPassword())){
runnable.setPassword("jx123456");
}
   if(StringUtils.isNotBlank(user.getDealPassword()) &&MD5.GetMD5Code("jx654321").equals(user.getDealPassword())){
runnable.setPassword("jx654321");
   }
new Thread(runnable).start();
logger.info(Thread.currentThread().getName() + "用户【" + user.getUsername() + "】发起投标请求"); index++;
}
}
} catch (Exception e) {
}
}






        这是一个新的程序入口, 测试两个线程的并发。



/**

* <p>Description:程序入口主函数 </p>
* <p>Company:xbd </p>
* @author:lilei
* @date:2016年11月4日下午2:14:40
*/
public static void main(String []args){
TestThread t = new TestThread();
// 用户1
t.setUserId(2381);
t.setAmount(200.00);
t.setBorrowId(12230);
t.setPassword("jx123456");
t.setThread(t);
new Thread(t).start();

TestThread t1 = new TestThread();
// 用户2
t1.setUserId(15167);
t1.setAmount(100.00);
t1.setBorrowId(12230);
t1.setPassword("jx654321");
t1.setThread(t1);
new Thread(t1).start();

}

}



/**
 * 
 */
package com.byzk.p2p.thread;


import org.apache.log4j.Logger;


import com.byzk.p2p.admin.fuiou.config.Config;
import com.byzk.p2p.admin.fuiou.util.WebUtils;
import com.byzk.p2p.admin.user.dto.User;
/*** <p>Title:MtThread </p>
* <p>Description: </p>
* <p>Company:xbd </p>
* @author:Administrator
* @date:2016年11月2日下午4:28:04 */
public class TestThread  implements Runnable{




private Double amount;
private String password;
private Integer borrowId;
private TestThread thread;
    private Integer userId;


/**
* @return the userId
*/
public Integer getUserId() {
return userId;
}


/**
* @param userId the userId to set
*/
public void setUserId(Integer userId) {
this.userId = userId;
}


private static  Logger logger = Logger.getLogger(TestThread.class);
/**
* @return the amount
*/
public Double getAmount() {
return amount;
}


/**
* @param amount the amount to set
*/
public void setAmount(Double amount) {
this.amount = amount;
}


/**
* @return the password
*/
public String getPassword() {
return password;
}


/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}


/**
* @return the borrowId
*/
public Integer getBorrowId() {
return borrowId;
}


/**
* @param borrowId the borrowId to set
*/
public void setBorrowId(Integer borrowId) {
this.borrowId = borrowId;
}


/**
* @return the thread
*/
public TestThread getThread() {
return thread;
}


/**
* @param thread the thread to set
*/
public void setThread(TestThread thread) {
this.thread = thread;
}






/**
* 构造函数
*/
public TestThread(){
}

public TestThread(TestThread thread){
this.thread =  thread;
}

@Override
public void run() {
String backStr="";
try {
backStr = WebUtils.sendHttp(Config.server_url+"/front/tender/save1", this.getThread());
logger.info(Thread.currentThread().getName()+"-----------is Tender borrow-----------详细信息 ");
} catch (Exception e) {
// TODO Auto-generated catch block
logger.info("错误消息:"+e.getMessage());
}
}


}



其中 config 类      WebUtils 类 涉及到 别的同事的 研发     未公开 望海涵!

0 0
原创粉丝点击