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 use Java to simulate multithreading to realize ticket grabbing code

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use Java to simulate multithreading to achieve ticket grabbing code". In daily operation, I believe many people have doubts about how to use Java to simulate multithreading to achieve ticket grabbing code. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for everyone to answer the question of "how to use Java to simulate multithreading to achieve ticket grabbing code". Next, please follow the editor to study!

Realize the demo of snapping up 100tickets

Here you need a variable to hold 100 sheets.

Local variables:

Defined in the method, the method exists, the method is destroyed at the end of the run, and a persistent data cannot be saved!

Member variables:

Saved in the class object, it exists after the object is created, and the object does not destroy member variables and will not be reclaimed by memory. Because in each class object, there is a corresponding member variable, these member variables are not the same data. Not sharing resources, not suitable!

Static member variables:

Saved in the [data area] of memory, the class file exists when it is loaded and can be used by every class object, and the modification is affected everywhere! It is very suitable as a shared resource!

problem

1. There is an abnormal sales order.

two。 There is a ticket that has been sold many times

Use Lock

The code is as follows

Class SaleThread implements Runnable {/ * uses static member variables as saving variables for 100 tickets and is a shared resource. * / private static int tickets = 100; @ Override public void run () {/ / complete the ticketing process while (true) {/ * string can be used as a lock object, because the string contained in double quotes has one and only one * / synchronized ("lock") {try {Thread.sleep (500);} catch (InterruptedException e) {e.printStackTrace () } if (tickets > 0) {System.out.println (Thread.currentThread (). GetName () + "sold" + tickets + "ticket"); tickets--;} else {System.out.println (Thread.currentThread (). GetName () + "sold out!!") ; break;}} public class Demo {public static void main (String [] args) {Thread T1 = new Thread (new SaleThread (), "ticket seller 1"); Thread T2 = new Thread (new SaleThread (), "ticket seller 2"); Thread T3 = new Thread (new SaleThread (), "ticket seller 3"); t1.start (); t2.start (); t3.start ();}}

At this point, the study of "how to use Java to simulate multithreading to achieve ticket grabbing code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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