Manju Rao

Greenhorn
+ Follow
since Mar 05, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Manju Rao

Dear All,

Below code is from K & B Chapter 3's exercise
class Dims {
public static void main(String[] args)
{
int[][] a = {{1,2,}, {3,4}};
int[] b = (int[]) a[1];
Object o1 = a;
int[][] a2 = (int[][]) o1;
int[] b2 = (int[]) o1;
System.out.println(b[1]);
}
}

In this I'm not able to understand that how can we assign an array into an Object reference which is not an array??
Please explain the above code.

Regards
Manju
Hello Friends,

I have 2 SUN vouchers available. It expires on 31-Jan-09.
If any of you or your friends going to register for the exam i will transfer my voucher. I am in Delhi and can send it anywhere in NCR region.
My contact No. is 9971230491 / 9818184733
I bought it for Rs. 4800 and ready to sell it at negotiable price.

Regards
Manju
13 years ago
Dear Friends ,

I made a class TryingProtected in a package, and a protected method displsy() in it.
Then i made a subclass SubProtected of class TryingProtected in another package.
Now , if a try to access the display method with the object of TryingProtected i am getting copilation errer "The method display() from TryingProtected is not visible."

The code is given below. I did not copy this code from anywhere , was trying to clear my concepts on protected access specifier.

superclass code :

package newPackage;

public class TryingProtected {
protected void display()
{
System.out.print("TryingProtected");
}

}
--------------------
subclass code is below :

package myPrograms;

import newPackage.TryingProtected;

public class SubProtected extends TryingProtected
{
public static void main(String args[])
{
TryingProtected tp1=new TryingProtected();
SubProtected tp2=new SubProtected();
tp2.play(); // compiles successfully
tp1.play(); // compilation error
}
}

-----------
My doubt is : if protected is accessible from subclass in different package why i am not able to access through superclass reference variable??

Please Help
Hello Everybody,

I'm facing one problem in below code from K&B Thread exercise Ques No. 2

public class Letters extends Thread {

private String name;

public Letters(String name) { this.name = name; }

public void write() {

System.out.print(name);

System.out.print(name);

}

public static void main(String[] args) {

new Letters("X").start();

new Letters("Y").start();

} }

We want to guarantee that the output can be either XXYY or YYXX, but never XYXY or any

other combination. Which of the following method definitions could be added to the Letters

class to make this guarantee? (Choose all that apply.)

A. public void run() { write(); }

B. public synchronized void run() { write(); }

C. public static synchronized void run() { write(); }

D. public void run() { synchronized(this) { write(); } }

E. public void run() { synchronized(Letters.class) { write(); } }

F. public void run() { synchronized(System.out) { write(); } }

G. public void run() { synchronized(System.out.class) { write(); } }

Answer:

� 3 E and F are correct. E and F both cause both threads to lock on the same object, which will

prevent the threads from running simultaneously, and guarantee XXYY or YYXX. It's a bit

unusual to lock on an object like System.out, but it's perfectly legal, and both threads are

locking on the same object.

�˚ A can't guarantee anything since it has no synchronization. B and D both synchronize on

an instance of the Letters class�but since there are two different instances in the main()

method, the two threads do not block each other and may run simultaneously, resulting in

output like XYXY. C won't compile because it tries to override run() with a static method,

and also calls a non-static method from a static method. G won't compile because

System.out.class is nonsense. A class literal must start with a class name. System.out is

a field not a class, so System.out.class is not a valid class literal.


In the above code I'm not able to understand that how we can restrict output to XXYY by synchronizing through System.out

Regards
Manju
Thanks Manni and Ankit for the reply.

Regards
Manju
Thanks Ankit for clearing this.

I just want to know one more thing that what "when that thread completes execution" means. Is this mean that when code inside run method completed?

Regards
Manju
Thanks Kenneth for correcting me.

I was trying to understand the flow of program sent by Aparna Misri in one of todays post at Javaranch. She had picked it from K&B book Chapter threads. I then made many changes in the program and not able to understand this thing that without calling notify how a waiting thread started.

Regards
Manju
Hello Everybody,

Please check the below code

class MyThread
{
public static void main(String[] args)
{
ThreadB t2=new ThreadB();
t2.start();
synchronized(t2)
{
try
{
t2.wait();
System.out.println("After waiting complete "+ t2.getPriority());
}
catch(InterruptedException e)
{
System.out.println("Exception thrown");
}
}
}
}
class ThreadB extends Thread
{
int total;
public void run()
{
System.out.println("Inside run");
}
}

and it's output is

Inside run
After waiting complete 5

I'm not able to understand one thing that I'm not calling notify() or notifyall() anywhere in this code. But still I'm getting the output "After waiting complete 5".
Kindly explain this.

Regards
Manju
Dear Abhishek,

Thank you for clearing my concept of overriding.

Regards
Manju
14 years ago
Please check the following code

class A
{
public void display()
{
System.out.println("Inside A display");
}
}
class B extends A
{
public void display()
{
System.out.println("Inside B display");
}
public void things()
{
System.out.println("Inside B things");
}
}
public class Overriding
{
public static void main(String[] args)
{
B b=new B();
A a=b;
b.display();
a.display();
b.things();
a.things();
}
}

the problem which is coming is
when I try to access a.things() then compile time error is coming. If we can access diplay method of class B through a.display which is overriden then why can't we access a.things.

Please help me to understand this concept.

Regards
Manju
14 years ago
Hi Ernest Friedman-Hill,

Your reply helped me to clear my concepts. Thank you for giving me time and help.

Regards
Manju
Dear all please look the following program

class Counter implements Runnable {

private int currentValue;

private Thread worker;

public Counter(String threadName) {
currentValue = 0;
worker = new Thread(this, threadName); // (1) Create a new thread.
System.out.println(worker);
worker.start(); // (2) Start the thread.
}

public int getValue() { return currentValue; }

public void run() { // (3) Thread entry point
try {
while (currentValue < 5) {
System.out.println(worker.getName() + ": "+currentValue++));
Thread.sleep(250); // (4) Current thread sleeps.
}
} catch (InterruptedException e) {
System.out.println(worker.getName() + " interrupted.");
}
System.out.println("Exit from thread: " + worker.getName());
}
}

public class Client {
public static void main(String[] args) {
Counter counterA = new Counter("Counter A"); // (5) Create a thread.

try {
int val;
do {
val = counterA.getValue(); // (6) Access the counter value.
System.out.println("Counter value read by main thread: " + val);
Thread.sleep(1000); // (7) Current thread sleeps.
} while (val < 5);
} catch (InterruptedException e) {
System.out.println("main thread interrupted.");
}

System.out.println("Exit from main() method.");
}
}

output from the program:

Thread[Counter A,5,main]
Counter value read by main thread: 0
Counter A: 0
Counter A: 1
Counter A: 2
Counter A: 3
Counter value read by main thread: 4
Counter A: 4
Exit from thread: Counter A
Counter value read by main thread: 5
Exit from main() method.

In the above program the output in first 2 line should be
Thread[Counter A,5,main]
Counter A: 0
because in the constructor of Counter we start the thread

Please tell me the flow.

Regards
Manju