Shivit Agarwal

Ranch Hand
+ Follow
since Feb 28, 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 Shivit Agarwal

Thanks a tonne, sir !!!

The link was worth reading....thanks.
9 years ago
At what instance and what is the use of this printStackTrace() method. If possible explain with a code ??
9 years ago
@Marc
wonderful explanation sir...
Thanks a lot, sir...
9 years ago
Hello !!
I have just finished Core Java and looking to delve self in Advance Java.

Can anybody suggest me god book for servlet, JSP ... which deals with basics, easy to understand ans can be covered quickly ???
9 years ago
Hello !!!
Can anybody tell me why we use the method hashcode() with an example of possible ??
9 years ago
@Amitabh Mehra
Thanks a lot. I got it.

@Nitesh
would have really appreciated if you would have spend the same time in giving the solution rather than writing such wonderful lines. May be I was bit harsh ,if do sorry but the answer was very short.
I am quite passionate about coding, so i use to play with the code. Even here i was just trying 2 figure out how the code output is being processed , so you saw hell lot of System.out. statement.
Thanks anwyay...

No issues....

Originally posted by Nitesh Kant:


Your bolden thing is not happening.
How do you determine that the control after notify is not going to put?



Please read the Q. twice before posting the reply. That is the most lame answer i encountered in this forum. Anybody can say looking at the program Producer->consumer->producer->consumer ....likewise the output will produced.

Have you even read the output w.r.t question, for the bolden text. I got the answer from the different forum. Please don't just reply to the query unless you read/understand the problem.
[ May 12, 2008: Message edited by: Shivit Agarwal ]
I know, that but why the the bolden thing is happening....
Hello everybody,
I understood the concept of notify() and wait(), but confused with the output ... why the statement "Entry of gettttttttttt:1" get printed...

I have bolded the line in the output statement where there is a confusion



code
class Q
{
int n;
boolean valueSet=false;

synchronized void put(int i)
{
if(valueSet)
{

try
{
System.out.println(" Entry of Put :"+i);
wait();
System.out.println("\n Exit of put ");
}
catch(InterruptedException ieo)
{
System.out.println("\n Interrupt Caught ");
}
}

n=i;
valueSet=true;
System.out.println(" Put : "+n);
notify();
System.out.println(" What will be the next line");
}

synchronized void get()
{
if(!valueSet)
{
try
{
System.out.println("Entry of gettttttttttt:"+n);
wait();
System.out.println(" Get after wait ");
}
catch(InterruptedException ieo)
{
System.out.println("\n Interrupt Caught ");
}
}

System.out.println(" Got : "+n);
valueSet=false;
notify();
System.out.println(" Testing .......................");

}

}

class Producer implements Runnable
{
Q q;

Producer(Q obj)
{
q=obj;
new Thread(this,"Producer").start();
System.out.println("\n\nProducer hai ");
}

public void run()
{
int i=0;

{
while(true)
{
System.out.println(" In while ---- put");
q.put(i++);
System.out.println(" In while ---- put down");
}
}

}
}

class Consumer implements Runnable
{
Q q;

Consumer(Q obj)
{
System.out.println("\n Consumer hai ");
q=obj;

new Thread(this,"Consumer").start();

}

public void run()
{


while(true)
{
System.out.println(" In while ---- get" );
q.get();
System.out.println(" In while ---- get down");
}
}

}

class withnotify
{
public static void main(String[] args)
{
Q q=new Q();

new Producer(q);
new Consumer(q);


for(int i=0;i<1000;i++);


System.exit(1);
}
}




Output

Producer hai

Consumer hai
In while ---- put
Put : 0
What will be the next line
In while ---- put down
In while ---- put
Entry of Put :1
In while ---- get
Got : 0
Testing .......................

Exit of put
Put : 1
In while ---- get down
In while ---- get
What will be the next line
Got : 1
Testing .......................
In while ---- get down
In while ---- get
[b]Notify() should pass the control to void put() which is on wait(), but instead the below statement get executed.... notify() gets avoided[b]
Entry of gettttttttttt:1
In while ---- put down
In while ---- put
Put : 2
What will be the next line
Get after wait
Got : 2
Testing .......................
In while ---- get down
In while ---- get
@Henry
Thanks mate. But can yoyu tell me the use of notify(). In the above code what notify is doing I cannot see any purpose behind above ??
Hello,
I have written my first program to connect to Oracle10g database but encountered the error below.

java.sql.SQLException: [Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified

How to resolve it. Do I need to download some extra drivers ??
What should I do now ??

I have created the table and also inserted the information in SQL.

And My java code

Hello,
I didn't get the output ...Can anybody explain me ??
I have bolden the problem in output.

The code is from THE COMPLETE REFERENCE - HERBERT SCHILDT


My second question is what happens when a notify() is encountered before a wait() statement as in the program below ??






Output
Producer hai
Put : 0
What will be the next line
Entry of Put :1 Why and how this line gets printed
Consumer hai
Got : 0
Entry in get:0
Put : 1
What will be the next line
How many times
Got : 1
Entry in get:1
Put : 2
What will be the next line
How many times
Got : 2
Entry in get:2
Put : 3
What will be the next line
How many times
Got : 3
Entry in get:3
Put : 4
What will be the next line
How many times
Got : 4
Entry in get:4
Put : 5
What will be the next line
Entry of Put :6
How many times
Got : 5
Entry in get:5
Put : 6
What will be the next line
How many times
Got : 6
Entry in get:6
Put : 7
What will be the next line
How many times
Got : 7
Entry in get:7


[HENRY: Fixed Indentation in Code]
[ April 19, 2008: Message edited by: Henry Wong ]
Even I have the confusion here. The problem is not with the public but else.

Every time i type the command say javac Sample1.java ,instead of my java file Sample.java another java file is created with the name Sample1.java with the content of Sample.java.
Whats the catch behind this ??
10 years ago
System.out.printf("%5.2f", myVariable);

This is not valid in java.
10 years ago