Right now i am trying to learn more about java threading, and i have a small question that i cannot find a direct answer to anywhere. Lets say i have two threadsthat both 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() {
// Does stuff
}
}
public class FooB implements Runnable
{
Object data;
public FooB(final Object newData)
{
data = newData;
}
public void doSomething()
{
synchronized(data)
{
System.out.println(data);
}
}
}
Would FooA block FooB when it is in the doSomething section of the code? Or vice versa? My gut feeling says yes, but according to the book i am reading it says no. Hence the need for monitor objects. I made a slightly more complex version of this, and everything worked fine.
I looked around a bit, but couldn't find a concrete answer.