If you use this class with several threads then the param and sb should also be marked as volatile or am i wrong??
private double param = Math.random();
private StringBuilder sb = new StringBuilder();
Why don´t you need to use volatile on "param" or "sb" ?
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Lisa
23 January 2022, 16:02
sb and param are only used in synchronized methods and synchronization is making sure that you're working with up to date variables (when entering and leaving the synchronized block it updates registers). So you could say synchronization implies volatile.
0