rengarajan vaikuntam

Ranch Hand
+ Follow
since Oct 04, 2004
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 rengarajan vaikuntam

Thankyou
Joyce lee!
19 years ago
Hi ranchers

Thankyou all, for all who cleared the doubts and for all those who posted their doubts.Everything was an eyeopener and its really been a good learning experience and with a fruitful result.I cleared my SCJP today with a score of 76%.Even though the score isn't impressive, im happy.(I know Barry and Corey is waiting to move this topic to a different forum).Main reason for me to post this in this forum is to thank everyone who contributed for this forum.I know aspiring SCJPians and real SCJPians will really log in to this forum atleast once in a day.Thanks to kathy and berts for their excellent book.Thanks to Dan for the excellent question and answers topic vice.Marcus for the very good mock exams.
And thankyou Barry and Corey for the excellent job you are doing to answer the questions immly.

Thankyou all.
19 years ago
Thanks jeff

I hve removed the wait() method.The output is ok.
But even if i remove the synchronized keyword from the method get()the output is the same.So is this code not a correct example for a synchronized behaviour.
Thanks
Look at this code:

I have created 3 threads.All the three acessing the get() method.My understanding is once a thread enters a sychronized method any other thread cannot enter the method until it is completed.Here i expect the result to be
12
12
12

but the result is
1112
22

I understand from this that there is no synchronization in this code and my basic understanding is wrong.Could anyone explain me or alter this code so that i shall be able to understand about synchronize.
Thanks in advance.

( tags added)
[ November 11, 2004: Message edited by: Barry Gaunt ]
Hi Barry

quote:
___________________________________________________________________________
You are not using threads as they are meant to be used so spending effort to explain this behaviour is not worthwhile (especially because it's easier to use threads the correct way.)
___________________________________________________________________________
Lets take a look at one of the questions from Marcus Greens mock exam.

31)What will happen when you attempt to compile and run the following code?

1) Compile time error
2) Output of first0, second0, first0, second1
3) Output of first0, first1, second0, second1
4) Runtime error

The Ans is 3)Output of first0, first1, second0, second1

In this question he has subclassed a thread class,invoked the start() method,made the Thread to sleep but instead of overriding the run() method he has overridden the start() method.So this actually is not a Thread.Here Threads are not used the right way.But it is not surprising to get such questions in the exam.You tend to get stuck here.



(Code formatted)
[ November 08, 2004: Message edited by: Barry Gaunt ]
Thanks Mikalai Zaikin
I have one more doubt
can we say, a class, whose constructors are all marked private, a final class.
Thamks
Can we say a variable declared inside a try block as a local variable?

Pls can someone explain.
Thanks.
Barry
You are right you call the start method to start a thread.But what surprised me is the difference in the output.When i dont set a priority for the thread and invoke the run method the run method is invoked and runs like any other normal method.But when i set the priority and call the run method it is not invoked.I just want to know why the code bahaves differently when the thread priority is set.
Thanks.
Thanks nitin

But you try this you remove all the comments in your code run the code see the result.
Comment only the line tt.setPriority(10) run the code and see the result.
Why is the difference in output?
Thanks.
Henry
I dont understand both of your sentences.
quote 1:
___________________________________________________________________________
The tt.run() method is *NOT* the run() method that you defined. It is the default run() method of the Thread class.
___________________________________________________________________________

If tt.run() method is not the one i defined and if it is going to be the default run() method of the Thread class ,then what about the output i get when i run the code.

quote 2:
___________________________________________________________________________
Just because tt.run() calls tc.run() under certain conditions is pure dumb luck as it was never meant to be called directly.
___________________________________________________________________________
calling the run method directly is legal but it will not run in a seperate thread rather it goes in to the current call stack and the run method will be executed like any other normal method.
But when i redifine the class to extend Thread and i call both start() and run() methods the output is same regardless of whether the setpriority() method is commented or not.Only if i implement the runnable interface the output is different with comments and without comments.
Thanks.
Look at this code:

Output is:

0 Thread-0
1 Thread-0
2 Thread-0
3 Thread-0
4 Thread-0


In the same code if i comment the line
//tt.setPriority(10);

Output is:

0 main
0 Thread-0
1 main
1 Thread-0
2 main
2 Thread-0
3 main
3 Thread-0
4 main
4 Thread-0

see, when the setPriority(10) method is commented the tt.run(); calls the run method and gives a ouput like this.
But when the setPriority(10) is included the tt.run(); is never executed.
I cant understand the behaviour of this code
could anyone explain this.
Thanx.
[ November 06, 2004: Message edited by: Barry Gaunt ]
Thanks corey
so if i have class called MyClass and if i say

MyClass mc = new MyClass();

Here also can i say 2 objects are created one with the keyword new and the other null and one reference variable mc.

thanx.
Look at this snippet:

String st = new String("JAVA");

How many objects are created and how many reference variables are created?
Let me try to xplain. Im also new if iam wrong let someone else correct me.

First let us see the method
static byte m2(short s)
The method declares very clearly that it is going to take a short as argument.It has got a memory capacity of only 16 bits.so if u call the method
m2(1);
here 1 is an integer even though this is well within the range of a short this is with 32 bits so the compiler complains that it cannot hold an integer in the place of short its a narrowing conversion so a explicit cast is needed.Now if you say

short a = 1;
then call the method
m2(a);
now the value 1 stored in the var a is a short so this also compiles without any problem.

Now the return statement.The method declares
return 42;
Here 42 is a compile time constant and it is well within the range of byte so this is fine.Whatever value you are going to pass in to this method and whatever operations are performed in the method it is going to return only 42.So there is no need of any casting here.
But if you say
return s+1;
here it will not compile and here you will need a explicit cast even though if it is going to be in the range of byte, but you dont know s is a variable the value of s might change during another call so here it needs a cast
return(byte)(s+1);
Hope this clears.
Thank you corey i got it and it is clear now.