Question

Essentially I have the following two statements:

Project project = projectDao.createProject(...);
projectDao.deleteInProgressEntry(...);

I want to make sure that the project creation is completed finished before the inProgress entry is deleted (before the next line is executed). The statements are completely independent of each other, so I am worried that the compiler may not respect the ordering in the code.

I have done some reading about volatile, and I tried adding volatile to the beginning of the first line so I would have volatile Project project. But that doesn't compile. I have had trouble finding what I am looking for without getting lost in multithreading, atomic operations, etc.

Was it helpful?

Solution

The statements are completely independent of each other, so I am worried that the compiler may not respect the ordering in the code.

No, it will. It's possible that other threads may observe the results of the operations out of order - but from the perspective of the executing thread, it will definitely execute in order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top