Sachin Dravid

Greenhorn
+ Follow
since Jan 21, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Sachin Dravid

Thanks a lot Winston. That was really helpful.
12 years ago
Hello,

Any inputs on the above post are awaited
12 years ago
Thanks a lot Steve and Anayonkar. Those were really helpful.

Steve:

2) Consider a class DEF where a static field is being accessed by a non-static synchronized method. We create 2 instances of the class - DEF_01 and DEF_02. Now, thread B is accessing the non-static method of DEF_01 and hence has obtained a lock on DEF_01, and another thread C is accessing non-static method of DEF_02 and so has obtained a lock on DEF_02. However, the method is trying to change the value of same static variable. What will happen in such case?

Since the data being accessed is shared (by virtue of being static) but the locks being used is not (because the two threads are synchronized using different objects) the data is unprotected, and problems can arise.



Is there any way to prevent to prevent the static field from getting corrupted? I know that the best practice is to access static fields via static synchronized methods and non-static fields via non-static synchronized method. But if the situation arises where I two threads are accessing the same static field through different instances of same class, is there any workaround to prevent the static data from being corrupted?

Anayonkar:

Thanks man. Although that was not exactly my question. By "static method accessing instance variables", I meant static methods accessing instance variable via instance of the class. Sorry for not stating it clearly.

Since the variable is instance variable, it can be accessed by synchronized static methods via the instance (E.g. objectReference.fieldName), while the same instance variable can be accessed by non-static synchronized method also. Now my question is, Suppose thread A is accessing the synchronized static method, the thread will obtain a class level lock. At the same time, another thread B is accessing the non-static synchronized method, so thread B will get object level lock. Now, as per SCJP - Kathy Sierra book, "a static synchronized method and a non-static synchronized method will not block each other—they can run at the same time"

So, my question is, will the instance variable be corrupted, if both the methods (static and non-static methods) are changing the value of that variable? Is there any strategy to prevent this from happening?
Hello All, warm greetings to all.

I have gone through SCJP - Kathy Sierra book and online articles, still the below doubts still puzzle me. It would be helpful if geeks here can throw some light on it:

1) Suppose we have a synchronized block in a object (say an instance of class ABC), and thread A is accessing it. There is a method call from this synchronized block. The method belongs to a different object (say an instance of class XYZ) (i.e. the method does not belongs to the class in which synchronized block is present.) In such case:
a) If the method in object XYZ is marked as 'synchronized', I guess thread A will obtain a lock on XYZ also. Am I correct here?
b) If the method in object XYZ is not marked as 'synchronized', what will happen then? Will thread A still acquire the lock on XYZ?

2) Consider a class DEF where a static field is being accessed by a non-static synchronized method. We create 2 instances of the class - DEF_01 and DEF_02. Now, thread B is accessing the non-static method of DEF_01 and hence has obtained a lock on DEF_01, and another thread C is accessing non-static method of DEF_02 and so has obtained a lock on DEF_02. However, the method is trying to change the value of same static variable. What will happen in such case?

3) Another scenario is somewhat inverse of the above scenario: class JKL has an instance variable which is accessed by a synchronized non-static method as well as synchronized static method. Now since the locks acquired by these 2 methods - object level lock and class level lock - will not interfere with each other. What will the result of two threads are independently these methods? One thread accessing static method and another accessing non-static method and both of them trying to change the value? What will be the result?

fred rosenberger wrote:you can use the javap command to read the class file:

C:\slop>javap Dog.class
Compiled from "Dog.java"
public class Dog extends Animal {
Dog();
}

C:\slop>javap Animal.class
Compiled from "Dog.java"
class Animal {
Animal();
}

C:\slop>



Thanks Fred.

I was able to run 'javap' and got the following:

G:\>javap Animal.class
Compiled from "ConstructorExample_02.java"
class com.learning.java.Animal {
java.lang.String name;
java.lang.String nameOfOwner;
com.learning.java.Animal();
com.learning.java.Animal(java.lang.String);
}

G:\>javap Dog.class
Compiled from "ConstructorExample_02.java"
class com.learning.java.Dog extends com.learning.java.Animal {
java.lang.String name;
int age;
int weight;
com.learning.java.Dog();
com.learning.java.Dog(java.lang.String);
com.learning.java.Dog(java.lang.String, int);
com.learning.java.Dog(java.lang.String, int, int);
public java.lang.String getName();
}

I did not see any code related to Animal inside Dog.class file. It simply states that Dog extends Animal (2nd line in Dog.class).

So, exactly how the things happen at runtime can you please elaborate a little? Till now, I was assuming that all the permitted (non private) code related to superclass is copied into subclass bytecode, since subclass inherits those properties and methods. But, this is not the exact scene as I was assuming.

Can I assume that at runtime, Java runtime links the Animal class with Dog class somehow? And if it is true, then how?
12 years ago
I have a doubt related to the intermediate bytecode (.class files) generated when inheritance is involved in the scene. Please consider the below code:

Dog.java


After compiling Dog.java, 2 class files are generated: Animal.class and Dog.class.
Animal.class file contains the bytecode of Animal class. Till this point, everything is fine.

Now, my question goes like this:
What are the contents of Dog.class file? Does Dog.class contains the bytecode only related to Dog class or it includes the bytecode of Animal class as well i.e. Bytecode of Animal + Bytecode of Dog? (Animal class being super class of Dog class)
12 years ago
Hello All,

I have total IT experience of 2 years and 7 months till now. I had started my career with one of the top MNC in India. I have worked on Java/J2EE application development projects for almost 2.5 years in that company. And I must say that I have developed good interest in Java/J2EE technologies and related frameworks.

Now, I have changed the company and working with another MNC in India itself. Initially I was recruited as a Java/J2EE applications developer, but the current project I am working upon does not involve much technical work related to Java technologies and frameworks. I am mainly involved with functional aspects and co-ordination kind of stuff - like interacting with clients, various stakeholders etc. working on finance domain.

I am not finding the work exciting as I am kind of a technical guy. In a nutshell, I plan to stay in technical side for at least couple of years down the line. I am still updating myself with Java language nuances and frameworks. Now, suppose after another 6-8 months, if I appear for Java/J2EE related interviews for other companies, how will this middle 6-8 months affect my job search, since I would not be working on the technical profile for those many months.

I know the questions seems to be quite personalized, but right now, I am pretty much confused. So any kind of inputs from you guys are appreciated.

Thanks in advance.
12 years ago
Thanks all for the reply. But I am still not sure about the last 2 questions:

2) I am using eclipse IDE. I was trying to apply breakpoints in the interface, but eclipse was not allowing me to put breakpoint in an interface. What is the reason for it? Why I cannot apply any breakpoint in an interface?

3) Agreed that interfaces are the best way for a java program to interact with the outside world, as they form a kind of contract between the implementing class and outside world. But suppose the developer of class XYZ has written another method, say method_02() in the class XYZ, which he feels is useful for the outside world. And suppose he forgets to declare that method in the interface, then outside world will not be able to access that method, still his program will compile and run fine. Is there any way to catch such scenario, say during testing phase?


Thanks in advance.
12 years ago
Thanks Joanne for the reply.

But that is what is puzzling me. Can you please elaborate a bit about memory/stack? Like where will the 2 reference variables land?
12 years ago
Hello All,

I have some basic doubts related to interfaces. Consider the following example:





Now, my questions go as follows:

1) When does the compiler checks if the class implementing a particular interface has implemented the methods declared in that interface or not? Is it during compile time?

2) I am using eclipse IDE. I was trying to apply breakpoints in the interface, but eclipse was not allowing me to put breakpoint in an interface. What is the reason for it? Why I cannot apply any breakpoint in an interface?

3) Agreed that interfaces are the best way for a java program to interact with the outside world, as they form a kind of contract between the implementing class and outside world. But suppose the developer of class XYZ has written another method, say method_02() in the class XYZ, which he feels is useful for the outside world. And suppose he forgets to declare that method in the interface, then outside world will not be able to access that method, still his program will compile and run fine. Is there any way to catch such scenario, say during testing phase?
12 years ago
Thanks Ritchie and Fred.

Continuing the discussion with Fred:

As you mentioned Fred - So, your statement "As soon as the second object is created, reference to the first object will be dropped" is simply not true.

I am confused a bit now. Since we are using the same reference variable name to refer to 2 objects, how can single variable can point to 2 different objects?

After reading a little about the memory concepts, I know now that each method has its own stack. So, main() method will have its own stack and we are creating 1st object inside main() method. Next, method_01() will have its separate stack and 2nd object is created from this method. So, has it got to do something with memory? The 2 reference variables will simply exist in different stacks (even though they are of same name) and will keep on pointing on different objects on the heap?
12 years ago
Hi,

Consider the following code:


In the above example, we are creating an object of type ABC inside main() method and calling a method on that object. Now, within the method, we are again creating an object of type ABC, using the same reference variable name 'abc'. I know that the values of 'x' and 'y' would be 10 and 20 after printing. Now, my question is:

01) We are using same reference variable to refer to 2 different objects. As soon as the second object is created, reference to the first object will be dropped and will be created for the second object, since we are using the same reference variable. Is it so?

02) If it is so, then how we are getting the values in first object (x=10, y=20) and not the new pair of values (x=30, y=40)
12 years ago