千家信息网

怎么使用Java多线程来实现简单的售票功能

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,小编给大家分享一下怎么使用Java多线程来实现简单的售票功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!完整代码pack
千家信息网最后更新 2026年11月02日怎么使用Java多线程来实现简单的售票功能

小编给大家分享一下怎么使用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多线程来实现简单的售票功能"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

状态 票数 线程 售票 篇文章 剩余 功能 内容 卖票 安全 不怎么 代码 同时 多个 大部分 更多 知识 行业 资讯 资讯频道 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 耶加雪菲 科技信息互联网 腾讯阿里互联网科技 深圳互联网科技公司 深圳齐家互联网科技 广西互联网科技公司 广州善班互联网科技 成都狮子座互联网科技 互联网养老 桔豆科技 滨湖区互联网智能科技产品供应 银川新华互联网科技职业技术培训学校图片 互联网经济与金融科技 互联网科技革命什么开始 知域互联网科技有限公司大骗子 艺电能源互联网科技有限公司招聘 中国互联网公司的科技投入 互联网时代高科技布局 互联网的科技体现在哪里 互联网科技对化妆品行业 广州全民伙伴互联网科技有限公司 百单网互联网科技有限公司怎么样 广州大一互联网络科技有限公司 江苏根荣互联网科技 上海亿帐通互联网科技 盛创互联网科技有限公司 速维有品互联网信息科技(苏州)有限公司 长沙茂晟互联网科技有限公司 上海科技产业互联网平台 风投北京互联网科技 互联网科技创新创业项目
0