This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
    Bookmark Topic Watch Topic
  • New Topic
friki data migration
Ranch Hand
Posts: 772
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator

run() is called:

What to look for: notice the order the Threads run in. Without being "forced" to sleep, the Threads will not necessarily take turns. If you can run this applet on both a Macintosh and a PC, you might see a DRASTIC difference. On a PC, thread scheduling often works the way you might hope: the Threads appear to take turns, more or less. But on a Macintosh, one Thread might just stay in until it has finished!

WARNING: turn-taking is NOT built into Java! The responsibility is for you, the programmer. So while you might get good behavior on a PC, you can't guarantee it across all Java platforms. To guarantee fair Thread behavior (proper turn-taking) you must use a strategy which employs yield() and/or sleep(). So if this NoSleep example runs on your machine exactly like the ThreadTwo example (which DOES sleep), you're lucky. Nothing programmed in the Java applet is making that happen.

Bottom Line: If you're just starting with Threads, ALWAYS USE SLEEP in your run() method to be safe. Later, as you become more experienced with Threads you may use more sophisticated scheduling strategies.


I agree. Here's the link:
    Bookmark Topic Watch Topic
  • New Topic