Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java multithread to realize train ticketing system

Shulou Source: shulou.com Published: 2022-06-02 12:56:56 09月13日 Update

This article mainly explains "how to use Java multithreading to achieve train ticketing system", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use Java multithreading to achieve train ticketing system" bar!

Java multithreaded train ticketing (the difference between Thread and Runnable)

There are two ways to implement multithreading in java.

Inheriting Thread class to implement Runnable interface

Inherits the class of the Thread class, uses the object .start () to start the thread to implement the class of the Runnable class, and uses new Thread (the implementation class of new Runnable ()). Start () to start

Here is a classic example that works well with a class that implements the Runnable interface.

A simple analysis of train ticketing:

1. The number of votes is shared, so you can't buy a ticket multiple times. two。 At the same time, there are multiple ticket outlets selling tickets, that is, multiple threads exist. 3. When a ticket office is selling tickets, other ticket outlets cannot intervene, and synchronous locks can be used here.

The code is as follows:

Test class

Public class TicketsTest {public static void main (String [] args) {SakeTickets st = new SakeTickets (); Thread [] t = new Thread [3]; for (int I = 0; I

< t.length; i++) { t[i] = new Thread(st,"售票口"+(i+1)); t[i].start(); } } } 实现Runnable接口的售票类 public class SakeTickets implements Runnable{private int totalTickets =200;public void run(){ while(totalTickets>

0) sale ();} public void sale () {synchronized (this) {String name = Thread.currentThread () .getName (); if (totalTickets==0) System.out.println (name+ "tickets are sold out.") ; else {totalTickets--; try {Thread.sleep (57);} catch (InterruptedException e) {e.printStackTrace ();} System.out.printf (name+ "sold one ticket, left:%-3D.\ n", totalTickets);}

The difference between Thread and Runnable

If a class inherits Thread, it is not suitable for resource sharing. However, if you implement the Runable interface, it is easy to achieve resource sharing.

Thank you for reading, the above is the content of "how to use Java multithread to achieve train ticketing system". After the study of this article, I believe you have a deeper understanding of how to use Java multithread to achieve train ticketing system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Ticketing threads trains systems interfaces ticket outlets learning content multiple resources resource sharing problems codes examples and at the same time ticket outlets objects that is ideas situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno Huawei Apple MariaDB