Java多線程之簡(jiǎn)單模擬售票功能
一、創(chuàng)建
二、完整代碼
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 (; ; ) { //鎖的狀態(tài)是默認(rèn)是打開(kāi)狀態(tài) //獲取鎖的狀態(tài) int lockStatus = this.findLockStatus(); if (lockStatus == 0) { //修改鎖的狀態(tài) =>>鎖定 this.locked(); //獲取總票數(shù) int tickets = this.findTickets(); //剩余票數(shù) int i = this.remainVotes(); //判斷票數(shù) if (tickets < 1) { //已售賣(mài)完 跳出循環(huán) break; } else { //賣(mài)一張票 int remainVotes = this.saleOneTicket(); System.out.println(this.getName() + "當(dāng)前的票數(shù):" + tickets); System.out.println(this.getName() + "售票后:" + remainVotes); // 釋放鎖 this.unlock(); } } } } /** * 剩余票數(shù) * * @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(); } /** * 賣(mài)票一張 */ 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; } /** * 獲取鎖的狀態(tài) */ 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; } /** * 修改鎖狀態(tài) */ 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; } /** * 查看總票數(shù) * * @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 { /** * 總票數(shù) */ private static Integer tickets = 100; /** * 鎖的狀態(tài) 0:未鎖 1:鎖 */ private static Integer lockStatus = 0; /** * 賣(mài)票 */ @RequestMapping("/saleOneTicket") public Integer saleOneTicket() { return tickets = tickets - 1; } /** * 查看總票數(shù) */ @RequestMapping("/findTickets") public Integer findTickets() { return tickets; } /** * 查看鎖的狀態(tài) */ @RequestMapping("/findLock") public synchronized Integer findLock() { Integer lock=lockStatus; //改變鎖狀態(tài),使線程串行執(zhí)行 this.locked(); return lock; } /** * 改變鎖狀態(tài) */ @RequestMapping("/locked") public synchronized int locked() { //更改鎖的狀態(tài)為1(上鎖),避免多個(gè)線程同時(shí)獲取鎖的狀態(tài)都為0(未上鎖),從而導(dǎo)致線程安全問(wèn)題 lockStatus = 1; return lockStatus; } /** * 釋放鎖 */ @RequestMapping("/unlock") public synchronized int unlock() { return lockStatus = 0; } /** * 剩余票數(shù) * * @return */ @RequestMapping("/remainVotes") public int remainVotes() { return tickets; } }
三、流程圖解析
到此這篇關(guān)于Java多線程之簡(jiǎn)單模擬售票功能的文章就介紹到這了,更多相關(guān)Java模擬售票功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
hashtable桶數(shù)通常會(huì)取一個(gè)素?cái)?shù)分析
這篇文章主要介紹了hashtable桶數(shù)通常會(huì)取一個(gè)素?cái)?shù)分析的相關(guān)資料,需要的朋友可以參考下2016-12-12SpringBoot實(shí)現(xiàn)短信發(fā)送及手機(jī)驗(yàn)證碼登錄
本文主要介紹了SpringBoot實(shí)現(xiàn)短信發(fā)送及手機(jī)驗(yàn)證碼登錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07使用jdbcTemplate查詢返回自定義對(duì)象集合代碼示例
這篇文章主要介紹了使用jdbcTemplate查詢返回自定義對(duì)象集合代碼示例,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02Java基于socket編程相關(guān)知識(shí)解析
這篇文章主要為大家詳細(xì)解析了Java基于socket編程的相關(guān)知識(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09從Hello?World開(kāi)始理解GraphQL背后處理及執(zhí)行過(guò)程
這篇文章主要為大家介紹了從Hello?World開(kāi)始理解GraphQL背后處理過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08SpringBoot集成shiro,MyRealm中無(wú)法@Autowired注入Service的問(wèn)題
今天小編就為大家分享一篇關(guān)于SpringBoot集成shiro,MyRealm中無(wú)法@Autowired注入Service的問(wèn)題,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03springBoot配置國(guó)產(chǎn)達(dá)夢(mèng)數(shù)據(jù)庫(kù)的示例詳解
本文向大家介紹springBoot?配置國(guó)產(chǎn)達(dá)夢(mèng)數(shù)據(jù)庫(kù)的相關(guān)知識(shí),文章結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04