Java multi-thread ~~~synchronized add parameters to implement a separate code snippet

Source: Internet
Author: User

Sometimes we don't want to add the Synchronized keyword to the whole method, which makes the whole method call slow and we're only in the key

Code to add this keyword to the synchronized, and then this will speed up the execution of the method or code. Then there may be a situation where

Yes, we have two variables, any of which can only be accessed by one variable at a time, but two variables are accessed at the same time by two variables, which

We need to use sychronized's parametric approach to achieve this requirement.


Let's look at an example, an example of a mock cinema ticket

Package Com.bird.concursey.charpet2;public class Cinema {Private long vacanciescinema1;private long vacanciesCinema2; Private Final Object controlCinema1 = new Object ();p rivate Final Object controlCinema2 = new Object ();p ublic Cinema () {VAC ANCIESCINEMA1 = 20;vacanciescinema2 = 20;} public boolean sellTickets1 (int number) {synchronized (CONTROLCINEMA1) {if (number < VACANCIESCINEMA1) { VACANCIESCINEMA1-= Number;return true;} Else{return false;}}} public boolean sellTickets2 (int number) {synchronized (CONTROLCINEMA2) {if (number < VACANCIESCINEMA2) { VACANCIESCINEMA2-= Number;return true;} Else{return false;}}} public boolean returnTicket1 (int number) {synchronized (CONTROLCINEMA1) {vacanciesCinema1 + = Number;return true;}} public boolean ReturnTicket2 (int number) {synchronized (CONTROLCINEMA2) {vacanciesCinema2 + = Number;return true;}} Public long getVacanciesCinema1 () {return vacanciesCinema1;} public void setVacanciesCinema1 (long vacanciesCinema1) {this.vacanciescinema1 = VACANCIESCINEMA1;}public long GetVacanciesCinema2 () {return vacanciesCinema2;} public void SetVacanciesCinema2 (long vacanciesCinema2) {this.vacanciescinema2 = VacanciesCinema2;}}



Here are the actual tickets for the class


Package Com.bird.concursey.charpet2;public class TicketOffice1 implements Runnable {private Cinema Cinema;public TicketOffice1 (Cinema cinema) {This.cinema = cinema;} @Overridepublic void Run () {cinema.selltickets1 (3); Cinema.selltickets1 (2); cinema.selltickets2 (2); Cinema.returnticket1 (3); Cinema.selltickets1 (5); cinema.selltickets2 (2); cinema.selltickets2 (2); CINEMA.SELLTICKETS2 (2);}}



package Com.bird.concursey.charpet2;public class TicketOffice2 implements Runnable { Private cinema cinema;public TicketOffice2 (Cinema cinema) {This.cinema = cinema;} @Overridepublic void Run () {cinema.selltickets2 (2); cinema.selltickets2 (4); Cinema.selltickets1 (2); Cinema.selltickets1 (1); Cinema.returnticket2 (2); Cinema.selltickets1 (3); cinema.selltickets2 (2); Cinema.selltickets1 (2);} public static void Main (string[] args) {Cinema cinema = new Cinema (); TicketOffice1 TicketOffice1 = new TicketOffice1 (cinema); Thread thread1 = new Thread (TicketOffice1, "TicketOffice1"); TicketOffice2 TicketOffice2 = new TicketOffice2 (cinema); Thread thread2 = new Thread (TicketOffice2, "TicketOffice2"); Thread1.start (); Thread2.start (); try {thread1.join (); Thread2.join ();} catch (Interruptedexception e) {e.printstacktrace ();} System.out.printf ("1 Vacancies:%d\n", CINEMA.GETVACANCIESCINEMA1 ()); System.out.printf ("2 Vacancies:%d\n", Cinema.getvacanciescinema2 ());}} 

As you can see here, each run will be the desired effect, and through the singleton mode, we have a good control of the two properties of the access order, very good real

We are now in demand.

Java multi-thread ~~~synchronized add parameters to implement a separate code snippet

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.