I wanted to make sure(that i understood correctly) and know how synchronized methods in java guarantees none interference on objects.
For example I have this code:
private void update_curr_mat(int stock_ind, double price, double time)
{
synchronized(current_mat_data)
{
current_mat_data[stock_ind][HIGH_PRICE_IND] = price;
current_mat_data[stock_ind][LOW_PRICE_IND] = price;
current_mat_data[stock_ind][CLOSE_IND] = price;
current_mat_data[stock_ind][HIGHEST_IND] = price;
current_mat_data[stock_ind][LOWEST_IND] = price;
current_mat_data[stock_ind][CURR_TIME_IND] = time;
}
}
In this example it is obvious, that current_mat_data
is synchronized, and when the method invoked, another thread can't write to current_mat_data
object.
In this example:
private synchronized void update_curr_mat(int stock_ind, double price, double time)
{
current_mat_data[stock_ind][HIGH_PRICE_IND] = price;
current_mat_data[stock_ind][LOW_PRICE_IND] = price;
current_mat_data[stock_ind][CLOSE_IND] = price;
current_mat_data[stock_ind][HIGHEST_IND] = price;
current_mat_data[stock_ind][LOWEST_IND] = price;
current_mat_data[stock_ind][CURR_TIME_IND] = time;
}
The synchronized is done in the method definition. I know that it guarantees that two threads can't invoke this method simultaneously.
So my question is, it is guarantees that other thread can't access the object current_mat_data
while the function in the second exmaple invoked ? if it is true, can you explain how it works ? If i'm not right about something i wrote or something isn't clear, please let me know.