您好,登錄后才能下訂單哦!
小編給大家分享一下怎么使用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 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;
}
/**
* 查看總票數
*
* @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多線程來實現簡單的售票功能”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。