我有一个在EDT上运行的方法,我想在新的(非EDT)线程上执行某些操作.我目前的代码如下:
@Override
public void actionPerformed(ActionEvent arg0) {
//gathering parameters from GUI
//below code I want to run in new Thread and then kill this thread/(close the JFrame)
new GameInitializer(userName, player, Constants.BLIND_STRUCTURE_FILES.get(blindStructure), handState);
}
Run Code Online (Sandbox Code Playgroud) 我们遇到了一个错误,我们无法追踪到什么东西冻结了我们的摇摆线程(它已经差不多2周了,没有真正的结果) - 我们是经验丰富的Swing程序员,但我们有一个庞大的程序,并相信它是在一些遗留代码
我想知道,除了编辑EventQueueJDK中的实际类之外还有什么方法可以让我们查看当前在事件调度线程上运行的所有代码 - 也许某种类型的工具可以让我们查看它们的内容进入或离开事件派遣线程?
我只是想知道是否仍然需要确保invokeLater()Runnable的同步性.
我遇到了死锁,需要在保持并发性的同时克服它.
这是一个好代码的例子吗?:
private String text;
private void updateText()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
synchronized(FrameImpl.this)
{
someLabel.setText(text);
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
对于相当糟糕的示例感到抱歉,但我们必须假设它text被不同的线程修改,无法注入,并且依赖于正确的值.
这是正确的解决方案,还是通过将同步代码发送到未知的上下文中而无意中造成死锁问题?
谢谢.
我有以下Java程序,其中大约50%的启动尝试启动.其余的时间它在后台接缝死锁而不显示任何GUI.我将问题追溯到JTextArea对象的setText方法.使用像JButton这样的另一个类可以使用setText,但是使用JTextArea死锁.任何人都可以向我解释为什么会发生这种情况以及以下代码有什么问题:
public class TestDeadlock extends JPanel {
private JTextArea text;
TestDeadlock(){
text = new JTextArea("Test");
add(text);
updateGui();
}
public static void main(String[] args){
JFrame window = new JFrame();
window.setTitle("Deadlock");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(new TestDeadlock());
window.pack();
window.setVisible(true);
}
public synchronized void updateGui(){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
System.out.println("Here");
text.setText("Works");
System.out.println("Not Here");
}
});
}
Run Code Online (Sandbox Code Playgroud)
}
我有一个奇怪的问题......我是一个相对较新的"狂热者"Java程序员(我曾经在以前的职业生涯中为我的生活黑客攻击Perl),正在开发我的第一个半实际应用程序."Main-Class"是MyApp创建UserInputDialog实例的类.
UserInputDialog是一类我写了extends ^ JFrame,农具ActionListener和KeyListener,使用FlowLayout,并呈现给用户一个JLabel,JTextField以及取消/ OK JButton秒.当JTextField生成一个KeyEventwhere keyReleased() == KeyEvent.VK_ENTER,或者当"OK" JButton生成一个时ActionEvent,UserInputDialog做一些输入验证,调用setVisible(false),然后调用MyApp.doSomething( JTextFieldInstance.getText() ).
这一切都很完美.但现在我正在尝试添加进度窗口MyApp,因为doSomething()偶尔可能需要相当长的时间才能完成.
我创建了这个ProgressWindow类,它扩展JFrame,使用BorderLayout和抛出一个JProgressBar.NORTH和a JScrollPane(包装JTextArea)in.CENTER.ProgressWindow从ProgressWindowTester测试数据实例化并提供测试数据时,它可以完美地工 它也能正常工作,如果我复制并粘贴从环路测试ProgressWindowTester到MyApp,并没有MyApp实例化UserInputDialog(即没有什么 …
我正在编写套接字编程.它具有服务器和客户端的GUI.在服务器GUI中有一个文本字段,显示用户请求的单词.但我在表达这个词时遇到了问题.
我试过了
txtWord.setText(sentword);
Run Code Online (Sandbox Code Playgroud)
它没有在文本字段中显示该单词.但是当我写这个
txtWord.setText(sentword);
JOptionPane.showMessageDialog(null, "the requesed word is: "+sentword);
Run Code Online (Sandbox Code Playgroud)
然后它会在文本字段中显示该单词,并在消息框中显示该单词.
我尝试过repaint()但是它有效.请尽快给我一些解决方案
我有一个Gui应用程序,可以在串行热敏打印机上打印票据.当我点击启动此操作的按钮时,我的GUI被冻结.我认为那是因为代码是在EDT上执行的.我使用jstack确定但我不理解下面的结果:
Full thread dump Java HotSpot(TM) Client VM (23.3-b01 mixed mode, sharing):
"Thread-12" prio=6 tid=0x03012000 nid=0xd04 runnable [0x038ef000]
java.lang.Thread.State: RUNNABLE
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
"Thread-6" prio=6 tid=0x0302c400 nid=0x1b0 waiting on condition [0x039ef000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at posO2.Threads.ThreadHorloge.run(ThreadHorloge.java:46)
"Thread-5" prio=6 tid=0x03511c00 nid=0x9e4 waiting on condition [0x0399f000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at posO2.Threads.ThreadIgor.run(ThreadIgor.java:29)
"Thread-7" prio=6 tid=0x034b9800 nid=0xb40 waiting on condition [0x0394f000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at posO2.Threads.ThreadHorloge.run(ThreadHorloge.java:46)
at java.lang.Thread.run(Unknown Source)
"Thread-4" prio=6 tid=0x0318ec00 nid=0xa08 …Run Code Online (Sandbox Code Playgroud) 我有一个函数graphics()创建我的JFrame和两个JRadioButtons并向它们添加ActionListeners.这个图形从main()调用,图形本身调用game().
public void game() throws Exception
{
jTextArea1.setLineWrap(true);
jTextArea1.setWrapStyleWord(true);
jTextArea1.setText("This is private information.");
jRadioButton1.setVisible(true);
jRadioButton2.setVisible(true);
try {
t.sleep(40000);
repaint();
} catch (InterruptedException e) {
// We've been interrupted: no more messages.
return;
}
Run Code Online (Sandbox Code Playgroud)
显示"这是私人信息"后.在文本区域中,我希望程序执行暂停40秒,或者直到用户按下JRadioButton,以较早者为准.所以我添加了一个ActionListener并在其中调用了t.interrupt().
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
t.interrupt();
jRadioButton1.setVisible(false);
jRadioButton2.setVisible(false);
//System.out.println(t.interrupted());
jTextArea1.setText("Please wait...");
}
Run Code Online (Sandbox Code Playgroud)
但是,即使选择了应该触发中断的JRadioButton,也不会发生这种情况,并且t.interrupted返回false.
任何帮助,将不胜感激.
我有一个按钮执行一个很长的功能,我想在用户点击它之后禁用此按钮,以防止他多次再次点击它
该按钮被禁用,但问题是在功能完成后按钮再次启用
我试图放入button.setEnabled(false);一个新的线程,但它没有工作
用于测试此代码示例
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
button.setEnabled(false);
for (int i = 0; i < Integer.MAX_VALUE; i++) {
for (int j = 0; j < Integer.MAX_VALUE; j++) {
for (int ii = 0; ii < Integer.MAX_VALUE; ii++) {
}
}
}
}
});
Run Code Online (Sandbox Code Playgroud) 这是更好地javax.swing.Timer在swing应用程序内使用而不是使用java.util.Timer?
例如:
Timer timer = new Timer(1000, e -> label.setText(new Date().toString()));
timer.setCoalesce(true);
timer.setRepeats(true);
timer.setInitialDelay(0);
timer.start();
Run Code Online (Sandbox Code Playgroud)
要么
new java.util.Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
label.setText(new Date().toString());
}
}, 0, 1000);
Run Code Online (Sandbox Code Playgroud)
这两个有什么区别吗?
java ×10
swing ×10
concurrency ×4
awt ×3
deadlock ×1
debugging ×1
invokelater ×1
java-8 ×1
jbutton ×1
jstack ×1
jtextarea ×1
jtextfield ×1
sockets ×1
timer ×1