8

Was writing a multithreaded program in Java with an infinite while loop (for waiting). The thread was never preempted. Added a print statement inside the while loop and everything started working :/

Now I need to print a stupid message just so the program works :P

Comments
  • 1
    when working with threads and you have to wait for other processes use wait(); & notify(); ;)
  • 2
    The print probably contains code that releases controll and that lets other code run. Never ever use a loop to wait in multi threaded code.

    Only time a loop is valid is in really old platforms with single thread where you use it to actually slow things down.

    But there is better ways for that to.

    Wait/notify/sleep or something depending on language is always a better idea.

    Or callbacks but then you might need some other code to handle timeouts.
  • 0
    Dude go check Jakob Jenkov's tutorials on multothreading and thread 'scheduling'. There you will find the proper way to do it ;)
Add Comment