• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java多线程之简单模拟售票功能

java 搞代码 4年前 (2022-01-05) 47次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了Java多线程之简单模拟售票功能,文中有非常详细的代码示例,对正在学习java的小伙伴们有很好地帮助,需要的朋友可以参考下

一、创建

二、完整代码

 package com.ql; import lombok.SneakyThrows; import okhttp3.Call; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import java.io.IOException; public class Mythread extends Thread { public Mythread(String name) { super(name); } @SneakyThrows @Override public void run() { for (; ; ) { //锁的状态是默认是打开状态 //获取锁的状态 int lockStatus = this.findLockStatus(); if (lockStatus == 0) { //修改锁的状态 =>>锁定 this.locked(); //获取总票数 int tickets = this.findTickets(); //剩余票数 int i = this.remainVotes(); //判断票数 if (tickets <1) { //已售卖完 跳出循环 break; } else { //卖一张票 int remainVotes = this.saleOneTicket(); System.out.println(this.getName() + "当前的票数:" + tickets); System.out.println(this.getName() + "售票后:" + remainVotes); //  释放锁 this.unlock(); } } } } /** * 剩余票数 * * @return * @throws IOException */ private int remainVotes() throws IOException, InterruptedException { OkHttpClient okHttpClient = new OkHttpClient(); Request req<b style="color:transparent">来源gao@!dai!ma.com搞$$代^@码网</b>uest = new Request.Builder().url("http://localhost:8080/lock/remainVotes").build(); Call call = okHttpClient.newCall(request); Response response = call.execute(); String string = response.body().string(); int ticketsVote = Integer.parseInt(string); return ticketsVote; } /** * 释放锁 */ private void unlock() throws IOException { OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("http://localhost:8080/lock/unlock").build(); Call call = okHttpClient.newCall(request); Response response = call.execute(); } /** * 卖票一张 */ private int saleOneTicket() throws IOException { OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("http://localhost:8080/lock/saleOneTicket").build(); Call call = okHttpClient.newCall(request); Response response = call.execute(); String string = response.body().string(); int remainVotes = Integer.parseInt(string); return remainVotes; } /** * 获取锁的状态 */ private int findLockStatus() throws IOException { OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("http://localhost:8080/lock/findLock").build(); Call call = okHttpClient.newCall(request); Response response = call.execute(); String string = response.body().string(); int lockStatus = Integer.parseInt(string); return lockStatus; } /** * 修改锁状态 */ private int locked() throws IOException { OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("http://localhost:8080/lock/locked").build(); Call call = okHttpClient.newCall(request); Response response = call.execute(); String string = response.body().string(); int lockStatus = Integer.parseInt(string); return lockStatus; } /** * 查看总票数 * * @throws IOException */ private int findTickets() throws IOException { OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("http://localhost:8080/lock/findTickets").build(); Call call = okHttpClient.newCall(request); Response response = call.execute(); String string = response.body().string(); Integer tickets = Integer.parseInt(string); return tickets; } } 
 package com.ql; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/lock") public class ClientService { /** * 总票数 */ private static Integer tickets = 100; /** * 锁的状态 0:未锁   1:锁 */ private static Integer lockStatus = 0; /** * 卖票 */ @RequestMapping("/saleOneTicket") public Integer saleOneTicket() { return tickets = tickets - 1; } /** * 查看总票数 */ @RequestMapping("/findTickets") public Integer findTickets() { return tickets; } /** * 查看锁的状态 */ @RequestMapping("/findLock") public synchronized Integer findLock() { Integer lock=lockStatus; //改变锁状态,使线程串行执行 this.locked(); return lock; } /** * 改变锁状态 */ @RequestMapping("/locked") public synchronized int locked() { //更改锁的状态为1(上锁),避免多个线程同时获取锁的状态都为0(未上锁),从而导致线程安全问题 lockStatus = 1; return lockStatus; } /** * 释放锁 */ @RequestMapping("/unlock") public synchronized int unlock() { return lockStatus = 0; } /** * 剩余票数 * * @return */ @RequestMapping("/remainVotes") public int remainVotes() { return tickets; } } 

三、流程图解析

以上就是Java多线程之简单模拟售票功能的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java多线程之简单模拟售票功能
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址