Java Concurrency Learning 12--Scheduling independent properties in a synchronized class

Source: Internet
Author: User

This article is a summary of the study of the article on the network, thank you for your selfless sharing.

When you use the Synchronized keyword to protect a block of code, you must pass a reference to an object as a parameter. Typically, you will use the This keyword to refer to the object that executes the method, but you can also use a different object reference. Typically, these objects are created only for this purpose. For example, you have two independent properties in a class that are shared by multiple threads. You have to synchronize access to each variable, and if there is one thread that accesses one property and another thread accesses another at the same time, there is no problem.

When you use the Synchronized keyword to protect a block of code, you use an object as a parameter. The JVM can guarantee that only one thread can access that object to protect all blocks of code (note that we are always talking about objects, not classes).

< Span style= "Color:rgb (102,102,102); font-size:14px; line-height:35px ">

Package Chapter2;public class Cinema {Private long vacanciescinema1;private long vacanciescinema2;private final Object Co Ntrolcinema1,controlcinema2;public Cinema () {controlCinema1 = new object (); controlCinema2 = new Object (); VACANCIESCINEMA1 = 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 returnTickets1 (int number) {synchronized (CONTROLCINEMA1) {vacanciesCinema1 +=number;return true;}} public boolean returnTickets2 (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;}}

Package Chapter2;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.returntickets1 (3); Cinema.selltickets1 (5); cinema.selltickets2 (2); cinema.selltickets2 (2); CINEMA.SELLTICKETS2 (2);}}

Package Chapter2;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.returntickets2 (2); Cinema.selltickets1 (3); cinema.selltickets2 (2); Cinema.selltickets1 (2);}}

Package Chapter2;public class Main3 {/** *  * <p> * </p> * @author Zhangjunshuai * @date 2014-9-15 PM 4:52:1 2 * @param args */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 (Exception e) {e.printstacktrace ();} System.out.printf ("1 Vacancies:%d\n", CINEMA.GETVACANCIESCINEMA1 ()); System.out.printf ("2 Vacancies:%d\n", Cinema.getvacanciescinema2 ());}}


Java Concurrency Learning 12--Scheduling independent properties in a synchronized class

Related Article

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.