Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Suppose we have the following class
Java
class Class1 {

    public void Method1() {
        synchronized(myobject) {
            /* some code */
        }
    }
}

where myobject is the instance of the class
Java
class myClass {

     public void Method2() {
        synchronized(someOtherObj) {
            /* some code */
        }
     }

     public synchronized void Method3() {
        /* some code without synchronized blocks */
     }

     public void Method4() {
        /* some code without synchronized blocks */
     }
}

Help me please to understand which code blocks of myobject are made available only for the tread called the method Method1.
Posted

You could read about it in original documentation (http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html[^]), but it would be better to understand the background first. This can help more, please try to understand it thoroughly:
http://en.wikipedia.org/wiki/Mutual_exclusion[^].

The big essential feature of Java (and other systems') mutual exclusion is that the synchronization is done by some object. It means that if two different threads execute some fragments of code "guarded" by different objects, no synchronization happens, none of the thread won't wait for another one. The rationale on this is easy to explain on the example of such a simple shared resource as some set of objects in memory. Suppose you have some object set you need to keep in integrity. If you access it by some other thread, it can be broken, because one thread modifies "half of it", but another thread works in assumption that the integrity is preserved, which could lead to unpredictable results. But if some two different shared sets of objects are unrelated, it's apparent that one thread can safely modify one while any other thread is working at another one. So, each set of shared objects can be guarded by a separate synchronization object. If you analyze performance considerations, you could see that it involves less coupling and is much better for throughput. (Common threading wisdom says: "best synchronization is no synchronization".)

Due to synchronization mechanism, when a thread starts to execute a synchronized fragment of code while some other thread is already executing it, this second thread will be put to a wait queue (the queue is always needed, because more than one thread might be waiting) and put to sleep, not wasting any CPU time, until it is "waken up". This waiting thread is said to be put in a "wait state": it is switched off from and not scheduled back to execution until it is waken up. One of the waking event is releasing the lock by the thread executing the guarded fragment of code, which happens when this thread goes out of the synchronized context.

—SA
 
Share this answer
 
Comments
Member 11333197 24-Dec-14 13:10pm    
Sergey, thank you for you response. It was very helpful.
Sergey Alexandrovich Kryukov 24-Dec-14 13:27pm    
You are very welcome.
Have great holidays, my best wishes for New Year.
Good luck, call again.
—SA
Synchronized Blocks on object make that part thread-safe. Means Only one thread at a time can access that block. To access that block thread have to acquire lock then it can make operations & changes inside that block.

In short it implements the mutual exclusion.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900