gpt4 book ai didi

java - 正确使用线程难倒了我

转载 作者:行者123 更新时间:2023-12-01 22:27:07 26 4
gpt4 key购买 nike

我正在尝试学习如何使用线程。我使用 API 和文档制作了一个示例,试图使其尽可能简单。一个 Form 2 按钮和一个要更新的进度条 我无法从 API 示例中使其正常工作。首先是我的基本表单,然后是应该正确运行线程的类。当我单击按钮时,它会卡住 GUI 并且不会更新进度栏,有些人提到我阻止了 GUI 线程......这让我很困惑。谁能告诉我哪里出错了。

package mythreadtry;

/**
*
* @author brett
*/
public class MyThreadMainGui extends javax.swing.JDialog {

/**
* Creates new form MyThreadMainGui
*/
public MyThreadMainGui(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jProgressBar1 = new javax.swing.JProgressBar();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addContainerGap(208, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(210, Short.MAX_VALUE)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(35, 35, 35))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
MyThreadTry mmt = new MyThreadTry();
mmt.doit(null);
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
MyThreadTry mmt = new MyThreadTry();
mmt.doit(null);
}
//
public void updateProgress(int newValue){
jProgressBar1.setValue(newValue);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MyThreadMainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MyThreadMainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MyThreadMainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MyThreadMainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
MyThreadMainGui dialog = new MyThreadMainGui(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JProgressBar jProgressBar1;
// End of variables declaration
}

接下来是我对一个线程的尝试..我希望这很简单。我包含了整个表格,这样你们就可以知道我是如何尝试给类(class)打电话的。

package mythreadtry;

import java.awt.event.MouseEvent;

/**
*
* @author brett
*/
public class MyThreadTry implements Runnable{
long minPrime;
volatile Thread p = new Thread();
volatile boolean threadSuspended;
MyThreadTry() {
int times = 100000;
for(int i = 0; i <= times; i++ ){
this.minPrime = i;
System.out.println(this.minPrime);
MyThreadMainGui myg = new MyThreadMainGui(null, threadSuspended);
myg.updateProgress((int)this.minPrime);
}

}
public void doit(String[] args) {
p.start();

}
public void run() {
Thread thisThread = Thread.currentThread();
threadSuspended = true;
while (p == thisThread) {
try {
p.sleep(500);

synchronized(this) {
while (threadSuspended && p==thisThread)
wait();
}
} catch (InterruptedException e){
}
}
}
public synchronized void mousePressed(MouseEvent e) {
e.consume();

threadSuspended = !threadSuspended;

if (!threadSuspended)
notify();
}
public synchronized void stop() {
this.p = null;
notify();
}
/**
* @param args the command line arguments
*/


}

最佳答案

When I click a button it freezes the GUI and doesn't update the progress bar

当您按下其中一个按钮时,您将创建一个 MyThreadTry 实例

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
MyThreadTry mmt = new MyThreadTry();
mmt.doit(null);
}

然后,MyThreadTry 的构造函数循环 100、000 次,创建 100、000 个 MyThreadMainGUI 实例

MyThreadTry() {
int times = 100000;
for(int i = 0; i <= times; i++ ){
this.minPrime = i;
System.out.println(this.minPrime);
MyThreadMainGui myg = new MyThreadMainGui(null, threadSuspended);
myg.updateProgress((int)this.minPrime);
}
}

所有这些都在事件调度线程的上下文中。 EDT 负责处理事件队列,其中包括绘制事件等。

这意味着在该方法实际完成之前,无法更新任何内容。

参见Concurrency in Swing了解更多详情。

当构造函数“finally”返回时,您调用 doit,它又调用 p.start。但由于您没有重写 prun 方法,也没有为其提供 Runnable,所以它什么也不做...

更新了可运行的示例

在这些情况下,我的偏好是使用 SwingWorker,但由于我们讨论的是线程,因此这里使用线程版本。

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private JProgressBar pb;
private JButton button;

private Worker worker;

public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;

pb = new JProgressBar();
button = new JButton("Run");

add(pb, gbc);
add(button, gbc);

pb.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println(pb.getValue());
if (pb.getValue() >= 100) {
button.setText("Run");
worker = null;
}
}
});

pb.addPropertyChangeListener("value", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
}
});

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (worker == null) {
worker = new Worker(pb);
Thread t = new Thread(worker);
t.start();
button.setText("Pause");
} else {
if (worker.isPaused()) {
button.setText("Pause");
worker.resume();
} else {
button.setText("Resume");
worker.pause();
}
}
}
});
}

}

public class Worker implements Runnable {

private ReentrantLock pausedLock;
private Condition pausedCondition;
private AtomicBoolean paused;

private JProgressBar pb;

public Worker(JProgressBar pb) {
paused = new AtomicBoolean(false);
pausedLock = new ReentrantLock();
pausedCondition = pausedLock.newCondition();
this.pb = pb;
}

public void pause() {

paused.set(true);

}

public void resume() {

paused.set(false);
pausedLock.lock();
try {
pausedCondition.signal();
} finally {
pausedLock.unlock();
}

}

@Override
public void run() {

int times = 100_000;
for (int i = 0; i <= times; i++) {
checkPauseState();
updateProgress(Math.round((i / (float) times) * 100f));
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
}
}

}

protected void checkPauseState() {

while (paused.get()) {
pausedLock.lock();
try {
pausedCondition.await();
} catch (InterruptedException ex) {
} finally {
pausedLock.unlock();
}
}

}

protected void updateProgress(int progress) {
if (EventQueue.isDispatchThread()) {
pb.setValue(progress);
} else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
updateProgress(progress);
}
});
}
}

public boolean isPaused() {
return paused.get();
}

}

}

确保您有时间通读 Concurrency in Java ,特别是关于锁的部分,以及 Concurrency in Swing .

关于java - 正确使用线程难倒了我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28537303/

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