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

Java多線程之簡單模擬售票功能

 更新時間:2021年04月25日 10:03:28   作者:喬 三石  
這篇文章主要介紹了Java多線程之簡單模擬售票功能,文中有非常詳細的代碼示例,對正在學習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)是默認是打開狀態(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) {
                    //已售賣完 跳出循環(huán)
                    break;
                } else {
                    //賣一張票
                    int remainVotes = this.saleOneTicket();
                    System.out.println(this.getName() + "當前的票數(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();
    }

    /**
     * 賣票一張
     */
    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;

    /**
     * 賣票
     */
    @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(上鎖),避免多個線程同時獲取鎖的狀態(tài)都為0(未上鎖),從而導致線程安全問題
        lockStatus = 1;
        return lockStatus;
    }

    /**
     * 釋放鎖
     */
    @RequestMapping("/unlock")
    public synchronized int unlock() {
        return lockStatus = 0;
    }

    /**
     * 剩余票數(shù)
     *
     * @return
     */
    @RequestMapping("/remainVotes")
    public int remainVotes() {

        return tickets;
    }

}

三、流程圖解析

在這里插入圖片描述

到此這篇關于Java多線程之簡單模擬售票功能的文章就介紹到這了,更多相關Java模擬售票功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

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

    Java?String相加底層原理分析

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

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

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

    如何在IDEA中對 hashCode()和 equals() 利用快捷鍵快速進行方法重寫

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

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

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

    詳解如何獨立使用ribbon實現(xiàn)業(yè)務客戶端負載均衡

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

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

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

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

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

    Spring Boot線程池使用的一些實用心得

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

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

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

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

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

最新評論

龙井市| 旬阳县| 车险| 乡城县| 和林格尔县| 新营市| 休宁县| 蚌埠市| 郸城县| 图木舒克市| 喀喇沁旗| 尉氏县| 勃利县| 报价| 云浮市| 垦利县| 垦利县| 云和县| 武汉市| 南宁市| 禹州市| 汉阴县| 循化| 石阡县| 海城市| 新兴县| 清丰县| 墨玉县| 龙岩市| 兖州市| 汤阴县| 泰来县| 抚宁县| 巴青县| 禹城市| 正宁县| 城口县| 衡东县| 楚雄市| 绥芬河市| 宝山区|