• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Question on Threads

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I thought the answer would be "It will be printing the same value for x and y incrementing by 1 on each line".

But, the correct answer is "You cannot say anything about the values".
They are saying the reason is that there are two different objects and both threads are acquring lock for their own.

So, in order to see the result I put sleep() (which is commented above now). It shows the result as I expected. How come it is the wrong answer.
Please explain. Thanks.

(got rid of smily)
[ June 17, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I executed the code. I got both x and y having same result and gets incremented in each pass. Since run method is synchronized and the for loop is an infinite loop there is no way for the second thread to execute the run method.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chitra AP, please change your displayed name to conform with our JavaRanch Naming Policy.

We need a name in the format <first name><space><family name> or in the format <initials><space><family name>. For example, "Barry Gaunt" where Barry is my first name, and Gaunt is my family name. If I wanted to use initials I would use "B. F. Gaunt".

You can change your displayed name via the "my profile" link.

Thanks
-Barry


(NR last chance before closure request))
[ June 17, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is, each thread is acquiring the lock for "this", which in each case is the thread itself. Each thread can be in its run method at the same time, basically there is no synchronization here, so the result of running this code will be platform dependant.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remeber that with threading, just because a set of outcomes happens on your computer, your friends computer and 100 other computers that you observe, does not mean it is guarateed to happen. This is the slightly unfortunate nature of the platform and implementation dependent nature of Java threading. It still beats the threading with most other languages though
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes there is no synchronism involved here.each thread gets lock on its own.and each of the threads are independent of each other.so its upto the underlying system to decide when to pause the first thread and give an chance to the second thread
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Timmy Marks:
The answer is, each thread is acquiring the lock for "this", which in each case is the thread itself. Each thread can be in its run method at the same time, basically there is no synchronization here, so the result of running this code will be platform dependant.



I am puzzled about synchronizing the run method, now. Is that never of any use ?
 
narendra darlanka
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
synchronizing the run method on the same thread itself is of no use.but we can synchronize the threads on some common objects used by them using block synchronization
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic