• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Synchronized Thread

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What effect do you think "synchronized" has on the run method?
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic