• Post Reply Bookmark Topic Watch Topic
  • New Topic

When to synchronize a method?

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I wonder when to synchronize a method. Please consider this:

Output:
C:\Java\EigeneJavaProgramme>java Question47a
Thread-1 0
Thread-1 1
Thread-1 2
Thread-1 3
Thread-1 4
Thread-2 5
Thread-2 6
Thread-2 7
Thread-2 8
Thread-2 9

I have two questions:
1. Is the result always the same no matter which operation system you have? This is beause it always starts first thread t1 before it starts second thread t2. Is it possible that Java starts t2 before t1 if the two threads have the same priority?
2. I think it is not neccessary to synchronize "aMethod()" in this case as threads run() method doesn't have a sleep or yield which interrupts the current thread t1 to work on thread t2.
Is that true?
Thanks for your answers.
Thomas
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Is the result always the same no matter which operation system you have?
No, it may depend on the system, and may even vary randomly on the same system.
This is beause it always starts first thread t1 before it starts second thread t2. Is it possible that Java starts t2 before t1 if the two threads have the same priority?
The start() method for t1 is always called before start() of t2. However all actions of the run() method for each thread are not guaranteed to be in any particular order compared to another thread. After the start(), t1 may finish first, or t2 may finish first.
2. I think it is not neccessary to synchronize "aMethod()" in this case as threads run() method doesn't have a sleep or yield which interrupts the current thread t1 to work on thread t2.
Is that true?

No. It doesn't matter that sleep() and yield() are not called. What you need to consider is, is there any data which is used by both threads? Here the two threads have different thread names, so the invocation of getName() is not a problem - the threads are accessing different data. But both threads are accessing the same i variable. Because of this (and because i is not a constant) it's necessary (or at least, strongly recommended in order to guarantee thread safety) that the code which accesses i should be synchronized.
In this example, it probably doesn't actually matter most of the time because t1 is short enough that it can usually complete before t2 executes, and the threads don't really interact. But this is not guaranteed; synchronize for safety.
[ January 12, 2003: Message edited by: Jim Yingst ]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The synchronization potential problem is based on simultanious access to a static variable by two seperate instances of the same type of object. Note that the code given is:

It is not:

The synchronized modifier for a method synchronizes on an individual object (on it's "moniter" really, whatever that's supposed to mean).
In this case, since two seperate objects are used, wouldn't the use of synchrnized do nothing? You would need a static object for all instances of the class to share as a lock.

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David is right - you will need a static lock object for proper synchronization in this case (you could alternatively use Question47a.class for this purpose).
Also notice that on a multi-processor machine, the two threads could run *literally* simultaneously.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah - I didn't mean to imply that simply declaring the method synchronized is sufficient here; I meant adding appropriate synchronization. Which I forgot to clarify - thanks for pointing that out. In this case I would simply declare aMethod() static (and synchronized), since it uses no instance data anyway. (Primarily because there is no instance data in this class). Alternately I'd synchronize on Question47a.class as Ilja notes, which is exactly the same monitor a static synchronized method would use. Why would we need some other lock object?
[ January 12, 2003: Message edited by: Jim Yingst ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, this class is currently thread unsafe. you quite possibly could loose an increment or three if you make the execution run long enough.
the current synchronization is useless because synchronization is per object, and you are creating new objects everytime.
Really you cant tell the difference between a multiprocessor maching and a single processor maching. The error will be no stronger or weaker if the OS behaves appropriately.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!