When you use the Synchronized keyword to protect a block of code, you must refer to the object reference as an incoming parameter. Typically, you use the this keyword to refer to the object to which the execution method belongs, or you can use other objects to reference it. In general, these objects are created for this purpose . For example, there are two non-dependent properties in a class that are shared by multiple threads, and you have to synchronize access to each variable, but only one thread is allowed to access one property variable at a time, and one other thread accesses another property variable.
Here we demonstrate the cinema ticketing scene. This example simulates a movie theater with two screens and two ticket offices. A ticket sold at a set of tickets can only be used on one of the screens and cannot be used for both screens, so the remaining number of votes per movie theater is a separate attribute.
Example:
package concurrency;public class main9 { 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 { //wait for the end of these two threads!! thread1.join (); thread1.join (); } catch (interruptedexception e) { e.printstacktrace (); } system.out.printf ("room 1 vacancies: %d\n", &NBSP;CINEMA.GETVACANCIESCINEMA1 ()); system.out.printf ("The 2 vacancies: %d\n ", cinema.getvacanciescinerna2 ()); }}// Cinema class class cinema{ private long vacanciescinema1; private long vacanciescinema2; private final object Controlcinema1,controlcinema2; public cinema () {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CONTROLCINEMA1 = new object (); controlcinema2 = new object (); vacanciescinema1 = 20; vacanciesCinema2 = 20; } public boolean selltickets1 (Int number) { synchronized (CONTROLCINEMA1) { if (NUMBER&NBSP;<&NBSP;VACANCIESCINEMA1) { vacanciesCinema1 -= number; return true; }else{ return false; } } } public Boolean selltickets2 (Int number) { synchronized (CONTROLCINEMA2) { if (number &NBSP;<&NBSP;VACANCIESCINEMA2) { vacanciesCinema2 -= number; return true; }else{ return false; } } } public boolean ReturnTickets1 (Int number) { synchronized ( CONTROLCINEMA1) &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VACANCIESCINEMA1 += number; return true; } } public Boolean returntickets2 (int number) { synchronized (CONTROLCINEMA2) { vacanciescinema2 += number; return true; } } public long GETVACANCIESCINEMA1 () { return vacanciescinema1; } public long getvacanciescinerna2 () { return vacanciescinema2; }}//Ticket Office 1class ticketoffice1 implements runnable{ private cinema cinema; public ticketoffice1 (Cinema cinema) { this.cinema = cinema; } //simulation of the operation of the cinema @Override public void run () { cinema.selltickets1 (3); Cinema.selltickets1 (2); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CINEMA.SELLTICKETS2 (2); cinema.returntickets1 (3); cinema.selltickets1 (5); &NBSP;CINEMA.SELLTICKETS2 (2); cinema.selltickets2 (2); }}//Ticket Office 2class ticketoffice2 implements runnable{ Private cinema cinema; public ticketoffice2 (Cinema cinema) { this.cinema = cinema; } //Analogue to Cinema operation @Override public void run () { cinema.selltickets2 (2); &NBSP;&NBSP;&NBSP;CINEMA.SELLTICKETS2 (4); cinema.selltickets1 (2 ); &NBSP;&NBSP;&NBSP;&NBSP;&NBsp; cinema.selltickets1 (1); CINEMA.RETURNTICKETS2 (2); cinema.selltickets1 (3); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CINEMA.SELLTICKETS2 (2); Cinema.selltickets1 (2); }}
When you protect a block of code with the Synchronized keyword, we use the object as its incoming parameter. The JVM guarantees that only one thread at a time can access this object-protected code protection block ( Note: We have been talking about objects, not classes ).
Note: This example uses an object to control access to the VacanciesCinema1 property, so only one thread can modify the property at the same time, and another object is used to control access to the VacanciesCinema2 property. So only one thread can modify this property at the same time. However, this example allows running two threads at the same time: one modifies the VacancesCinema1 property and the other modifies the VacanciesCinema2 property.
Synchronization based on thread synchronization using non-dependency properties