File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread class execution explanation required

 
Haani Naz
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • 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


 
Udara Amarasinghe
Ranch Hand
Posts: 109
Java Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks! now it makes sense
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic