Divyeshh Patel

Ranch Hand
+ Follow
since Aug 03, 2010
Merit badge: grant badges
For More
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 Divyeshh Patel

It is impossible to say whether this is correct or not without looking at the full code, but assuming the code is as usual for threads(e.g. o is a Runnable and object1 is same as o, etc), you are right, the picture correctly depicts the concept.
Also, instead of writing synchronized(object1), it would even be okay if you declare the method as synchronized in this case. When run method of one thread finishes, notifyAll() method is automatically called and another random thread waiting for this monitor will be picked up for running.

Gari Jain wrote:
The notify() method is used to send a signal to one and only one of the
threads that are waiting in that same object's waiting pool.

then when i run this code..how come all the readers are notified?


Gari,
If you haven't already figured it out, even the notify() call in this program is superfluous.
You can run the program after removing that call and you will get the same result.
Thanks for the pointer Dieter.

Jim Hoglund wrote:
I'm still confused about the meaning of "main thread". Can you explain further?

Jim ... ...


By main thread I mean the thread that starts when you execute the program(and which executes the main() method)

Shanky Sohar wrote:It could also be compiler error because they donot give the assumption for import statement...
and code given by you requires following

statement........


You are right, even the import is missing and nothing is mentioned about import assumption.
8. Consider the following lines of code:
String s1 = "Whatever";
String s2 = new String("Whatever");
String s3 = new String ("Who");
Which of the following statements is true? (Choose all that apply.)
A. The compiler will create two strings Whatever and Who and put them in the pool, and
there will be a string Whatever and Who created at runtime.
B. The compiler will create two strings Whatever and Who and put them in the pool, and
there will be no string created at runtime.
C. The compiler will create two copies of Whatever and one copy of Who and put them in
the pool, and there will be strings Whatever and Who created at runtime.
D. The compiler will create one copy of Whatever and put it in the pool, and there will be
strings Whatever and Who created at runtime.
E. The compiler will create a string Whatever and put it in the pool, and there will be a
string Who created at runtime.

The given answer is A.
Just wondering if that is the correct answer (or how properly worded the question/answers are)
This question is from Apress book for SCJP, chapter 8, Question 6

Consider the following code:
1. FileOutputStream out=new FileOutputStream("objectStore.ser");
2. ObjectOutputStream os = new ObjectOutputStream(out);
3. os.writeObject("Object on the fly!");
What is the result of this code?
A. The string "Object on the fly!" is written into the file objectStore.ser.
B. Line 3 causes a compiler error.
C. The code compiles fine but an exception is thrown at line 3 when the code is executed.
D. An exception occurs at line 2.
E. A compiler error occurs at line 2.

I assumed that the answer would be compiler error at line 1 which does not appear in the option list, but they wanted me to assume that the code is enclosed in try-catch...
One more answer I came across (don't remember where) was "it is appropriate to throw AssertionError in the program"(the word "explicit" was not mentioned explicitly in the question)
But what I believe is, putting an assert statement in the program (though it might always be evaluated to false) is different from throwing an AssertionError in the program
Hope what I wrote is not too confusing...

Dieter Quickfend wrote:I can't give the full question, because it's in the exam engine and I can't copy it, but basically, to summarize:


"if x5.equals(x6) == true, which of the following statements will always be true?"


Correct answer:

C: x5.hashCode() == x6.hashCode();

Explanation: By contract, if .....




...What did I miss? By contract? That wasn't in the question...


I guess when nothing explicit is mentioned, you have to assume ground rules, if you go by Developers' rules there can be thousand different answers
Nevertheless, I fully agree with you that fewquestions in LearnKey are confusing.
One that I remember was something like "House has roof, and turn on Stereo, house has doors". This question can also have different answers

Jim Hoglund wrote:Divyeshh : In the first post the wait() was on an object that extends Thread,
not the "main" thread. So I think the mystery notify() is a behavior of type
Thread. We should study Thread to find the answer.

Jim ... ...


Yes Jim, I meant Thread only when I wrote that it has something to do with start() method (yes, i can know you can define a method with any name, within java's rules of course, but I was thinking of the already existing methods )
The main thread ends only if you have called the start() method of the Thread on which the main thread is waiting, otherwise it does not end. So probably as I had mentioned in my first post, something is happening when the run() method finishes. [Remember, calling the run() method directly also does not end the main thread, run() has to be called via start() only]

Or is it that notify() is automatically called when the run method finishes?


mohitkumar gupta wrote:
in the above code,animals of type is passed to addAnimal method that is supertype of Dog.then why not in the First Problem i posted


Passing the list as an argument and adding something to the list are two different things, aren't they?

mohitkumar gupta wrote:
2.why do line 22 ,21 give error ?


list is of Horse Type,then why such error


No doubt, list is of type Horse, but has been declared as <? super Horse> so the only way to access it is (Object o:list)

mohitkumar gupta wrote:
3.why the casting is required in the line 42 but not in 28?


Since Object is the supertype of all objects, you can assign anything to Object without a cast which is not the case with line 42
e.g.
Congrats buddy.
13 years ago

Seetharaman Venkatasamy wrote:

Divyeshh Patel wrote:Though it does not harm replacing line 24, but it is unnecessary as far as the question is concerned(just making use of generic types)


if you were not apply generic, then there is a possibility to put any Object into Map right? example : aMap.put(key, new Object()); where in generic only you can put Integer or its subclass

hth


Please read the post carefully, I am sorry to say but I think your thoughts are totally irrelevant to my doubt. It is not about whether we can put an Object into a Map or not, it is only about replacing the existing line.
Looks like it has something to do with start() method.
The wait() on thread ends automatically only when start()[ultimately run()] finishes execution.
If there is no call to start() method on the thread, the main thread waits forever...

This class is to be updated to make use of appropriate generic types, with no changes in behavior
(for better or worse). Which of these steps could be performed? (Choose three.)
Correct answers are
B. Replace line 13 with
private Map<String, Integer> accountTotals = new HashMap<String, Integer>();
E. Replace lines 17–20 with
Integer total = accountTotals.get(accountName);
if (total == null) total = 0;
return total;
G. Replace line 24 with
accountTotals.put(accountName, amount);

My doubt is, why should option G be in the list of answers?
Though it does not harm replacing line 24, but it is unnecessary as far as the question is concerned(just making use of generic types)
Can anyone please correct me, if I am wrong?