Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

About Thread.

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all, plz see the following code and give help:

The o/p is

What is the difference between //1, //2 and //3.
Thanks in advance.
Guoqiao
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The o/p is

What is the difference between //1, //2 and //3.
Thanks in advance.
Guoqiao[/b]
</BLOCKQUOTE>

Hmm interesting question Guoqiao
Here is my thought..
1. Its pretty straight forward.
start() causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
2. Direct access of instance method run(). So only main thread is
involved
3. This is one is interesting to me. I know overloaded Thread
constructor have many signatures, but i dont see this one.
I do see Thread(Runnable target) which makes me to think if
an instance of class which inherits Thread is equivalent to
Runnable interface implemented class's instance? I donno
Any body?
[This message has been edited by Ragu Sivaraman (edited August 14, 2001).]
[This message has been edited by Ragu Sivaraman (edited August 14, 2001).]
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Ragu, as to your point 3, as far as i know, since Thread implements Runnable, so we can use any Thread instance in case of Runnnable is required.
Guoqiao

Originally posted by Ragu Sivaraman:
3. This is one is interesting to me. I know overloaded Thread
constructor have many signatures, but i dont see this one.
I do see Thread(Runnable target) which makes me to think if
an instance of class which inherits Thread is equivalent to
Runnable interface implemented class's instance? I donno
Any body? [/B]


 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guoqiao,
Just to add Ragu's explanation, in the second case
t2.run(); // here run() will be called using the main thread

I have modified your code a little, please take a look at the code.


From run main
second Run!*****
-- this is the second that runs using main thread.
From run first
first Run!*****
-- this is the first that runs using separate thread.
From run third
second Run!*****
this is the third thread , but why
this.getName() returns--"second",
currentThread().getName()-- returns "third"



Thanks,
Vanitha.


[This message has been edited by Vanitha Sugumaran (edited August 14, 2001).]
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
When playing around Guaqio's code i expeted the output of the (following) slightly changed code will be

Run : 1
Run : 2
Run : 3


the output is either :

Run : 1
Run : 2
Run : 1

or,

Run : 1
Run : 1
Run : 2

Would anyone explain why not Run : 3
------------------
azaman
[This message has been edited by Ashik uzzaman (edited August 15, 2001).]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashik,
It is because counter is an instance variable. If you make it static then it will give the result you expected.

Originally posted by Ashik uzzaman
the output is either :

Run : 1
Run : 2
Run : 1

or,

Run : 1
Run : 1
Run : 2

Would anyone explain why not Run : 3


You can see my code in previous post, and when added counter as instance variable I got,

From run main
second Run!***** 1
From run first
first Run!***** 1
From run third --> counter is 2 because the second thread
is again running
second Run!***** 2



[This message has been edited by Vanitha Sugumaran (edited August 15, 2001).]
 
Are we home yet? Wait, did we forget the tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic