Dmitry Katsubo opened SPR-16623 and commented
In my case I would like to run several tasks in parallel and then combine them into one StopWatch. The difficulty is that StopWatch does not allow concurrent modification e.g. adding to List<TaskInfo> taskList is not thread-safe. To overcome that, the time for each spawned subtask can be accumulated in long timeMillis or TaskInfo and then added to StopWatch. To achieve that I suggest to make all fields of StopWatch protected. This will allow the subclass to extend the StopWatch with methods like this:
public class MyStopWatch extends StopWatch {
public synchronized void addAsyncTask(String taskName, long timeMillis) {
lastTaskInfo = new TaskInfo(taskName, timeMillis);
taskList.add(lastTaskInfo);
taskCount++;
}
public synchronized void addAsyncTask(TaskInfo taskInfo) {
lastTaskInfo = taskInfo;
taskList.add(taskInfo);
taskCount++;
}
public synchronized void addAllAsyncTasks(StopWatch sourceStopWatch) {
taskList.addAll(sourceStopWatch.taskList);
taskCount += sourceStopWatch.taskList.size();
}
public synchronized void stop() {
super.stop();
}
}
One may argue of course that total time (long totalTimeMillis) will not be representative, but it could be just treated as "total time for synchronous tasks".
Affects: 5.0.1
Comment From: spring-projects-issues
Dmitry Katsubo commented
I hope this is not a big effort to make fields of StopWatch protected... If there is an interest, I can provide a pull request.
Comment From: dmak
Can we progress on this issue?
Comment From: rstoyanchev
For one, protected fields are rare in the framework, outside of loggers and constants. Second, the class states it is not designed for concurrency and fields like startTimeNanos and currentTaskName are for one task at a time. So adding synchronized would make it look like there is concurrency support but not really since you still have to measure one task a time.
Implementation details aside, it sounds like you are running several StopWatch instances and then trying to combine their TaskInfo[] into one StopWatch presumably for reporting purposes? Is that so and if not what is the actual goal and reason?
If it is for reporting which I guess is to be done at the end, then adding synchronized seems to have no practical benefit. And in any case there may be other ways of achieving what you're trying to do, so please clarify more the goal and reasons, rather than the implementation.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.