• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

thread class execution explanation required

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i'm taking an online course in android as a hobby and i've been given the following code with no explanation as to what it does (they're taking a time to get back to me)

i was hoping somebody can give me a detailed explanation ( noob-level : P ) as to how the following code works! thanks.

p.s: i am a beginner to java but i'm posting the code here cos i suspect its beyond the beginner level


 
Ranch Hand
Posts: 109
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you run this program you will see a output like :

$1$ : 4
$1$ : 2
$1$ : 3
$1$ : 1
$2$ : 4
$2$ : 3
$2$ : 2
$2$ : 1

here how it works

First in the main methods for loop on line 19 it's create 2 SimpleThread objects as the for loop counts. And each time SimpleThread object createed it calls SimpleThread objects default constructor on line 4 and store threadCount value as the name by using super constructor on line 5.
Then it's call start method for start the SimpleThread objects thread execution. when it started it execute the code in run method on line 11. In the run methods while loop counts until countDown variables value become 0.
The countDown variables value check using if statement on line 14 and when it evaluate countDown variable and see countDown == 0 then run method returns(exit).
And also another thing happen always that while loop counts. That is it print this SimpleThread objects string representation. That String representation is conducted by the toString method. Each time toString method calls it return string object that include this SimpleThread objects name and countDown value. That's all.


 
Haani Naz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Udara.

kinda stumped on one thing.

you said:

And also another thing happen always that while loop counts. That is it print this SimpleThread objects string representation. That String representation is conducted by the toString method.



how is the toString() method called? i don't a see a method call so am a little puzzled
 
Udara Amarasinghe
Ranch Hand
Posts: 109
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Haani Naz wrote:how is the toString() method called? i don't a see a method call so am a little puzzled


It's happen like this when some object pass into the System.out.println method, then println method invoke that passed objects toString() method and print that returned string on the screen. Ok..
 
Haani Naz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks! now it makes sense
 
reply
    Bookmark Topic Watch Topic
  • New Topic