java-using multi-threaded runnable, common one parameter problem

Source: Internet
Author: User

Case:

The salesman sells the ticket question, a tourist attraction has 40 tickets, opened two buys the ticket window, uses the thread way, carries on the simulation salesman to sell the ticket process, and carries on the analysis.

Idea: The 40 tickets of the attraction is a pool of shared tickets, when the ticket is made, the number of tickets confirmed the ticket number-1, in order to ensure that the tickets sold are not the same ticket, must be locked and votes to judge

Enables two threads to share a secure data to ensure data security.

//thread function: Mythread3.java
/*** Ticket Thread * To simulate the ticket thread, use the Runnable interface to implement the override run method to ensure thread security *@author[Light] (required) *@see[Run ()] (optional) *@since[version 1.0] (required)*/ Public classMyThread3Implementsrunnable{//Number of tickets intNum=40; //Set the lock flagObject lock=NewObject (); @Override Public voidrun () {//TODO auto-generated Method Stub//Cycle Start Ticketing while(num>0){ //Lock, get the lock synchronized(lock) {//decide whether to sell out if(num>0) {//simulate the ticketing processSystem.out.println (Thread.CurrentThread (). GetName () + "sell first" +num+ "Ticket"); Num--; } Try{//to sleep, temporarily abandon the use of the CPU, but do not release the lockThread.CurrentThread (). Sleep (500); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } }}
Main function Mytest.java/************************************************************ Copyright (C), 1988-1999, Huawei Tech. Co., Ltd. FileName:test.cpp author:light version:version1.0date:2018/7/11description://Analogue Sales Ticket version://Release Information 1. Open two processes and implement multi-threading through the Runnable interface 2. Guaranteed two processes common one ticket parameter function List://main function and its function 1.Th RED3 Ticketing thread 2. Thred3.start (); Open thread History://Historical change record <author> <time> <version > <desc> Light 2018/7/11 1.0 Build this moudle ***********************************************************/ Public classMyTest {//Main function Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//Creating MyThread3 ThreadsRunnable mythread3=NewMyThread3 (); //Open Two ThreadsThread t1=NewThread (MYTHREAD3); Thread T2=NewThread (MYTHREAD3); T1.start (); T2.start (); }}

The result of the final operation:

As can be seen from the results, the open two threads common to the same ticket pool, for ticketing, through the lock, to achieve each ticket for each person's sale, the final example of simulation.

java-using multi-threaded runnable, common one parameter problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.