• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronization confusion

 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ......

 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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")...
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please bear with my ignorance...

I do not understand the semantics of the below code

 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic