wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronized Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronized Thread" Watch "Synchronized Thread" New topic
Author

Synchronized Thread

Anurag Swaym
Greenhorn

Joined: Aug 12, 2002
Posts: 14
This is from a mock exam from the sun site.
. public class MyThread implements Runnable {
2. private String holdA = "This is ";
3. private int[] holdB = {1,2,3,4,5,6,7,8,9,10};
4.
5. public static void main(String args[]) {
6. MyThread z = new MyThread();
7. (new Thread(z)).start();
8. (new Thread(z)).start();
9. }
10.
11. public synchronized void run() {
12. for(int w = 0;w < 10;w++) {
13. System.out.println(holdA + holdB[w] + ".");
14. }
15. }
16. }
D. Compilation succeeds and the program prints each value in the holdB array at the end of the "This is " line. Each value is printed two times before the program ends, and the values are not printed in sequential order.
E. Compilation succeeds and the program prints each value in the holdB array at the end of the "This is " line. Each value is printed in order from 1 to 10 and, after the value 10 prints, it starts printing the values 1 to 10 in order again.
My question is why cannot the answer be "D". Aren't thread implementation platform dependent, and you cannot predict which thread is being run at a particular time.
If they have the same priority (I assumed same priority (5) since priority has not been explicitly mentioned), even though the threads are synchronized, can you really predict that the 2nd thread will be run after the 1st thread is completely finished ?
Thanks.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Wellcome to the Ranch swamyarun
Please read our naming policy and adjust your displayed name accordingly.
Because the two threads are properly synchronized, one thread will have to wait for the other to complete the synchronized method before starting to execute it itself.
[ August 14, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
What effect do you think "synchronized" has on the run method?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Oooh, this one's tricky. I might well have missed it if it had appeared on my real exam.
The key here is to notice that while there are two Thread objects, they are sharing a single MyThread object.
[ August 14, 2002: Message edited by: Ron Newman ]

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Without run() being synchronized, D would be the correct answer because the target z of type MyThread is shared between two threads. The effect of synchronized here is that once a thread is running (that is, has entered its run() method), it owns the lock on the shared MyThread object and can run to completion without any problem. Once one of the thread has finished, it releases the lock and dies, the second thread starts executing then.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi There:
I made some changes to code above:

Now two threads are not syncronized. That answer D
will be correct. I am assuming that original code
was example of object lock. How do you implement
class lock so that all objects of the locked class
are syncronized?
Thanks
Barkat
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi Ron.

The key here is to notice that while there are two Thread objects, they are sharing a single MyThread object.

To elaborate on that, the key is to notice that, two or more threads (whichever) are going to execute methods (whichever) that are protected by the same lock. In this case the lock is the one corresponding to the z object.
Hi Barkat.
In order to synchronize properly several methods they must be synchronized on the same object. Using the "instance method modifier" synchronized will requiere, to satisfy the former sentence, the use of the same object, z. Because when an instance method declaration is synchonized the lock to be used is the one corresponding to that instance.
If we were using something like "synchronized(lock) { ... }" within the body of a method, the object lock must be the same on every method that we want to synchronize.
[ August 20, 2002: Message edited by: Jose Botella ]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Here's some sample code:

Each Strider object is running on its own, well, thread, while two Threads are accessing the single Runner object in a multi-threaded manner.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronized Thread
 
Similar Threads
Hi Thread doubt
synchronized lock the object or method
Sun sample question.
Thread Program
Threads