• 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

JQ+ Mock - Threads - Question ID: 988383703406

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is quite similar to the one I posted here: https://coderanch.com/t/257971/java-programmer-SCJP/certification/JQ-Mock-Threads-ID

Tried to compile and run the code to understand the concepts but I got even more confused as the results seem to contradict my thoughts.



I tried running the code and it shows this (below) which really doesn't agree with the explanation given (above). The synchronized method does seem to work.:


[ August 08, 2006: Message edited by: Shanel Jacob ]
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
synchronized keyword has nothing to do here as there is two different locks and each thread will acquire its own lock.

Answer is d: Reason is you can't be so sure about the order of execution. Does not matter how many times you got the same output what you posted.


Naseem
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still need to clarify the concepts. So we have 2 thread objects and even if the run() is synchronized, it doesn't matter because each thread object calls its own run method? In this scenario, x and y may not be the same.



Now suppose I synchronized by object instead. That means both thread objects (we cannot know which thread) will take turn incrementing x and y, so x and y will always have same values.

 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you want to synchronized both threads then check code below.
As you are running two threads with two different instances of Test
class so they will acura two seprate locks on two seprate thread and two seprate locks will not block each other.

 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it ok to summarize the levels of synchronization as follow:


public synchronized void run()
------------------------------
any of the 2 threads can access, x and y might NOT be equal


synchronized (this)
-------------------
any of the 2 threads can access, x and y will always be equal


synchronized (Test1.class)
--------------------------
only 1 thread can access, x and y will always be equal
 
Paper beats rock. Scissors beats tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic