Where do I need to place the keyword synchronized
if I want to prevent that the two threads are manipulating tab
simultaneously?
Mainclass
owns the variable tab
it's methods f1,f2,f3
are manipulating tab
. f2,f2
are called by the main method in a loop, and f3
is called by a thread, in x
milliseconds after a event occurs in the main method.
main method:
main() {
Mainclass mainclass = new Mainclass(); // class containing variable tab
while (condition) {
mainclass.f1(); // manipulating tab
mainclass.f2(); // manipulating tab
if (eventOccured) {
mainclass.startThread(x);
// thread calls f3() after x milliseconds whitch is manipulating tab
}
}
}
IMainclass:
public class Mainclass {
public void f1(){...} // manipulating tab
public void f2(){...} // manipulating tab
public void f3(){...} // manipulating tab
public final TabObject[][] tab;
}
Can I synchronize tab
, or do I have to synchronize f1,f2,f3
, should I use a synchronized block like synchronized (tab) {...}
or syncronize the whole mehtods?