Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of Railway Station ticketing based on Java Multithreading

Shulou Source: shulou.com Published: 2022-06-02 03:14:43 09月28日 Update

This article introduces Java multithreaded train station ticketing example analysis, the content is very detailed, interested friends can refer to, hope to be helpful to everyone.

Package com.thread;/** title: people line up at the ticket window of the railway station to buy train tickets. The door of Beijing West Railway Station 2. Open the ticket window 3. There are 10 tickets to Changsha in Beijing West Railway Station. Open 2 ticket windows, 5 suppose each window buys a ticket every 1 second. According to the nouns Person, railway station (Station), train ticket (Ticket), ticket window e is the thread of the railway station * / import java.util.*;public class TestTrain {public static void main (String [] args) {/ / the train west station opens Station station = new Station ("train west station") / / Open window / / Thread win1=station.openWindow ("normal window"); Thread win2 = station.openWindow ("student window"); / / window 1 start selling tickets / / set thread priority setPriority () / / win2.setPriority (Thread.MAX_PRIORITY); / / win1.start () Win2.start (); try {Thread.sleep (2000); / / when the thread stops stop () / / interrupt () interrupts the thread / / win2.stop (); win2.suspend () / / Exhibition Hall Thread.sleep (4000); win2.resume ();} catch (Exception e) {e.printStackTrace ();} class Station implements Runnable {private String name / name of railway station private String [] persons = {"Zhao Haiyang", "Wang Rui", "Song Sha", "Li Xinwei", "Xu Wenxiao", "Meng Lingqiang", "Wang David", "Li Baoqin", "Wang Hao"}; / / people waiting in line to buy tickets at the railway station private int i =-1; public Station (String name) {super () This.name = name;} / / Open ticket window Student window, normal window, military window / / ticket window is thread Thread public Thread openWindow (String name) {return new Thread (this, name) } / / Railway Station ticketing public void sale () {try {while (I < persons.length-1) {iPrefecture; String person = persons [I] System.out.println (person + "" + Thread.currentThread (). GetName () + "bought a train ticket from Beijing to Shanghai"); Thread.sleep (2000);}} catch (Exception e) {e.printStackTrace () } @ Override public void run () {/ / start selling tickets sale ();} public String getName () {return name;} public void setName (String name) {this.name = name }} on the Java multi-threaded railway station ticketing example analysis is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Train ticketing railway station thread west station train ticket example case analysis analysis ordinary people content students more Beijing Beijing West Railway Station help good priority interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft OPPO Reno Shulou Technology vpn Shulou Information