8 Replies Latest reply: Mar 25, 2013 11:53 PM by 967983 RSS

    Unable to understand the need for synchronization

    967983
      With respect to the subject line, my question is if there are 10 threads lets allot time to each thread and make other threads sleep during that time. When thread1 finishes, make thread1 to sleep and make thread2 to start and give it the time it requires to complete the task and so on. So what is the use of the synchronized keyword in java?

      Thanks in advance for the replies
        • 1. Re: Unable to understand the need for synchronization
          Kayaman
          Concurrency is quite a large chunk to explain in a post, but here are some examples:
          1. To control access to a shared resource: Imagine there's 3 guys and one cookie jar. Only one hand fits in the cookie jar at a time, so you need to synchronize access to make sure that two hands aren't taking cookies at the same time.

          2. To control access order: A person fills the cookie jar when it's empty, so he needs to wait until the jar becomes empty, and the other people need to wait while he's filling the jar.


          But it's not just about cookies. And remember that with modern multi-core CPUs, there can be more than 1 thread scheduled to run at a time.
          • 2. Re: Unable to understand the need for synchronization
            gimbal2
            Kayaman wrote:
            2. To control access order: A person fills the cookie jar when it's empty, so he needs to wait until the jar becomes empty, and the other people need to wait while he's filling the jar.
            Because otherwise a person might try to take a cookie someone else is still holding and you end up breaking the cookie in half, giving one the chocolate part and the other the vanilla part.
            • 3. Re: Unable to understand the need for synchronization
              967983
              Didn't understand the example you gave. Can you give some other example? My question is why not allot time to each thread so that they can access the shared resource for that duration?

              Suppose thread1 is using a function to print the characters "ABCD" and thread2 is trying to use the same function to print the characters "EFGH". So if we know the time taken by the function to print one character, why not calculate the time to print all the characters and allot that much time to that particular thread?

              Seriously confused...
              • 4. Re: Unable to understand the need for synchronization
                gimbal2
                964980 wrote:
                Didn't understand the example you gave.
                you don't understand it because you didn't study it yet. You first need to spend a good amount of time researching this material because as said - its not something you cover in a forum post. Its something that fills entire books.
                • 5. Re: Unable to understand the need for synchronization
                  Kayaman
                  964980 wrote:
                  My question is why not allot time to each thread so that they can access the shared resource for that duration?
                  That's kinda what's happening. For the duration of the synchronization, only one thread can perform the specified things. But time has nothing to do with it.
                  • 6. Re: Unable to understand the need for synchronization
                    967983
                    Hi Kayaman,

                    Thanks for your reply.

                    Suppose thread1 is using a function to print the characters "ABCD" and thread2 is trying to use the same function to print the characters "EFGH". So if we know the time taken by the function to print one character, why not calculate the time to print all the characters and allot that much time to that particular thread and make all other threads to sleep for that time?

                    Edited by: 964980 on Mar 25, 2013 4:29 AM
                    • 7. Re: Unable to understand the need for synchronization
                      Kayaman
                      964980 wrote:
                      So if we know the time taken by the function to print one character, why not calculate the time to print all the characters and allot that much time to that particular thread and make all other threads to sleep for that time?
                      Because then you'd be wasting time by making other threads sleep. They can be doing something useful at the same time. It's only when accessing shared resources that you need to synchronize the access.

                      Doing some sort of time calculations (even if it was possible) and making threads sleep would be a very poor and inefficient implementation.
                      • 8. Re: Unable to understand the need for synchronization
                        967983
                        Because then you'd be wasting time by making other threads sleep. They can be doing something useful at the same time. It's only when accessing shared resources that you need to synchronize the access.

                        Doing some sort of time calculations (even if it was possible) and making threads sleep would be a very poor and inefficient implementation.

                        Thanks