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  synchronization confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " synchronization confusion" Watch " synchronization confusion" New topic
Author

synchronization confusion

narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
khalid mughal book page:631.



output:

(b) The letter B is always printed.
(d) The letter B is never printed after End.


can anybody tell me how this output..

about synchronization problem
I am not understanding when we are creating two thread objects .. and invoking a synchronized method then also one executed first and one later but according the book the instance of the same thread when used to invoke synchronized method then this should happen.....

Can anybody help me on Thread Syncronization ......


SCJP1.6,SCWCD1.5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I can't see any synchronized methods in that code. The answers (b) and (d) also seem wrong to me. The second thread is a daemon thread i.e. the program can exit even when it is not finished. So there are no guarantees that "B" will be displayed (ruling out option b). "B" can be displayed after "End" in a few cases. The JVM might complete main method before the first thread (which is not a daemon) then complete the second thread (displaying "B") and then execute the first thread (displaying "A")...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

about synchronization problem
I am not understanding when we are creating two thread objects .. and invoking a synchronized method then also one executed first and one later but according the book the instance of the same thread when used to invoke synchronized method then this should happen.....


I am not able to understand what you want to say here about syncrronization problem, Can you explain it again?
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Can somebody please explain this part of the code




I have only known that the object that is passed to the thread's constructor should be of a class which implements Runnable. Here a string object is passed and Strings don't implement
Runnable.
Also there is a block containing the run() method and it is part of Threadedprint class but it is getting invoked ! How ?

Please enlighten me!!

Martin Jedrzejewski
Greenhorn

Joined: May 14, 2007
Posts: 11

I have only known that the object that is passed to the thread's constructor should be of a class which implements Runnable.


wrong, there are many overloaded constructors for thread class, look in to documentation:

http://java.sun.com/javase/6/docs/api/java/lang/Thread.html
Martin Jedrzejewski
Greenhorn

Joined: May 14, 2007
Posts: 11
Yogesh Gnanapraksam wrote:
Also there is a block containing the run() method and it is part of Threadedprint class but it is getting invoked ! How ?


run() gets "called" when start() is executed - actually it is put into the Runnable state and waits for the system to be called when system finds time for executing it.

from java docs:

void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Thanks Martin.
I still have a question :- The class containing the 'run()' method should do one of the following:
1.Extend Thread class
2.Implement Runnable.

But I see ThreadedPrint class is not doing any of these....but still it works ? how
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Yogesh Gnanapraksam wrote:Thanks Martin.
I still have a question :- The class containing the 'run()' method should do one of the following:
1.Extend Thread class
2.Implement Runnable.

But I see ThreadedPrint class is not doing any of these....but still it works ? how


At a stab Id say when the call to new Thread(id) is made. Don't forget, the Thread class itself implements runnable.


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Please bear with my ignorance...

I do not understand the semantics of the below code

Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Yogesh Gnanapraksam wrote:Please bear with my ignorance...

I do not understand the semantics of the below code



Its the declaration of a Thread class instance, which implements runnable and which this class overrides the run method accordingly.
Its position in the code you illustrate, seems to be the instantiation of an anonymous inner class which uses an overloaded constructor to pass a copy of the id reference variable.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronization confusion
 
Similar Threads
Reg. Some Thread basics
Newbie to ThreadLocal
Study question on daemon threads
Synchornized Block..
How the Synchronization works