最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java多線(xiàn)程之簡(jiǎn)單模擬售票功能

 更新時(shí)間:2021年04月25日 10:03:28   作者:?jiǎn)?三石  
這篇文章主要介紹了Java多線(xiàn)程之簡(jiǎn)單模擬售票功能,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下

一、創(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),使線(xiàn)程串行執(zhí)行
        this.locked();
        return lock;
    }

    /**
     * 改變鎖狀態(tài)
     */
    @RequestMapping("/locked")
    public synchronized int locked() {
        //更改鎖的狀態(tài)為1(上鎖),避免多個(gè)線(xiàn)程同時(shí)獲取鎖的狀態(tài)都為0(未上鎖),從而導(dǎo)致線(xiàn)程安全問(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多線(xiàn)程之簡(jiǎn)單模擬售票功能的文章就介紹到這了,更多相關(guān)Java模擬售票功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java?String相加底層原理分析

    Java?String相加底層原理分析

    這篇文章主要介紹了Java?String相加底層原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 淺談s:select 標(biāo)簽中l(wèi)ist存放map對(duì)象的使用

    淺談s:select 標(biāo)簽中l(wèi)ist存放map對(duì)象的使用

    下面小編就為大家?guī)?lái)一篇淺談s:select 標(biāo)簽中l(wèi)ist存放map對(duì)象的使用。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • 如何在IDEA中對(duì) hashCode()和 equals() 利用快捷鍵快速進(jìn)行方法重寫(xiě)

    如何在IDEA中對(duì) hashCode()和 equals() 利用快捷鍵快速進(jìn)行方法重寫(xiě)

    這篇文章主要介紹了如何在IDEA中對(duì) hashCode()和 equals() 利用快捷鍵快速進(jìn)行方法重寫(xiě),需要的朋友可以參考下
    2020-08-08
  • JAVA多線(xiàn)程搶紅包的實(shí)現(xiàn)示例

    JAVA多線(xiàn)程搶紅包的實(shí)現(xiàn)示例

    這篇文章主要介紹了JAVA多線(xiàn)程搶紅包的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 詳解如何獨(dú)立使用ribbon實(shí)現(xiàn)業(yè)務(wù)客戶(hù)端負(fù)載均衡

    詳解如何獨(dú)立使用ribbon實(shí)現(xiàn)業(yè)務(wù)客戶(hù)端負(fù)載均衡

    這篇文章主要為大家介紹了詳解如何獨(dú)立使用ribbon實(shí)現(xiàn)業(yè)務(wù)客戶(hù)端負(fù)載均衡,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • java中的Consumer、Supply如何實(shí)現(xiàn)多參數(shù)?

    java中的Consumer、Supply如何實(shí)現(xiàn)多參數(shù)?

    Java的Consumer接口只能接受一個(gè)參數(shù),但可以通過(guò)自定義接口、使用Tuple或嵌套結(jié)構(gòu)來(lái)實(shí)現(xiàn)對(duì)多個(gè)參數(shù)的處理,對(duì)于Supplier接口,它不能接受參數(shù),但可以通過(guò)自定義BiSupplier、結(jié)合Function或封裝參數(shù)為對(duì)象來(lái)實(shí)現(xiàn)對(duì)兩個(gè)參數(shù)并返回一個(gè)值的功能
    2024-11-11
  • Springcloud hystrix服務(wù)熔斷和dashboard如何實(shí)現(xiàn)

    Springcloud hystrix服務(wù)熔斷和dashboard如何實(shí)現(xiàn)

    這篇文章主要介紹了Springcloud hystrix服務(wù)熔斷和dashboard如何實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Spring Boot線(xiàn)程池使用的一些實(shí)用心得

    Spring Boot線(xiàn)程池使用的一些實(shí)用心得

    理論上線(xiàn)程越多程序可能更快,但在實(shí)際使用中我們需要考慮到線(xiàn)程本身的創(chuàng)建以及銷(xiāo)毀的資源消耗,以及保護(hù)操作系統(tǒng)本身的目的我們通常需要將線(xiàn)程限制在一定的范圍之類(lèi),這篇文章主要給大家介紹了關(guān)于Spring Boot線(xiàn)程池使用的一些實(shí)用心得,需要的朋友可以參考下
    2021-09-09
  • SpringBoot整合Servlet和Filter和Listener組件詳解

    SpringBoot整合Servlet和Filter和Listener組件詳解

    這篇文章主要介紹了SpringBoot整合Servlet和Filter和Listener組件詳解,在整合某報(bào)表插件時(shí)就需要使用Servlet,Spring Boot中對(duì)于整合這些基本的Web組件也提供了很好的支持,需要的朋友可以參考下
    2024-01-01
  • Java 創(chuàng)建并應(yīng)用PPT幻燈片母版的方法示例

    Java 創(chuàng)建并應(yīng)用PPT幻燈片母版的方法示例

    幻燈片母版可供用戶(hù)設(shè)置幻燈片的樣式,本文將介紹如何用Java創(chuàng)建并應(yīng)用單個(gè)或多個(gè)幻燈片母版。感興趣可以了解一下
    2020-06-06

最新評(píng)論

南城县| 齐河县| 三穗县| 岳阳市| 黑龙江省| 华宁县| 拉萨市| 金川县| 吉水县| 承德市| 连平县| 庆阳市| 肇州县| 二手房| 台中市| 桦南县| 蓝山县| 祁门县| 德庆县| 团风县| 财经| 彰化县| 新源县| 甘泉县| 上栗县| 方城县| 邓州市| 朝阳区| 宾阳县| 锦州市| 江门市| 泰来县| 贺兰县| 鄂尔多斯市| 塔城市| 宁津县| 宾川县| 揭西县| 合山市| 石家庄市| 长乐市|