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

How to realize the function of site ticketing by Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "Java how to achieve the site ticketing function", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java how to achieve site ticketing function" bar!

The topic requires us to create 5 threads to simulate ticket selling, which of course includes the existence of multithreading, that is, the problem we have to solve, repeated ticket selling and overselling. That is, multiple windows sell the same ticket and windows sell tickets with non-positive numbers.

But this problem can be put aside for a moment. Let's first create the basic threading model and create five threads in the main method to let them run.

Don't say much, put on the code.

Public class Ticket {public static void main (String [] args) {for (int I = 1 world I 0) {System.out.println (code + "_" + tickets--) / / after the ticket has been sold, sleep for a while and wait for other threads to operate try {Thread.sleep (100) } catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace () The complete code: public class Ticket {public static void main (String [] args) {for (int I = 1) I 0) {synchronized (lock) {/ / if the tickets are sold out, jump out of if (tickets)

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