Pooja Jindal

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

Recent posts by Pooja Jindal

Method would be inherited in class A.
and you can override it.
public class ThreadDemo {
synchronized void a() { actBusy(); }
static synchronized void b() { actBusy(); }
static void actBusy() {
try { Thread.sleep(1000); }
catch (InterruptedException e) {}
}
public static void main(String[] args) {
final ThreadDemo x = new ThreadDemo();
final ThreadDemo y = new ThreadDemo();
Runnable runnable = new Runnable() {
public void run() {
int option = (int) (Math.random() * 4);
switch (option) {
case 0: x.a(); break;
case 1: x.b(); break;
case 2: y.a(); break;
case 3: y.b(); break;
} }
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable);
thread1.start();
thread2.start();
} }

Which of the following pairs of method invocations could NEVER be executing at the same
time?

Answer is
A , F and H.
How ? Please Explain me with Details
1.) Yes static methods can be syncronised.
they are synchronized on the class
eg-syncronized(<class name>

2.)Yes each object has only one lock
Yes, Line 3
Integer i=2 also creates a new Object.
But there is a difference between
Integer i=2 //Line 1
and Integer i=new Integer(2); //Line 2

How?
Here is an Example to Illustrate it:-

Integer i1 = new Integer(2);
Integer i2 = new Integer(2);
if(i1 != i2) System.out.println("different objects");

This would always Print "different objects" since new Object is created everytime you use new Keyword.

But if you do it like this,
Integer i1 = 2;
Integer i2 = 2;
if(i1 != i2) System.out.println("different objects");

Both i1 and i2 refers to same Object !.So it wont Print "different objects".


However it is subject to some conditions,
two instances of the
wrapper objects will refer to same Object when their primitive values are the same and each of these is :
Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127
Well,
If you have Protected variables/Method in class pack1class.
and you are accessing it in subclass of pack1subclass (Please use Extends pack1class against it).
Then,
You can not access a Protected variable on Reference of the Super Class.
So,
pack1class pc=new pack1class();
System.out.println("protected method"+pc.prot); is Wrong!!

You should do it only through Inheritance.
like,replacing above code piece with this
System.out.println("protected method"+prot);



For More CClarifaication Refer to K& B Page no 35
we have not explicitly put a call to super() but
compiler would automatically put a call to super().
So the o/p would be

Phone.showDevice Phone.device
Mobile.showDevice Mobile.device
Mobile.showDevice Mobile.device