Skip to content

Commit

Permalink
单线程版本
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrric committed May 13, 2021
1 parent 2521a05 commit ea7a7d3
Showing 1 changed file with 29 additions and 112 deletions.
141 changes: 29 additions & 112 deletions src/main/java/com/github/lyrric/service/SecKillService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class SecKillService {

private final Logger logger = LogManager.getLogger(SecKillService.class);

private ExecutorService service = Executors.newFixedThreadPool(200);

public SecKillService() {
httpService = new HttpService();
}
Expand All @@ -46,128 +44,47 @@ public void startSecKill(Integer vaccineId, String startDateStr, MainFrame mainF
long startDate = convertDateToInt(startDateStr);

AtomicBoolean success = new AtomicBoolean(false);
AtomicReference<String> orderId = new AtomicReference<>(null);
Runnable task = ()-> {
do {
try {
//1.直接秒杀、获取秒杀资格
long id = Thread.currentThread().getId();
logger.info("Thread ID:{},发送请求", id);
orderId.set(httpService.secKill(vaccineId.toString(), "1", Config.memberId.toString(), Config.idCard));
success.set(true);
logger.info("Thread ID:{},抢购成功", id);
} catch (BusinessException e) {
logger.info("Thread ID: {}, 抢购失败: {}",Thread.currentThread().getId(), e.getErrMsg());
//如果离开始时间120秒后,或者已经成功抢到则不再继续
if(System.currentTimeMillis() > startDate+1000*60*2 || success.get()){
return;
}
// if("操作过于频繁,请稍后再试!".equals(e.getErrMsg()) && new Random().nextBoolean()){
// try {
// Thread.sleep(500);
// } catch (InterruptedException ex) {
// ex.printStackTrace();
// }
// }
} catch (Exception e) {
e.printStackTrace();
logger.warn("Thread ID: {},未知异常", Thread.currentThread().getId());
}
} while (orderId.get() == null);
};
long now = System.currentTimeMillis();
try {
//log接口,不知道有何作用,也许调用这个接口后,服务端做了什么处理也未可知
httpService.log(vaccineId.toString());
} catch (IOException e) {
e.printStackTrace();
}
if(now + 2000 < startDate){
if(now + 1000 < startDate){
logger.info("还未到开始时间,等待中......");
Thread.sleep(startDate-now-2000);
}
//如何保证能在秒杀时间点瞬间并发?
//提前2000毫秒开始秒杀
try {
//log接口,不知道有何作用,也许调用这个接口后,服务端做了什么处理也未可知
httpService.log(vaccineId.toString());
} catch (IOException e) {
e.printStackTrace();
}
logger.info("###########提前2秒 开始秒杀###########");
for (int i = 0; i < 20; i++) {
service.submit(task);
}
//提前1000毫秒开始秒杀
try {
//log接口,不知道有何作用,也许调用这个接口后,服务端做了什么处理也未可知
httpService.log(vaccineId.toString());
} catch (IOException e) {
e.printStackTrace();
}
do {
now = System.currentTimeMillis();
}while (now + 1000 < startDate);
logger.info("###########第一波 开始秒杀###########");
//提前500毫秒开始秒杀
do {
now = System.currentTimeMillis();
}while (now + 500 < startDate);
logger.info("###########第二波 开始秒杀###########");
for (int i = 0; i < 20; i++) {
service.submit(task);
}
try {
//log接口,不知道有何作用,也许调用这个接口后,服务端做了什么处理也未可知
httpService.log(vaccineId.toString());
} catch (IOException e) {
e.printStackTrace();
}
//提前200毫秒开始秒杀
String orderId = null;
do {
now = System.currentTimeMillis();
}while (now + 200 < startDate);
logger.info("###########第三波 开始秒杀###########");
for (int i = 0; i < 40; i++) {
service.submit(task);
}
//准点(提前20毫秒)秒杀
try {
//log接口,不知道有何作用,也许调用这个接口后,服务端做了什么处理也未可知
httpService.log(vaccineId.toString());
} catch (IOException e) {
e.printStackTrace();
}
do {
now = System.currentTimeMillis();
}while (now + 20 < startDate);
logger.info("###########第四波 开始秒杀###########");
for (int i = 0; i < 40; i++) {
service.submit(task);
}
try {
//1.直接秒杀、获取秒杀资格
long id = Thread.currentThread().getId();
logger.info("Thread ID:{},发送请求", id);
//log接口,不知道有何作用,也许调用这个接口后,服务端做了什么处理也未可知
httpService.log(vaccineId.toString());
orderId = httpService.secKill(vaccineId.toString(), "1", Config.memberId.toString(), Config.idCard);
success.set(true);
logger.info("Thread ID:{},抢购成功", id);
break;
} catch (BusinessException e) {
logger.info("Thread ID: {}, 抢购失败: {}",Thread.currentThread().getId(), e.getErrMsg());
//如果离开始时间120秒后,或者已经成功抢到则不再继续
if(System.currentTimeMillis() > startDate+1000*60*2 || success.get()){
return;
}
} catch (Exception e) {
e.printStackTrace();
logger.warn("Thread ID: {},未知异常", Thread.currentThread().getId());
}
} while (orderId == null);

service.shutdown();
if(mainFrame != null){
mainFrame.setStartBtnEnable();
}
//等待线程结束
try {
service.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
if(success.get()){
if(mainFrame != null){
mainFrame.appendMsg("抢购成功,请登录约苗小程序查看");
}
logger.info("抢购成功,请登录约苗小程序查看");
}else{
if(mainFrame != null){
mainFrame.appendMsg("抢购失败");
}
if(success.get()){
if(mainFrame != null){
mainFrame.appendMsg("抢购成功,请登录约苗小程序查看");
}
} catch (InterruptedException e) {
logger.info("抢购成功,请登录约苗小程序查看");
}else{
if(mainFrame != null){
mainFrame.appendMsg("未知异常");
mainFrame.appendMsg("抢购失败");
}
logger.info("抢购失败:",e.getCause());
}

}
Expand Down

0 comments on commit ea7a7d3

Please sign in to comment.