so we're now going to talk about barrier synchronization which is a very interesting capability that's provided in a number of different ways by java and so in this particular first part of the lesson i'm going to explain to you what barrier synchronization is and also describe three different ways to use barrier synchronizers in java as is our tradition we'll also talk about a human known use of barrier synchronization as well so the earlier discussions we've had about java synchronizers have largely focused on classes and synchronizer classes of course that affect the behavior of individual threads so for example we saw how atomic operations are actions that either happen all at once or not at all we took a look at mutual exclusion synchronizers including reader writer locks that allow concurrent access and updates to shared mutable data within critical sections and then we also talked about coordination synchronizers that ensure that all computations run properly in the right order at the right time and under the right conditions etc so in contrast to those approaches a barrier is a synchronizer that ensures that a group of threads have to stop at a certain point and cannot proceed until all the other threads that are part of the group reach the barrier so there's a bunch of examples i like to use a good one would be for example the starting gate for a horse race or the starting blocks for some kind of sprint or marathon where all the contestants all the participants have to begin at the same time and we'll see that there's a couple different variants here that are important to understand in java there are sort of three different ways of using barriers and to make this example fun we're going to use a video rendering engine as a running example in this part of the lesson so the first way of being able to do things is what's called an entry barrier and this is used to keep current concurrent computations from running until all the objects are fully initialized so let's say for example we want to do a bunch of video rendering and we want to start the videos rendered at the same time and we've got a pool of threads let's say we have four threads in the pool because we maybe we have eight cores or four cores or 16 cores but we only want to limit ourselves to 4 and so what will happen here is we want the main thread to spawn a bunch of these worker threads and then the main thread will perform some type of perhaps time-consuming initialization of data structures before it lets the threads to start running and doing their rendering work so the way we're going to do this is we're going to use an entry barrier and that entry barrier is going to start out with a certain count and what we're going to do is we're going to make all the threads wait on the entry barrier until the main thread completes its initializations and when the main threads done with its initializations then it'll decrement the entry barrier to zero thereby informing all the worker threads that they can begin to run and they can all do their rendering computations so that's an entry barrier another approach kind of at the other end of the spectrum if you will is an exit barrier and that says don't let a thread continue until a group of concurrent threads have finished their processing so in this case we're going to have a main thread that's going to wait on an exit barrier for all the worker threads to finish in fact both entry barriers and exit barriers are often used together in the same program where the main thread will do the entry barrier to let the other thread start to run and then it will wait on an exit barrier for all the threads to finish their computations so in this case let's have ourselves have an exit barrier which we call m conversion done barrier and that'll start with an initial count of four and then as each of the threads finishes its image processing computations it will decrement the count by one until it finally reaches zero and when the exit barrier count equals zero then and only then can the main thread continue to do its processing yet another type of barrier in java is called a cyclic barrier and this is kind of a variant on entry and exit barriers but what works here is you have a group of threads that all have to wait for each other to reach a certain point in their processing cycle before they can advance to the next cycle so let's say for sake of argument we have a fixed or variable size threads pool of threads that can all run concurrently and at the end of each cycle then a decision is made about whether to keep going or not and so that's what a cyclic barrier is now let's take a quick look at some examples of human known uses of barrier synchronization the kinds of things you'd find in in everyday life so one example that we'll use here is a the protocol that's used by a museum tour guide so let's say remember back in the day when we actually went to do events or museums or something and before the pandemic and you might have a group of people that all want to go walk through a museum and be guided by a tour guide to talk about sculpture or paintings or whatever so we might start out with an entry barrier so we might say all the tourists in a given group like let's say that the 130 tour group wait outside the museum either until it opens or until the next tour is scheduled to begin so you can't go in the museum until it opens or until it's time for the tour then you might also have an exit barrier where the museum can actually close down only after the last group of tourists leave so we have to wait to close things down until everybody's done and has left the the museum and then you might also have a cyclic barrier where the tour guide waits for all the tourists to finish exploring a given room before then continuing the tour in the next room so all the tourists work together kind of as a group and they all go wander around and look at the paintings or the sculptures or whatever and then they all go in mass into the next room so that would be an example of a cyclic barrier as we'll see when we go a little bit more in terms of the features that are available in java cyclic barriers can be used either as entry or as exit barriers so we'll also see that there's ways in which barriers can be used for either a fixed number or a variable size number of participants or tourists in this particular example and different features that are available in java are used to cover these things in different ways so that's the end of our overview and our introduction to barrier synchronization i mentioned a little bit about java as we go through this but we won't really talk about the actual classes involved until we get to the next part of this lesson