Now I am trying to learn more about the java thread, and I have a small question that I cannot find a direct answer anywhere. Suppose I have two threads that share an object:
public class FooA implements Runnable
{
Object data;
public FooA(final Object newData)
{
data = newData;
}
public void doSomething()
{
synchronized(data)
{
data = new Integer(1);
}
}
public void run() {
}
}
public class FooB implements Runnable
{
Object data;
public FooB(final Object newData)
{
data = newData;
}
public void doSomething()
{
synchronized(data)
{
System.out.println(data);
}
}
}
Will FooA block FooB when it is in the doSomething section of code? Or vice versa? The feeling of my feeling says yes, but according to the book I read, it says no. Hence the need for monitoring objects. I made a slightly more complicated version of this, and everything worked fine.
I looked around a bit, but could not find a specific answer.
source
share