How to use Java multithreading to realize simple ticketing function
Editor to share with you how to use Java multithreading to achieve a simple ticketing function, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The complete code 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 (; ) {/ / the state of lock is open by default / / the state of acquiring lock is int lockStatus = this.findLockStatus (); if (lockStatus = = 0) {/ / modify the status of lock = > > lock this.locked (); / / get total votes int tickets = this.findTickets () / / remaining number of votes int I = this.remainVotes (); / / determine the number of votes if (tickets < 1) {/ / sold out of the cycle break } else {/ / sell a ticket int remainVotes = this.saleOneTicket (); System.out.println (this.getName () + "current number of tickets:" + tickets); System.out.println (this.getName () + "Post-sale:" + remainVotes) / / release lock this.unlock ();}} / * * remaining votes * * @ 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 } / * release lock * / 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 ()) } / * sell one ticket * / 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;} / * * get the status of the lock * / 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;} / * * modify lock status * / 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;} / * View the total number of votes * * @ 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 {/ * Total votes * / private static Integer tickets = 100; / * status of lock 0: unlocked 1: lock * / private static Integer lockStatus = 0 / * selling tickets * / @ RequestMapping ("/ saleOneTicket") public Integer saleOneTicket () {return tickets = tickets-1;} / * * check the total number of votes * / @ RequestMapping ("/ findTickets") public Integer findTickets () {return tickets } / * check the state of the lock * / @ RequestMapping ("/ findLock") public synchronized Integer findLock () {Integer lock=lockStatus; / / change the lock state so that the thread executes this.locked () serially; return lock } / * change lock state * / @ RequestMapping ("/ locked") public synchronized int locked () {/ / change lock state to 1 (locked) to prevent multiple threads from acquiring lock status 0 (unlocked) at the same time, resulting in thread safety problem lockStatus = 1; return lockStatus } / * release lock * / @ RequestMapping ("/ unlock") public synchronized int unlock () {return lockStatus = 0;} / * remaining votes * * @ return * / @ RequestMapping ("/ remainVotes") public int remainVotes () {return tickets }} above are all the contents of this article entitled "how to use Java multithreading to achieve simple ticketing functions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!