aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi

i guess that the threads will access the run method once they invoked the start method.in the following program the output is alwaays same.i doubted how the two threads access the run method in a synchronous manner.Thread will usuallly execute in a async manner.why it is different here .






and also
i confused with the output


public class Morecombe{
public static void main(String argv[]){
Morecombe m = new Morecombe();
m.go(new Turing(){});//THIS SHOULD THROW AN ERROR HJELP ME!!!
}
public void go(Turing t){
t.start();
}

}
class Turing extends Thread{
public void run(){
for(int i =0; i < 2; i++){
System.out.println(i);
}
}

}
[ April 09, 2005: Message edited by: Parameswaran Thangavel ]
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Parameswaran,
Ur code displays the same value on every run because the less number of iterations. In ur code whats happening is that before the next thread start its run the previous one has already run its share. So u get the same output everytime.

include this in ur run method and check the output:

or may be u can also iterate upto 1000.


m.go(new Turing(){}); //THIS SHOULD THROW AN ERROR HJELP ME!!!

This is an example of anonymous class usage. So its not an error here.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

i doubted how the two threads access the run method in a synchronous manner.


Who said the two threads access the run method in a synchronous manner. Take a look at the output. It is difficult to determine with "i<2" in the for loop of the run method. Make it "i<10"

first0
first1
first2
first3
first4
first5
second0
first6
second1
first7
second2
first8
second3
first9
second4
second5
second6
second7
second8
second9


now make the run method synchronized like




Look at this synchronized output

first0
first1
first2
first3
first4
first5
first6
first7
first8
first9
second0
second1
second2
second3
second4
second5
second6
second7
second8
second9


m.go(new Turing(){}); //THIS SHOULD THROW AN ERROR HJELP ME!!!


The paranthesis doesn't make any difference while creating a new instance of Turning class.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
m.go(new Turing(){}); //THIS SHOULD THROW AN ERROR HJELP ME!!!

--------------------------------------------------------------------------------



The paranthesis doesn't make any difference while creating a new instance of Turning class.
is it true that both new Turing() and new Turing(){} are True.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Parameswaran Thangavel:
m.go(new Turing(){}); //THIS SHOULD THROW AN ERROR HJELP ME!!!

--------------------------------------------------------------------------------



The paranthesis doesn't make any difference while creating a new instance of Turning class.
is it true that both new Turing() and new Turing(){} are True.



Parameswaran , you are wrong .

m.go(new Turing(){});

paranthesis makes difference . new Turing() , means you are passing an object of Turing class . new Turing(){} , means you are passing an object of sub class of Turing class . And that sub class is anonymous ( has no name ) .
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
but the sub class contains nothing so it is same as its super class. thats why i am asking whether is it correct to give new Turing() itself
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread