multithreading - Java Threads Busy Waiting -


hi doing project , have reached part stuck. have tried search ways learn how write while loop busy wait haven't found , code runs infinite loop. can explain me how busy waiting loop should work , me break out of infinite loop?

the project wants following happen: in morning, after student wakes (it take random time) head bathroom ready new school day. if bathroom taken, student takes break (use yield()) , later on wait (use busy waiting) bathroom become available. students use bathroom in first come first serve basis (you can use boolean array/vector having them released in order).

 public class student implements runnable      {         private random rn = new random();         private string threadnum;         private volatile boolean bathroomfull = false;         private static long time = system.currenttimemillis();         private thread t;       public student(string studentid)      {       threadnum = studentid;        t = new thread(this, "student thread #"+threadnum);       system.out.println("thread created = " + t);       // call run() function       t.start();    }     public void run()     {        int waittime = rn.nextint(4000 - 2000 + 1)+2000;          system.out.println( "the current time " + (system.currenttimemillis() - time) + "and wait time is: " +waittime );           //student wakes after random time         while((system.currenttimemillis()-time) < waittime)        {           // system.out.println("the remaining sleep time " + (system.currenttimemillis()-time));             ;        }        int = rn.nextint(4000 - 2000 + 1)+2000;       try        {           //system.out.println("i'm going sleep " +a + " milliseconds");         thread.sleep(a);       }        catch (interruptedexception e)        {         // todo auto-generated catch block         e.printstacktrace();       }        //this busy wait loop bathroom full thread yield until available     int l = rn.nextint(10 - 1)+1;   bathroomfull = true;        while(bathroomfull)         {           for(int j = 0; j < l; j++)           {               system.out.println("i in bathroom " + l + "minutes " + thread.currentthread());           }           thread.yield();           bathroomfull = false;           //exitbathroom();          }     bathroomfull = true; 

this main method allows user specify how many student threads want. , yes don't understand how implement change of value busy wait while loop can broken.

 public static void main(string args[])     {        int numberofstudents;        numberofstudents = integer.parseint(joptionpane.showinputdialog("how many students there in university? "));       // system.out.println("there " + numberofstudents);         for(int = 0; < numberofstudents; i++)        {               new student(string.valueof(i+1));        }           new teacher();    } 

here working example of busy wait. uses atomicboolean indicate if bathroom occupied or not. atomic operations executed in 1 step, important guarantee thread-safety. use normal boolean , write compareandset ourselves:

private static synchronized boolean compareandset(boolean expected, boolean value) {     if (occupied == expected) { // (1)         occupied = value; // (2)         return true;     } else {         return false;     } } 

this equivalent (for example) of java implementation. synchronized needed otherwise possible 2 threads succeed test @ (1) before (2) executed (because 2 operations aren't atomic) , 2 people go in bathroom together...

import java.util.concurrent.atomic.atomicboolean;  public class student extends thread {      // note static: there 1 bathroom students     private static atomicboolean occupied = new atomicboolean(false);      private string name;      public student(string name) {         this.name = name;     }      private void sleep(int millis) {         try {             thread.sleep(millis);         } catch (interruptedexception e) {             system.out.println(name + " wet his/her pants");         }     }      @override     public void run() {         int r = (int)(math.random() * 5000);         system.out.println(name + " sleeps " + r + " ms");         sleep(r);         system.out.println(name + " goes bathroom");         // ***** busy wait *****         while (!occupied.compareandset(false, true)) {             system.out.println(name + " takes break");             thread.yield();             sleep(1000);         }         // ***** end (in bathroom) *****         system.out.println(name + " in bathroom");         sleep(1000);         occupied.set(false);         system.out.println(name + " goes university");     }      public static void main(string[] args) {         new student("bob").start();         new student("alice").start();         new student("peter").start();         new student("marcia").start();         new student("desmond").start();         new student("sophia").start();     }  } 

possible output:

bob sleeps 2128 ms
marcia sleeps 3357 ms
alice sleeps 1289 ms
peter sleeps 820 ms
desmond sleeps 1878 ms
sophia sleeps 2274 ms
peter goes bathroom
peter in bathroom
alice goes bathroom
alice takes break
peter goes university
desmond goes bathroom
desmond in bathroom
bob goes bathroom
bob takes break
sophia goes bathroom
sophia takes break
alice takes break
desmond goes university
bob in bathroom
sophia takes break
alice takes break
marcia goes bathroom
marcia takes break
bob goes university
sophia in bathroom
alice takes break
marcia takes break
sophia goes university
alice in bathroom
marcia takes break
alice goes university
marcia in bathroom
marcia goes university


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -