Reputation: 11
I have recently started learning multi-threading in java. I was trying various scenarios in synchronization. This is my code
package main_classes;
public class Synchronized_test implements Runnable {
public Object lock2=new Object();
private static int shared_var;
public Synchronized_test()
{
shared_var=0;
}
int getsharedvarvalue()
{
return shared_var;
}
void setsharedvarvalue(int i)
{
shared_var=i;
}
public void run()
{
synchronized(lock2)
{
System.out.println("Child Thread: Waiting");
for(int i=0;i<5;i++)
{
//this.setsharedvarvalue(i);
System.out.println("Child Thread: Shared Variable = " + shared_var);
}
}
}
//@SuppressWarnings("unused")
public static void main(String args[]) throws InterruptedException
{
Synchronized_test st=new Synchronized_test();
Thread t=new Thread(new Synchronized_test());
t.start();
synchronized(st.lock2)
{
for(int i=0;i<5;i++)
{
st.setsharedvarvalue(i);
System.out.println("Main Thread: Shared Variable = " +st.getsharedvarvalue());
}
}
}
}
This is the output
Child Thread: Waiting
Main Thread: Shared Variable = 0
Child Thread: Shared Variable = 0
Main Thread: Shared Variable = 1
Child Thread: Shared Variable = 1
Main Thread: Shared Variable = 2
Child Thread: Shared Variable = 2
Main Thread: Shared Variable = 3
Child Thread: Shared Variable = 3
Main Thread: Shared Variable = 4
Child Thread: Shared Variable = 4
From what I understood from reading online, a thread has to wait if the lock is acquired by some other thread. So as per the code whichever thread acquired lock first will print out all the values and then the second thread should execute. But from the output is seems that both thread are inside their synchronized block and running simultaneously. Please explain this behavior.
PS: This output is not the only output I get. Sometimes I get the output consistent with the theory mentioned above. Like this one
Child Thread: Waiting
Child Thread: Shared Variable = 0
Child Thread: Shared Variable = 0
Child Thread: Shared Variable = 0
Child Thread: Shared Variable = 0
Child Thread: Shared Variable = 0
Main Thread: Shared Variable = 0
Main Thread: Shared Variable = 1
Main Thread: Shared Variable = 2
Main Thread: Shared Variable = 3
Main Thread: Shared Variable = 4
Upvotes: 1
Views: 65
Reputation: 118
You are running only one Thread 't'. Kindly note that st is an instance of Synchronized_class which has implemented Runnable interface. You will have to start a new thread 't0'. Try the following code:
Thread t0 = new Thread(new Synchronized_test());
Thread t = new Thread(new Synchronized_test());
t0.start();
t.start();
Further, your code is not clear for understanding the concept of 'synchronized statements'. I suggest you try a simple example for Synchronized Statements from the link-http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html. It can clear your understanding and help you structure your code better.
Upvotes: 0
Reputation: 272437
You're only running one thread object, aren't you ?
Synchronized_test st=new Synchronized_test();
Thread t=new Thread(new Synchronized_test());
t.start();
only starts thread t
and you're not running st
at all.
Upvotes: 2