• Post Reply Bookmark Topic Watch Topic
  • New Topic

A Question for beginners - 4

 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without compiling please comment on the following
which is possible , which is not.


CASE I

CASE II

CASE III


}
------------------
Regds.
Rahul P. Mahindrakar
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
case1: constructor cannot be synchronized...
case 2: I do not know... How about synchronized code inside (outside is prohibited) a constructor?
case 3: array is an object, so this should work.
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gosh, am I the only beginner in these forum? Probably... Thanks to Rahul, we now have more experts than beginners here!
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi rahul,
Thread is my weakest area in java after I/O. I know
that constructor can't be synchronized ( although I don't know why it is so).
on basis of my knowledge I feel none of the cases will compile (for first one I'm sure)
please explain the right answers
regards
deekasha
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deekasha gunwant,
Yes both I/O and Threads are difficult topics to understand. But keep things going by working on small programs and in a few days you will find that you have got them in your grasp. It took me nearly 3 months!! to get things going on these fronts.

------------------
Regds.
Rahul P. Mahindrakar
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to compile case2 and it is OK. Now I am curious: why case1 is illegal, why a constructor cannot be declared synchronized? Case2 means practically the same as if we declared constructor synchronized, difference is only formal. So why is it illegal for constructor to be declared synchronized?
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I enclose what JLS 2.0 says in this regard
Unlike methods, a constructor cannot be abstract, static, final, native, or synchronized. A constructor is not inherited, so there is no need to declare it final and an abstract constructor could never be implemented. A constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static. There is no practical need for a constructor to be synchronized, because it would lock the object under construction, which is normally not made available to other threads until all constructors for the object have completed their work. The lack of native constructors is an arbitrary language design choice that makes it easy for an implementation of the Java Virtual Machine to verify that superclass constructors are always properly invoked during object creation.
Case2 means practically the same as if we declared constructor synchronized, difference is only formal.

They may be practically the same but they are not the same. I read somewhere that using synchronized methods is faster than using the synchronized block. Also the synchronized block generates the moniterenter and the monitorexit opcode while a synchronized method does not generate these opcodes.
It indeed puzzles me too as to why a synchronized block is allowed inside a constructor with "this" as a argument.
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul, thanks so much!
Your questions make us think...
 
subbu v
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I beleive I know why there can be a synchronized(this) inside a constructor. I am not very sure (I'm just beginning my foray into threads). I guess you can pass 'this' as a reference of the current object to some other object from within your constructor. So if you do that in the first few lines of the constructor and then have the synchronized(this) block, data integrity can be maintained. I hope somebody can work on these lines and come up with a more lucid answer.
 
Douglas Wolfinger
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to learn the subject of threads. However, as I mentioned in my topic entitled, "Threads in Thinking in Java" (which remains unanswered ), Bruce Eckel uses a 'com.bruceeckel.swing' package for his thread programs. I have created from the previous chapter as he instructs, but the thread programs in ch. 14 still don't recognize the package. Any help would be much appreciated!
Thanks, Doug
Originally posted by Rahul Mahindrakar:
deekasha gunwant,
Yes both I/O and Threads are difficult topics to understand. But keep things going by working on small programs and in a few days you will find that you have got them in your grasp. It took me nearly 3 months!! to get things going on these fronts.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!