• Post Reply 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
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

More Generic questions but WITH code!!

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK...here's some code:

The output to this code is:


What is the most appropriate way to have the second thread yield() to the first thread, so my output is more split up between 0's and 1's? I'd like to see thread: 0 get a chance to execute even through thread: 1 has priority. Is this done in the run() method? Since both threads share the same run() method, how could I tell one thread to hold up while another executes? Would that be done with something like

...and if so, how would I handle manipulating multiple threads being spawned if I weren't planning on naming each one of them individually (say, using a for loop with an incrementing counter as the name)?
Or should the yield() be done in the constructor?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you give one thread higher priority than another, it will generally be favored by the thread scheduler. On some systems this may be absolute - a thread may never run as long as a higher priority thread is runnable (and not waiting, sleeping, or blocking on input). On other systems it's a bit more random and unpredictble. There's really no reliable way to control threads as precisely as you seem to want; the Java specifications purposely avoid being too specific about priorities, leaving different implementations free to concentrate on other things like efficiency and reliability. If you want to give multiple threads equal opportunity to run, make them equal priority, and call yield() periodically. If you want threads to have unequal opportunity, give them unequal priority - but don't expect any further refinement in your ability to control, especially if you run on different platforms.
However if you're worried that the poor low-priority thread will never run, here's a simple modification to reassure you:

If your threads have intervals where they are sleeping, waiting, or blocking on input, then the JVM will make sure the time isn't wasted - it will find another runnable thread to run, if one is available.
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Jim. Your example is perfect for then next question I was going to ask: How is it that you can call Thread.sleep() without using the threads actual name? For example, in this program I have 2 threads, thread: 0 and thread: 1. Because they both use the same run() method, is that why the call to generic class Thread.sleep() works? Would runner.sleep() and runner2.sleep() work also (assuming I declared the variables in a larger scope)? I've always wondered why I can just call Thread.WHATEVER and how that will affect the thread I'm running or even which one. Any comments?
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you will find that the Thread.sleep() methods all work on the currently running thread (whichever thread calls the method).
The same is true of most of the other static methods of the Thread class
(yield, holdsLock, interrupted, dumpStack, etc)
 
reply
    Bookmark Topic Watch Topic
  • New Topic