gpt4 book ai didi

java - Java 中的 while 循环在线程完成之前循环

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:46 26 4
gpt4 key购买 nike

我在运行菜单系统的 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");
}

}

最佳答案

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

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

关于java - Java 中的 while 循环在线程完成之前循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29850084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com