5 Replies Latest reply on Sep 16, 2012 4:54 AM by DrClap

    Is synchronization 100% safe?

      If a thread is executing a synchronized block, does it have to complete the method fully before another thread accesses the block?
      It will release the lock on the object only after it exits the method right?
      What happens if the thread is interrupted or goes into wait? Does it release the lock? If the lock is released, then another thread can enter the synchronized method and modify any global variables, thus making synchronization useless right?
        • 1. Re: Is synchronization 100% safe?
          There's a tutorial about that: Lesson: Concurrency. You'll generally find it faster and more reliable to just go through the tutorial, rather than waiting for random strangers to give you answers which have random levels of correctness.
          • 2. Re: Is synchronization 100% safe?
            user5560729 wrote:
            thus making synchronization useless right?
            You should really read the tutorial to understand synchronization. It's obvious you don't.
            • 3. Re: Is synchronization 100% safe?
              Thanks for the tip...i'll do that. It's not that I don't know anything about synchronization. Sometimes I get weird questions in my mind like this one that I don't feel bad about to ask someone else.
              • 4. Re: Is synchronization 100% safe?
                It's not about whether you feel bad or not. It's about the most efficient and reliable way of answering your doubts. Everything you have asked here is specified and documented. You will get the right answer there. You could get any gibberish on forums, and you could waste a lot of time, or infinite time, waiting for any answer at all, let alone sorting the wheat from the chaff.
                • 5. Re: Is synchronization 100% safe?
                  Look at it this way: millions of people have written applications in Java, many of them very successful applications which use synchronization heavily. Now, what's the probability that somebody who is new to Java will find out that synchronization doesn't work properly? As I'm sure you can see, the probability is zero.

                  And therefore the answer to your question is already clear. It just remains for you to find out why. And while you're doing that, you might as well learn about synchronization. Hence the tutorial.