File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes thread class execution explanation required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "thread class execution explanation required" Watch "thread class execution explanation required" New topic
Author

thread class execution explanation required

Haani Naz
Greenhorn

Joined: May 30, 2010
Posts: 23
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

Joined: Aug 17, 2009
Posts: 109

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

Joined: May 30, 2010
Posts: 23
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

Joined: Aug 17, 2009
Posts: 109

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

Joined: May 30, 2010
Posts: 23
thanks! now it makes sense
 
Consider Paul's rocket mass heater.
 
subject: thread class execution explanation required
 
Similar Threads
Thread
priortiy
Brogden's Exam Cram and Thread and static
Updated MulitThreads Problem
How to wait for multiple thread to finish run