虽然在循环完成之前循环使用Java循环

时间:2015-04-24 14:26:48

标签: java while-loop

我在运行菜单系统的while循环方面遇到了麻烦。

这是我正在使用的代码。每个线程或借用者在每个线程中使用sleep()命令同时执行任务。我现在遇到的问题是每个借款人线程运行一次,例如

for(int i = 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();
}

然而,借款人开始,由于涉及sleep()命令,While循环返回并重新打印线程输出中的菜单。

有没有办法确保While循环只在所有线程完成后循环回来?

以下是我正在使用的代码:

****编辑并更新了代码*****

package model;

import java.util.ArrayList;
import java.util.Scanner;

public class LibraryModel implements Runnable{
  Library library = new Library();
  Librarian librarian;
  ArrayList<Borrower> BorrowArray = new ArrayList<Borrower>();
  int runs = 0;
  Boolean isActive = true;

public LibraryModel() {
    library.AddBooks();
}

public static void main(String[]args) 
{
    int borrowCount = 0;
    System.out.println("Welcome to the Library Simulator\n");


    LibraryModel model = new LibraryModel();
    Scanner sc = new Scanner(System.in);
    String a = sc.next();
    char quit = 'y';

    while(quit != 'q')  {

    String a = sc.next();
    System.out.println("\n[S = start, A = About, Q = quit]");

    switch (a) {

        case "S":
           System.out.println("Please enter the number of Borrowers\n");
           borrowCount = sc.nextInt();
           System.out.println("Please enter how many runs the Program will run");
           model.runs = sc.nextInt();

           model.librarian = new Librarian(model.library,model.runs);

           for(int i = 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();
                }
        break;


        case "A":
        break;

        case "Q" :
            quit = 'q';
        break;


        default :
            System.out.println("Incorrect Entry, please enter a correct");
        break;
    }
}

涉及借款人的请求代码:

package model;

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 i = 0; i < runs; i++) {
            RequestBooks();
            ReturnBooks();
        }

    } finally {}
}

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 i = 0; i < 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 from 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 by 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);

    for (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 to library: "+returnedBooks+"\n");
}

}

2 个答案:

答案 0 :(得分:0)

一旦启动了所有线程,就可以添加另一个在所有线程上调用join的循环。有关详细信息,请参阅tutorial

不是直接使用Threads,而应该使用Runnable,它允许你使用一些更高级别的抽象。例如:

ExecutorService threadPool = Executors.newCachedThreadPool();
threadPool.submit(librarian);
for (Borrower b : borrowers) {
    threadPool.submit(b);
}
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.DAYS); // do this in a loop or error if it hasnt finished?

答案 1 :(得分:0)

您可以使用join()等待线程完成。启动它们,然后再次循环调用join()。

https://docs.oracle.com/javase/tutorial/essential/concurrency/join.html