Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MG1 #54

 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone explain the following code (preferably in steps)?

the output is one one two two
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that this code is not 'starting' new threads as it might appear to be. To start a thread you need to issue:
obj.start()
but this code is saying:
obj.run()
Therefore run() method is called as regular method. No new thread is created. After first run() call is returned, second is run() is executed.
Hope this makes sense.
Barkat
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i totally agree that there is no threads being started, however i am a bit confused about sleep followed by yields. can you please clarify?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each call for run() will sleep for a second and then attempt to yield. But as you said there is not other thread to yield to, same thread (main) will continue.
Hope this makes sense.
Barkat
 
shirley tao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if we change the "run" to "start", what's the difference of the result?
and what's the use of the yield in the code?
Thanks!
Shirley
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
posted by Shirly:
But if we change the "run" to "start", what's the difference of the result?
and what's the use of the yield in the code?
Thanks!
Shirley

The output will be unpredictable. yield is used to allow other methods of same priority to run.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barkat Mardhani:
posted by Shirly:

The output will be unpredictable. yield is used to allow other methods of same priority to run.

yield is used to allow other methods of same or higher priority to run.
[ September 18, 2003: Message edited by: Vicken Karaoghlanian ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no gaurantee whether the thread which is allowed to run is of more, less or equal priority.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic