Two classes factor and addition
factor is having val variable that means it is shared across multiple threads (in the current application we are using two threads).
addition class is having a add variable it is also shared across multiple threads, because it's instantiated in factor class.
my question is
1)if synchronized(this) is used, which means that any of the two threads will lock on factor instance and increment val variable value till the loop exits.
so synchronized(this) means here that we should not use any other instance variables. We have to use only the variables of factor instance inside the synchronized block?
2) if synchronized(addition) means here that we have to use only add variable not the val variable of factor instance class?
There is a big confusion regarding this synchronization block .
what i understood is synchronization block will lock on the object's instance and guard the operation and make it
thread safe. But using different instance really means that it should guard only that particular instance variables not any other instance variables. Can anyone explain in depth concept regarding this relating with the code provided below
The factor class
The addition class
The main class