• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Thread

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic