This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Can threads be made to have equal chance of running???

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q. what should we insert so that the o/p is most likely ab ab ab ab..

class Thread1 implements Runnable {
public void run() {
for(int x = 0; x < 4; x++) {
try { Thread.sleep(500); } catch (Exception e) { }
System.out.print("a");
} }

public static void main(String [] args) {
Thread t = new Thread(new Thread1());
t.start();
for(int x = 0; x < 4; x++) {
// insert code here
System.out.print("b");
} } }


Ans: we can insert -->try { Thread.sleep(500); } catch (Exception e) { }

It's said in the explaination that "the most likely way to get the two threads to take turns evenly is to have them sleep for the same amount of time."

My doubt is, once the thread went in sleep state,it leaves its running state. It goes into the ready to run (i.e. runnable pool of threads)state.
Now the scheduler will decide that which thread to choose next... the running of threads will not be fixed then how come we say that by calling sleep method we can give them even chance. I think their chance of running is not in our hands....

Please correct me where I am wrong....and explain this to me...


Thanks
Swapnil
[ June 27, 2006: Message edited by: Swapnil Trivedi ]
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct when you say that thread operation is out of your control, but you can make things "more likely" (as the question asks).

For instance, as they are written, Thread1 has a 500ms sleep call on each iteration of its for loop while the loop in main does not. As a result, the most likely output is something like: abbbbaaa or some variation thereof (on my system, it gave: bbbbaaaa).

If you give the loop in the main thread the same sleep code - if you make each loop take about the same amount of time to execute - then it is likely (although not guaranteed) that the output would be something like: abababab

Running the unmodified program 5 times gave "bbbbaaaa" each time. The 500ms wait was long enough for the "b" loop to complete every time before a single "a" was written.

Running the modified program 5 times gave:

babababa
bababaab
abbababa
babababa
babababa

Because the threads now have a similar exeuction profile (identical, actually), the output is more likely to be in sync. But, as you said and the above illustrates, it is by no means guaranteed.
 
Water! People swim in water! Even tiny ads swim in water:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic