code_junkie

In Java critical sections, what should I synchronize on?

Nov 13th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. private void doSomething() {
  2. // thread-safe code
  3. synchronized(this) {
  4. // thread-unsafe code
  5. }
  6. // thread-safe code
  7. }
  8.  
  9. public void foo() {
  10. synchronized (this) {
  11. // do something thread-safe
  12. }
  13. }
  14.  
  15. public synchronized void foo() {
  16. // do something thread-safe
  17. }
  18.  
  19. Vector v = getSomeGlobalVector();
  20. synchronized (v) {
  21. // some thread-safe operation on the vector
  22. }
  23.  
  24. public class Foo {
  25. private final Object syncObject = new Object();
  26. …
  27. }
  28.  
  29. private Object mutex=new Object();
Add Comment
Please, Sign In to add comment