Reputation: 644
I had been trying out different behavior of multi-threading in java. If i am using both synchronized static and non static method in a class. What i had understood is,
-> if the thread enters into a synchronize method the thread acquire the lock of an object until the execution of method.
-> if the thread enters into a static synchronize method the thread acquire the lock of a class instead of an object.
The Real confusion part is the output ??.
package com.threadImplementaion.examples;
class MyRunable implements Runnable
{
public void run()
{
iterationMethod() ;
staticIteration();
}
public synchronized void iterationMethod()
{
//int count = 0 ;
for(int i = 0 ; i < 5; i++ )
{
System.out.println( Thread.currentThread().getName() + " : " + i);
}
}
public static synchronized void staticIteration()
{
for(int i = 0 ; i < 10 ; i++ )
{
System.out.println(Thread.currentThread().getName() + " static " + i);
}
}
}
public class MyRunnable
{
public static void main(String[] args) {
Runnable runnable = new MyRunable() ;
Thread thread1 = new Thread(runnable);
thread1.start();
thread1.setName("Thread1");
Thread thread2 = new Thread(runnable) ;
thread2.start();
thread2.setName("Thread2") ;
Thread thread3 = new Thread(runnable);
thread3.start();
thread3.setName("Thread3");
}
}
Output :
Thread1 : 0
Thread1 : 1
Thread1 : 2
Thread1 : 3
Thread1 : 4
Thread1 static 0
Thread1 static 1
Thread1 static 2
Thread1 static 3
Thread1 static 4
Thread1 static 5
**Thread1 static 6**
Thread3 : 0
Thread3 : 1
Thread3 : 2
Thread3 : 3
Thread3 : 4
Thread2 : 0
Thread2 : 1
Thread1 static 7
Thread2 : 2
Thread1 static 8
Thread2 : 3
Thread1 static 9
Thread2 : 4
Thread2 static 0
Thread2 static 1
Thread2 static 2
Thread2 static 3
Thread2 static 4
Thread2 static 5
Thread2 static 6
Thread2 static 7
Thread2 static 8
Thread2 static 9
Thread3 static 0
Thread3 static 1
Thread3 static 2
Thread3 static 3
Thread3 static 4
Thread3 static 5
Thread3 static 6
Thread3 static 7
Thread3 static 8
Thread3 static 9
Upvotes: 1
Views: 1329
Reputation: 41200
static
methods lock/unlock the class(MyRunable
) object where as non static methods lock/unock the MyRunable
objects. And this both are different, I mean to say.
A synchronized method (§8.4.3.6) automatically performs a lock action when it is invoked; its body is not executed until the lock action has successfully completed.
If the method is an instance method, it locks the monitor associated with the instance for which it was invoked (that is, the object that will be known as this during execution of the body of the method).
If the method is static, it locks the monitor associated with the Class object that represents the class in which the method is defined.
If execution of the method's body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor.
Upvotes: 1
Reputation: 72854
A synchronized static
method obtains a lock on the Class
object X
representing the class in which the method is defined. In this case, the synchronized
keyword is in principle meant to synchronize between static
methods only.
Whereas a synchronized instance (non-static
) method locks the current object Y
on which the method is called.
Therefore, a synchronized static
method and a synchronized instance method can still interleave because they lock on two different objects.
Upvotes: 2