Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread not yielding or sleeping?  RSS feed

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Well, I'm not quite sure what to make of this one. In my main method, I create 10 threads, and attempt to tell them all to run in a loop. I don't ever return from the first one, which seems strange to me (I'm sure it's operator error). I try sleeping and yielding in the Thread's run() method, but the control never returns back to the main method. Is it maybe the while(true) loop? Any help would be greatly appreciated, and thanks in advance as always!

 
Sean Collins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't actually created any Threads. Your application is single-threaded. Your Runnables won't run as separate Threads unless you 'wrap' them in new Thread(Runnable)s and start() the Threads. The thread that executes the for loop in main also executes the run method in your first 'ti' instance, which loops forever.

You need



or something similar. The start() method in Thread is responsible for invoking the Runnable's run() method.

Can you post code with ClassNames with an upper-case first character and methodNames with a lower-case first character (as in the Java API) in future please? I almost had a brain haemorrhage from autistic outrage. Thanks.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D'oh, very true! I'm on the road and don't have Henry's threading book with me, and my brain has gone numb from never using threading in real life. Thanks. As for the naming conventions, I'm familiar with them, but this was how I received the program, and hadn't cleaned it up yet.

Sean Collins wrote:You haven't actually created any Threads. Your application is single-threaded. Your Runnables won't run as separate Threads unless you 'wrap' them in new Thread(Runnable)s and start() the Threads. The thread that executes the for loop in main also executes the run method in your first 'ti' instance, which loops forever.

You need



or something similar. The start() method in Thread is responsible for invoking the Runnable's run() method.

Can you post code with ClassNames with an upper-case first character and methodNames with a lower-case first character (as in the Java API) in future please? I almost had a brain haemorrhage from autistic outrage. Thanks.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, didn't mean to post
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!