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 类 涉及到 别的同事的 研发 未公开 望海涵!
- java模拟高并发请求
- 模拟高并发请求
- PHP_curl模拟高并发请求
- java模拟并发请求
- JAVA 模拟瞬间高并发
- JAVA 模拟瞬间高并发
- JAVA 模拟瞬间高并发
- JAVA 模拟瞬间高并发
- java CountDownLatch 模拟用户并发请求
- curl_multi_init 模拟并发请求
- 模拟高并发测试
- 高并发模拟
- CountDownLatch 模拟高并发
- java模拟并发请求测试方法是否线程安全
- 买票系统模拟Java高并发(ConcurrentQueue的解决方案)
- php 模拟高并发访问
- 并发 - java高并发解决方案
- java-并发-高并发概述
- react ntive snippet的妙用
- Mongos与集群均衡
- 1996: [Hnoi2010]chorus 合唱队
- 上周C语言作业
- fiddler使用方法
- java模拟高并发请求
- Android 一个有趣的小程序!!!看你能有多短!!!
- Android手机查看某个烦人的进程日志是哪个打印的
- 在Eclipse中使用JUnit进行 单元测试
- iOS10 真机调试报错
- BruteXSS:XSS暴力破解
- 理解opencv中的step[]和step()
- 线程试题
- git学习—git log 和git diff