Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of Railway Station ticketing based on Java Multithreading

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report