Gagan Sabharwal

Ranch Hand
+ Follow
since Apr 23, 2006
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Gagan Sabharwal


I am going through the following url
Java theory and practice - IBM

Under the section, What do you mean by "publish"? , the following code is written

1) The respected author says that it is a safe publication. I fail to understand why ? The contention in my mind is that what if the compiler or processor or cache reorders

statement in the constructor and puts it as the first statement in the constructor. Won't the inner class MyThread be able to see a partially constructed object ?
Hi Chris,

As per my nderstanding,

1) I think in context of thread safety final is used to make an object immutable.Also since final in new JMM has a changed meaning which says that if the variables of a class are final, then during the instantiation, all those varibles will get properly initialized value before the reference of that instance is made visible to other threads.

2) The internal Data Structure(which contains the constant pool) in the runtime area is thread safe, which is used by the classloader to load the class and create a .class instance. Now since Static initialization happens just after the class is loaded by the class loader and its .class instance is created in the heap, it remains questionable whether the assignment of appropriate values to static variables is thread safe or not.

Could someone throw some light on the same ?
Hi Paul,

Thanks for the reply. Here is the code.

1) When we say java TreeSetMain.class and the class is loaded by a system classloader, what will be the content of
a)Method Area...namely constant pool, field information,method information,information of the classloader.

2)Since there is only one main thread, how does stack frame for main method and initAnotherDataStructure interact with the method area and heap?

3) Those jvms which have method tables, how is the information stored?
11 years ago

Can somebody guide me through a simple java program which reads a class file and prints an output which shows what parts of a program goes into which section of a runtime data areas?

11 years ago
Thanks Fred for the answer. Quick sort as per my knowledge solely depends on what your bound/pivot is, so that when you sort the members of your array you get balanced right and left arrays. Merge sort on the other hand makes this comparison after dividing a large array into single elements and then compares them.

I am looking for an example (may be a real world ex) where computational times of quick sort is better than merge sort. Let's say merge sort has its worst case and quick sort its best. Will quick sort be able to beat merge sort in computational times when both have an upper bound of O(n log n)?
11 years ago

I have just started learning algorithms and went through merge sort and quick sort.Run time analysis states that both have an O(n log n) time for best cases. Where does the difference lie? I wonder if it could be in the number of comparisons. Considering a real world application where an array has a considerable length and some part of it is in order, when would I want to choose a quick sort over a merge sort(keeping aside the fact that merge sort needs additional storage in terms of auxillary array).

Thanks much
11 years ago
Thanks much Peter. I will try it out.
11 years ago

I am entering into the world of Unix OS. I intend to write a simple shell script which runs a java program. My java program takes three VM arguments and classpath of various directories.Let the root directory be ab_root.
Can some one please guide me to a sample script for such tasks ?

Also I am having a hard time mugging up basic unix commands. Not too sure if its the lack of interest or what.

11 years ago
Thanks Jelly for summing it up!

I actually read someone's blog just a few days back, lost that net link.

You have mentioned about storing an object's reference in a static field, if we have say a public static field, will it be thread safe if the object to which this public static field references has even one instance field as non final ?

Will the above line of code be thread safe?I feel it is not, even if its the first thing to happen in class during the time of class loading.

Could some one please guide me through the various ways of publishing an object where its thread safety could be compromised. I went through an article, but still have not found a grip on the same. Could some one please direct me to an article where there are a good number of examples on the same!!
Hi Wouter,

Thanks for your reply. But that's precisely my question. I am expecting that when say Thread 2 reaches that sysout line, the resource should have an address reference of a partially constructed object by Thread 1. I am trying just to reecreate it. It may happen one in 10000 times but so far I am unlucky to see that.

Do you have any other code snippet where the probability of seeing a partially constructed object is more ?


I am trying to create and see a race condition by running a very common double checked locking code. Instead I am seeing a NullPointerException at the bold line underneath, which I don't want to.

I am running the above code say some 2000 times from a for loop, but have been unfortunate so far to see a hashcode value of a partially constructed object. I am running it on java 6. If I can predict that what is going to happen, the whole importance of threading is lost. But just for the heck of it, I want to see a race condition as I mentioned above. Need help!!!