Professional Documents
Culture Documents
Calling Multiple Synchronized Methods
Calling Multiple Synchronized Methods
Calling Multiple Synchronized Methods
Date written:
04/21/2011
Declaration:
I hereby declare that this document is based on my personal experiences of
my project. To the best of my knowledge, this document does not contain any
material that infringes the copyrights of any other individual or organization
including the customers of Infosys.
Submitted By:
Prakhar Deep Gupta
Contents
Topics Page Number
2) (Switching threads) 4
3) What is Synchronization
7
a. Thread Interference 7
b. Synchronized Methods 8
c. Intrinsic Locks and Synchronization 9
Locks In Synchronized Methods 9
4) Creating Deadlocks 9
5) How to improve concurrency 11
For e.g.
public class ExampleOfThread extends Thread
{
public void run()
{
System.out.println (“Inside Thread”);
}
}
t1.start();
t2.start();
The above code does not guarantee that thread t1 will start before t2.
Although in most of the cases this will be true, it cannot be taken for
granted.
3. All the threads called in the main method are executed after all
statements in the main thread have been executed. However,
if the main thread calls sleep(long) method as shown in the
first example above, in which case the control is relinquished
to some other thread.
printing a line
printing a line
Inside Thread
Inside Thread
And not
printing a line
Inside Thread
printing a line
This is because the main thread executes first and then the threads
inside it.
package test2;
import static java.lang.System.out;
public class AlternatingThreads
{
public static void main(String[] args) {
SyncingClass SC = new SyncingClass();
EvenPrinter even = new EvenPrinter (SC);
OddPrinter odd = new OddPrinter (SC);
}
}
Here first the threads for the Even and Odd numbers are created
and an object on which the operations need to be performed is
passed at the time of creation. Whenever some operation is to be
performed, the data members of the object that was passed is
used.
Thread Interference
class Total {
private int sum = 0;
}
Total is designed so that each call to increment will add 1 to sum, and each call
to decrement will subtract 1 from sum. However, if the SAME Total object is
referenced from multiple threads, interference between threads may not give the
expected outcomes
It might not seem possible for operations on instances of Total to interleave, since
both operations on sum are seemingly single statements. However, even simple
statements can translate to multiple steps by the virtual machine. For e.g.
The expression sum-- can be decomposed into multiple operations the same way.
1. Thread A: Retrieve c.
2. Thread B: Retrieve c.
3. Thread A: Increment retrieved value; result is 1.
4. Thread B: Decrement retrieved value; result is -1.
5. Thread A: Store result in c; c is now 1.
But this sequence is unpredictable as explained earlier. So any of the results may
be lost or the result maybe correct also.
There is no fix for it. So one has to prevent this situation from happening.
This is where synchronization comes into play.
Synchronized Methods
class Total {
private int sum = 0;
It is not possible for two invocations of synchronized methods on the same object
to interleave. When one thread is executing a synchronized method for an object,
all other threads that invoke synchronized methods for the same object block
(suspend execution) until the first thread is done with the object.
try {
Thread.sleep(50);
} catch (InterruptedException e) {
synchronized (resource2) {
System.out.println("Thread 1: locked resource 2");
}
}
}
};
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
synchronized (resource1) {
System.out.println("Thread 2: locked resource 1");
}
}
}
};
// The deadlock will occur,
// and the program will never exit.
t1.start();
t2.start();
}
}
is same as
is same as
void foo() {
synchronized(this) {
...
}
}
For e.g.
You can use any object as the lock. If you want to lock subsets of static
methods, you can
class SomeClass {
private static final Object LOCK_1 = new Object() {};
private static final Object LOCK_2 = new Object() {};
static void foo() {