java - Putting functions from Multiple Threads into a Single Queue -


i creating library simulator has inside library, librarian , multiple borrower objects. involves multiple borrower threads , single librarian thread run concurrently, performing tasks requesting books, returning books etc.

i have been using synchronized functions ensure functions within each borrower not become muddled, trying figure out how instead to, once borrower begins function, put queue set sleep between entries, , output posted console within intervals.

my question once borrower gets function e.g. requestbooks, instead of outputting console, , due fact there multiple borrowers, there way can put output of function or borrower executing function within queue, that, say, every 3 seconds, queue processes entry, runs function output each function put queue prints out in intervals.

at point using lock system (which unsure of how execute on multiple threads) dictate borrower sends function queue, if helps confusion.

the borrower code below :

public class borrower extends thread {  private int noofbooks; private set<book> booksborrowed; private set<integer> booksrequested; private int id; private int runs; private library library; private random randsleep = new random();  public borrower(int id, library library, int runs) {      this.library = library;     this.id = id;     this.runs = runs;     noofbooks = 1;      }  public borrower(){}  public string getloans() {     string output = "";     for(book b : booksborrowed) {         output +=" "+b.getbookid()+" ";     }     return output; }  public void run() {        try {         initialize();          for(int = 0; < runs; i++) {             requestbooks();             returnbooks();         }      } {} }  public synchronized void initialize() {      int min = 1;     int max = 10;     random r = new random();     noofbooks = r.nextint(max - min + 1) + min;      system.out.println("--------------------------");     system.out.println("borrower "+id+" starting");      notifyall(); }  public synchronized void requestbooks () {      random r2 = new random();     book temp = null;      arraylist<book>books = new arraylist<book>(library.getbooks());     arraylist<integer>chosen = new arraylist<integer>();      for(int = 0; < noofbooks; i++){         int index = r2.nextint(books.size());         temp = books.get(index);         int tempid = temp.getbookid();         chosen.add(tempid);     }     system.out.println("--------------------------");     system.out.println("\nborrower "+id+" requests " +noofbooks+" books library ");      booksrequested = new hashset<integer>(chosen);      string requestedbooks = "";      for(integer bookid : chosen) {         requestedbooks = requestedbooks+bookid+" ";         booksrequested.add(bookid);      }     system.out.println("borrower "+id+" request books: "+requestedbooks);      booksborrowed = library.rqst(id,booksrequested);     arraylist<book> chosenbooks = new arraylist<book>();     chosenbooks.addall(booksborrowed);      system.out.println("books requested borrower "+id+" : "+requestedbooks+"\n");      string receivedbooks = "";     book[]bookarray = booksborrowed.toarray(new book[booksborrowed.size()]);     for(book b : bookarray) {         receivedbooks = receivedbooks+b.getbookid()+" ";     }      system.out.println("borrower "+id+" books recieved :"+receivedbooks);     system.out.println("--------------------------");     notifyall();  }  public synchronized void returnbooks() {     set<integer> booksreturned;     arraylist<integer> returningbooks = new arraylist<integer>();     string returnedbooks = "";     arraylist<book> borrowed = new arraylist<book>(booksborrowed);      (book b : borrowed) {         returningbooks.add(b.getbookid());         returnedbooks = returnedbooks+b.getbookid()+" ";     }      booksreturned = new hashset<integer>(returningbooks);      library.rtrn(booksreturned);     system.out.println("\nborrower "+id+" returned books library: "+returnedbooks+"\n"); }    

here librarian code :

public class librarian extends thread {  library library; int runs = 0;  public librarian(library library, int runs) {     this.library = library;     this.runs = runs;  } public arraylist<book> addbooks() {       arraylist<book> addbooks = new arraylist<book>();      addbooks.add(new book("the lord of rings, fellowship",1,5));     addbooks.add(new book("the lord of rings, 2 towers",2,5));     addbooks.add(new book("the lord of rings, return of king",3,5));     addbooks.add(new book("the hunger games",4,5));     addbooks.add(new book("a game of thrones",5,5));     addbooks.add(new book("storm of swords",6,5));     addbooks.add(new book("the eagles lair",7,5));     addbooks.add(new book("the philosophers stone",8,5));     addbooks.add(new book("programming in java",9,2));     addbooks.add(new book("hit , run",10,5));     addbooks.add(new book("the man in iron mask",11,5));     addbooks.add(new book("a dance dragons",12,5));     addbooks.add(new book("a feast crows",13,5));     addbooks.add(new book("jack ripper",14,1));     addbooks.add(new book("the oddessey",15,1));      return addbooks;   } @override public void run() {     try {         //random rsleep = new random();         initialize();      for(int = 0;i < runs; i++) {          //sleep(rsleep.nextint(10000));         getborrowers();     }      } catch(exception e){} }  public void initialize(){     system.out.println("\nstarting librarian");     system.out.println("library initializing\n"); }  public void librarystatus() {  }  public void getborrowers() {     system.out.println("\ngetting current borrowers\n");     string borrowedbooks ="";     set<entry<integer,set<book>>> tempset = library.getborrowers().entryset();     for(entry<integer,set<book>> entry : tempset) {          for(book b : entry.getvalue()) {             borrowedbooks = borrowedbooks + b.tostring()+"";          }          system.out.println("borrower "+entry.getkey()+" borrowed books : "+borrowedbooks);         borrowedbooks ="";     } }  public void setlibrary(library library2) {     this.library = library2;  }    

}

and here main class controls others :

public class librarymodel {  library library = new library(); librarian librarian; arraylist<borrower> borrowarray = new arraylist<borrower>(); int runs = 0; int isfinished; boolean isactive = true;   public librarymodel() {     library.addbooks();  }  public static void main(string[]args)  {     int borrowcount = 0;     system.out.println("welcome library simulator\n");       librarymodel model = new librarymodel();      char quit = 'y';     scanner sc = new scanner(system.in);      while(quit != 'q')  {         system.out.println("\n[s = start, = about, q = quit]");          string = sc.next();          switch (a) {              case "s":                 model.isactive = true;                 system.out.println("please enter number of borrowers\n");                 borrowcount = sc.nextint();                 system.out.println("please enter how many runs program run");                 model.runs = sc.nextint();                 model.isfinished = model.runs;                  model.librarian = new librarian(model.library,model.runs);                       for(int = 0 ;i < borrowcount; i++) {                         model.getborrowers().add(new borrower(i+1,model.getlibrary(),model.runs));                     }                     model.librarian().start();                      for(borrower b : model.getborrowers()) {                         b.start();                     }                      try {                 model.librarian.join();                 for(borrower b : model.getborrowers()) {                     b.join();                 }             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }             break;              case "a":                 break;              case "q" :                 quit = 'q';                 break;              default :                 system.out.println("incorrect entry, please enter correct");                 break;             }     }    }  public library getlibrary() {     return library; }  public arraylist<borrower> getborrowers() {     return borrowarray; }  public librarian librarian() {     return librarian; }   public int runs() {     return runs; } 

}

thankyou in advance help


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 -