一、创建
二、完整代码
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();
}
}
}
}
private int remainVotes() throws IOException, InterruptedException {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = 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;
}
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;
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;
}
@RequestMapping("/remainVotes")
public int remainVotes() {
return tickets;
}
}
三、流程图解析
到此这篇关于Java多线程之简单模拟售票功能的文章就介绍到这了,更多相关Java模拟售票功能内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!