大家好,所以我必须创建两个同时出售火车票的线程,并在两个不同的窗口上显示输出,我创建了一个生成票和Runnable的类,但是我不确定如何显示票的输出我试图传递TextBox参数,但在两个不同的Text区域框中使用了两个不同的线程,但是没有任何效果吗?
pymongo.errors.ServerSelectionTimeoutError: cluster111-shard-111-111-11111.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get
local issuer certificate (_ssl.c:1056),cluster111-shard-111-111-11111.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local
issuer certificate (_ssl.c:1056),cluster111-shard-111-111-11111.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issu
er certificate (_ssl.c:1056)
答案 0 :(得分:0)
这是一个快速可运行的应用程序,以演示如何从后台线程更新2个单独的文本区域,就像您确保已通读解释发生情况的注释一样。显然,这与您的程序并不完全相同,但我保留了核心概念。
public class Main extends Application {
private AtomicInteger totalTickets = new AtomicInteger(100);
private boolean isSellingTickets = false;
@Override
public void start(Stage stage) {
VBox vBoxContainer = new VBox();
vBoxContainer.setAlignment(Pos.CENTER);
vBoxContainer.setPrefSize(400,250);
TextArea textAreaTop = new TextArea();
TextArea textAreaBottom = new TextArea();
vBoxContainer.getChildren().addAll(textAreaTop, textAreaBottom);
Button controlSalesButton = new Button("Start Ticket Sales");
controlSalesButton.setOnAction(event -> {
if(isSellingTickets) {
isSellingTickets = false;
controlSalesButton.setText("Start Ticket Sales");
}
else {
isSellingTickets = true;
//I didn't name these threads because I never reference them again
// of course create var name if you need them
new Thread(() -> sellTickets(textAreaTop)).start();
new Thread(() -> sellTickets(textAreaBottom)).start();
// This is on the main Thread so no need for Platform.runLater(...)
controlSalesButton.setText("Stop Ticket Sales");
}
});
vBoxContainer.getChildren().add(controlSalesButton);
stage.setScene(new Scene(vBoxContainer));
stage.show();
}
private void sellTickets(TextArea textArea){//This Whole Function is on another thread don't forget about that
while(isSellingTickets && totalTickets.get()>1) { //Continue selling tickets until you stop
// And Make Sure there is tickets to sell
//Platform.runLater(... is used to update the main thread by pushing updates to
// the JavaFX Application Thread at some unspecified time in the future.
Platform.runLater(() -> {
textArea.appendText("SOLD Ticket Number:" + (int) (Math.random() * 1000 + 1000) + " \t"
+ totalTickets.decrementAndGet() + " Left to sell\n");
});
try {
Thread.sleep((int) (Math.random() * 1000 + 100));//Different selling speeds this is irrelevant
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
如果您有任何问题让我知道