I have a MyService
class, which has only one public
method doTask()
, I use synchronized
keyword to keep it being accessed in a thread-safe manner:
public class MyService {
String myTaskId;
public MyService {
myTaskId = getTaskId();
}
public synchronized void doTask() {
myTaskId = getTaskId();
...
}
private String getTaskId() {
...
}
}
There is a private function getTaskId()
which is invoked both in constructor and in doTask()
function. I am wondering is it worthy to have synchronized keyword also on getTaskId()
function?