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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Threads

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi all,
Can anyone explain the flow of the below code. I'm really confused. Your help will be appreciated.

------------------------------
Here is the output
------------------------------
Child Thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread
Main Thread: 2
Main Thread: 1
Main thread exiting

Thanks,
-Lalitha

(Marilyn added code tags)
[ August 03, 2005: Message edited by: Marilyn de Queiroz ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What is it specifically that you're confused about?

Basically, there are 2 threads here running concurrently. Each counts down from 5 to 1, sleeping a set (minimum) amount of time with each iteration. One thread tends to sleeps longer than the other, so it will likely take longer to finish running.

However, the exact output is unpredictable, because how the threads actually end up running is determined by the (platform-dependent) thread scheduler. In fact, you're likely to see different output from one execution to the next.
 
Sheriff
Posts: 9103
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator


Line 1 creates a new NewThread which calls the constructor in line 5
The constructor makes another Thread labelled "Demo Thread" in line 6.
Line 7 prints the thread created in line 6.
Line 8 calls the run() method on the thread in line 6.
The constructor exits and line 2 starts a loop to print (line 3) the thread that is running main() which is a separate thread than the one created in the NewThread constructor. The loop waits 1 second between each println call (line 4).
Meanwhile the run() method begins a loop (line 10) to print (line 11) the thread created in line 5 waiting 1/2 second between each println call (line 12).
The rest is to demonstrate that the two loops (and the two threads) are running independently of each other. The results may vary (or not) each time you run the program.
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic