Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whiz Lab MOCK Quesitions on THREADS

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whiz Lab MOCK Quesitions



What would be the output ?

a.compilation fails
b.output cannot be predicted
c.1thread-0 2thread-1 or 1thread-1 2thread-0
d.Exception
e.none of these

i think c

Is Synchronization there in the class ?
a.YES
B.No

if yes,then why??
if no,then why and how can it be synchronized ??



i think ,it's not synchronized as each TH would have it's own LOCK object ,so every thread can call incre even if it's been called by another thread

would it still be synchronized without the synchronized block ?
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)
wheres the reference variable here...?

2)
This is an instance method of Lock.... so you must invoke it using reference variable of lock only.... [the one you are going to give in "1)" ]

3) After the corrections....The code is definitely synchronized on The object of Lock (Not on TH).... The threads you created in TH (a and b) will try to access the same static Lock object's
synchronized method incre...
so, they (a and )b cannot access the incre method at the same time...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the code and correct it~
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have corrected the code.
karthick
1.Is it that Lock is declared as static due to which,there is synchronization ??
2.if static modifier is removed from the LOCK,then would the code be synchronized ??

 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not Exactly........
In this code if you remove the static modifier of Lock object then each "Th object" will point to a different instance of Lock and
Lock is not even a critical section in that case as no two(or more) threads area accessing the same object......
But you have synchronized your code.....
The point is there will be no need for synchronization in your code if you remove the static modifier of Lock
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta, Did you get the output which you mentioned in you first post on this thread? Check the below code.


And, There is no relationship with declaring the Lock a as static with the synchronization. It's up to the developer. If you don't declare the Lock a as static, then every TH object will have a separate Lock object. So, logically, the synchronization won't needed. I mean, there is no point of synchronization. Because, two threads, two object.
Use proper indentation in your post. This is said many times, that's why, you don't get the responses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic