File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Whiz Lab MOCK Quesitions on THREADS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Whiz Lab MOCK Quesitions on THREADS" Watch "Whiz Lab MOCK Quesitions on THREADS" New topic
Author

Whiz Lab MOCK Quesitions on THREADS

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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 ?

OCPJP 6.0 93%
OCPJWCD 5.0 98%
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

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...


KARTHICK.C , SCJP6-93%
(Born to Win)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Please go through the code and correct it~


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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

Joined: Jul 06, 2009
Posts: 196

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

Joined: Nov 04, 2009
Posts: 2066

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Whiz Lab MOCK Quesitions on THREADS
 
Similar Threads
synchronized mess up
Thread Synchrinization
Thread Synchronization
Synchronized block
locking??