- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个网络抓取工具,该工具应该使用抓取的数据执行各种操作。
因此,我需要各种不同的 GUI 才能有序工作,因此,我需要 main 方法在每个 GUI 完成其目的之前等待。
经过一段时间的搜索,我发现了以下 StackOverflow 问题,它们提供了一些有关如何解决问题的线索,但我无法实现,因为它们与我的情况有一些差异:
How to wait for input in a text field How to make main thread wait a different thread to finish
<小时/>我知道我可以使用 GUI 组件(例如按钮)的监听器来触发代码,但我很难让主线程等待该监听器将其唤醒,而GUI 线程(如果有的话)的代码由主线程初始化...
<小时/>这是一个简化的代码,用于演示程序应该如何工作:
public class Main {
/*
* Waiter is a simple GUI with just an "Start" button in it. Here in place of my actual GUIs.
*/
private static Waiter auth; //Represents my NTLM-authentication form.
private static Waiter status; //Represents a status-feedback GUI that will be displayed during processing.
private static Waiter operation; //Represents a GUI in with the user choses what to do with the gathered data.
public static void main(String[] args) throws InterruptedException {
auth = new Waiter();
auth.setVisible(true);
System.out.println("NTLM Authentication form. Should wait here until user has filled up the GUI and clicked \"Start\".");
System.out.println("Authenticates WebClient's NTLM using data inputed to the GUI...");
auth.dispose();
Thread srt = new Thread(status = new Waiter());
srt.start();
status.setVisible(true);
//Performs webscraping operations...
System.out.println("Prepares the webscraped data here...Things like downloading files and/or parsing text...");
System.out.println("Keeps the user aware of the progress using the \"status\" GUI.");
status.setVisible(false);
//Clears the status GUI.
operation = new Waiter();
operation.setVisible(true);
System.out.println("Operation selection form. Should wait here until user selects an option.");
System.out.println("Starts performing the operation(s)...");
operation.dispose();
status.setVisible(true);
System.out.println("Performs the operation(s), while giving status-feedback to the user.");
status.setVisible(false);
System.out.println("Displays a file-save dialog to save the results.");
System.out.println("And finally, displays a \"End of operations\" dialog before ending.");
}
}
<小时/>
更新1:我遇到的主要困难是实现这样的事情(这就是我想做的):
//Main method...code...
Thread srt = new Thread(status = new Waiter());
//Before "srt.start();"...
status.startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
main.continueExecution();
}
});
//Thread's run() being something like "status.setVisible(true); main.waitGUI();"
srt.start();
//continues here after the Listener is triggered...more code...
而不是这个(如果我理解正确的话,大多数其他人的解决方案是什么......)(这是我不想要做的,如果可能的话):
//GUI before this one...
//code...
Thread srt = new Thread(status = new Waiter());
status.startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*
* Code that should come after this GUI.
*/
}
});
//Thread's run() being something like "status.setVisible(true);"
srt.start();
//"ends" here...(Initial code or GUI before this "status")
换句话说,我在实现 GUI 和监听器来触发主线程的“ sleep ”和“唤醒”操作时遇到了麻烦,而不是触发实际的处理代码。
<小时/>更新2:
按照 @JB_Nizet 关于 SwingUtilities.invokeLater()
的提示,我仔细查看了 SwingUtilities docs ,在我发现 SwingUtilities.invokeAndWait()
方法如何工作之后,我想我已经找到了如何使用 Semaphore
和 invokeAndWait()
.
我需要对多线程和/或 GUI 有更好了解的人来确认它是否是安全、有效的解决方案。 (然后我将编辑问题并清理它,如果得到确认,则以正确的“答案格式”发布)
无论如何,这是修改后的代码,它似乎对我有用:
public class Main_Test {
//Semaphore:
public static Semaphore semaphore;
//GUIs:
private static Waiter auth; //Represents my NTLM-authentication form.
public static void main(String[] args) {
try {
semaphore = new Semaphore(1);
// semaphore.acquire();
auth = new Waiter() {
@Override
public void run() {
try {
System.out.println(Main_Test.getThread() + this.getName() + " has been created and is now running.");
semaphore.acquire(); //Makes main pause.
this.setVisible(true);
} catch (InterruptedException ex) {
Logger.getLogger(Main_Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
auth.jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(getThread() + "NTLM has been hypothetically authenticated.");
semaphore.release(); //Makes main continue after GUI is done.
auth.dispose();
}
});
// semaphore.release();
SwingUtilities.invokeAndWait(auth);
semaphore.acquire(); //<- Where the main effectively gets paused until the permit is released.
/*
* GUI's run() will accquire the semaphore's permit.
* The invokeAndWait() garantees (?) it will happen before main's acquire().
* This causes the main to pause when trying to acquire the permit.
* It stays paused until the actionListener release() that permit.
*/
System.out.println(getThread() + "This message represents the processing, and should come only after the hypothetical NTLM authentication.");
} catch (InterruptedException ex) {
Logger.getLogger(Main_Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(Main_Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static String getThread() {
return String.format("%-32s --- ", Thread.currentThread().toString());
}
}
最佳答案
我不确定我是否完全理解你想要做什么,但在我看来,你有一个消费者线程(主线程,等待来自事件调度线程的事件)和一个生产者线程(事件调度线程)。
实现这一点的典型方法是使用阻塞队列作为通信机制:
关于java - 如何让 main 方法等待 GUI 上的输入而不使用 Listener 作为直接触发器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11767788/
我正在尝试捕捉评论状态事件,例如有人正在关闭它。并在从插件中获得任何生命迹象之后。向日志文件(从任何日志级别)或 STDOUT 发送一行。atlassian-plugins.xml 看起来像这样:
我在跑; sass --watch --style compressed --sourcemap css/sass:css 我得到了; NameError: uninitialized constan
您好,我正在创建一个 android 应用程序作为 ejabbered 服务器的 XMPP 客户端。 但我真的很困惑,因为我看到我可以将消息作为数据包或消息发送,我也可以使用 PacketListen
我有一个 imageview - 它的属性 -focusable 和 focusableintouchmode 都设置为 true 我已经在我的 Activity 中实现了 onFocus
我有一个查询来查找指定条形码的文档 ID: Future findBarcode() async { String searchBarcode = await BarcodeScanner.sca
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 5 年前。 Improve
这可能是一个非常基本的问题,但我就是不明白。使用 Express.js 创建应用和启动应用监听 1234 端口有什么区别,例如: var express = require('express'); v
所以我想知道什么是更好看的解决方案/有什么区别以及在决定制作多个监听器(1 个监听器用于 1 个按钮)还是仅 1 个 ActionListener 用于 GUI 中的所有按钮(大约 10 个按钮),并
我目前正在尝试制作一个聊天室服务器。我正在努力做到这一点,以便我可以同时监听新请求的连接和监听从已建立的连接发送的消息。 我可以用它来监听请求的连接: def reqlisten(): glo
有没有办法在事件监听器方法中访问类上下文并有可能删除监听器? 示例 1: import {EventEmitter} from "events"; export default class Event
应用程序在本地主机上工作正常。但是当它连接到服务器时出现错误。 我通过端口 22 连接服务器 这是错误 Error: listen EADDRNOTAVAIL Error: listen EADDRN
我有类似的东西: $scope.$on(config.SOME_CONSTANT, ()=> { activate(); // plus a bunch of instantiatio
我的 HTML 页面上有以下 Controller : ... ... 此子 Controller 映射到以下 c
我的 HTML 页面上有以下 Controller : ... ... 此子 Controller 映射到以下 c
我构建了一个自定义属性并将其添加到可观察列表中。但是,如果属性内容发生更改,则不会调用任何监听器。以下代码片段向您展示了“建筑”: public static final class TestObje
这里我不明白这两种方法的基本区别是什么。 var events = require('events'); var eventEmitter = new events.EventEmitter(); v
我正在尝试使用 grunt-express 设置 Grunt 来启动我的 Express 服务器。读完docs后和 this SO question ,我还是想不通。我已经为我的 Grunt 文件尝试
如果这里问题的某些方面不清楚,我深表歉意,因为我是 Node 和 javascript 的新手。请询问更多详情 我有一个使用 socketio 连接到 firebase 的 Node 应用程序。在 h
情况 我可能没有使用传统意义上的 PHPUnit。我正在使用带有 Selenium 2 的 PHPUnit。我们有这个想法来记录 Selenium 以“重现步骤”的方式执行的操作。这意味着如果我们调用
我正在尝试学习在 struts2 中使用 session 。所以,我只想实现一个登录/注销、配置文件应用程序。我正在关注互联网上提供的基本教程。但是,它根本不起作用。请帮助我解决以下问题如何解决。 S
我是一名优秀的程序员,十分优秀!