gpt4 book ai didi

java - Swing Worker 和普通线程之间的区别?

转载 作者:行者123 更新时间:2023-12-01 17:02:22 25 4
gpt4 key购买 nike

根据我之前的问题What's the best way to have a dynamic JTable whose data updates itself with changes in database table? ,我使用了以下代码来自动更新 jList

       DefaultListModel x = new DefaultListModel();
jList1.setModel(x);
int initialDelay = 0;
int period = 5000;
Timer timer = new Timer();
TimerTask task = new TimerTask() {

public void run() {
x.clear();
try {
conn = Database.Connect("patient.db");
sql = "SELECT * FROM PATIENT_APPOINTMENTS";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();

while(rs.next()) {

x.addElement(rs.getString("patientid"));

}


}
catch ( Exception e) {

JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);
}
}
};
timer.scheduleAtFixedRate(task,initialDelay,period);

但是我读过有关 SwingWorker 的内容,这似乎是正确的方法。Swing Worker 的正常实现有 3 个 protected 方法: doInBackground 、publish 和 did 。我还尝试了 SwingWorker 的另一个版本,即

    DefaultListModel x = new DefaultListModel();
jList1.setModel(x);

int initialDelay = 0;
int period = 5000;
Timer timer = new Timer();
TimerTask task = new TimerTask() {

public void run() {
x.clear();

try {

SwingWorker<ArrayList<String>, Void> worker = new SwingWorker<ArrayList<String>, Void>() {

protected ArrayList<String> doInBackground() throws Exception {

ArrayList<String> patientid = new ArrayList<String>();
conn = Database.Connect("patient.db");
sql = "SELECT * FROM PATIENT_APPOINTMENTS";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();

while (rs.next()) {

patientid.add(rs.getString("patientid"));

}

return patientid;
}

protected void done() {

ArrayList<String> id = new ArrayList<String>();
try {

id = get();
for (int i = 0; i < id.size(); i++) {
x.addElement(id.get(i));
}

} catch (InterruptedException e) {

JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
} catch (ExecutionException e) {
JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
}
}

};
worker.execute();
} catch (Exception e) {

JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
}

}
};
timer.scheduleAtFixedRate(task, initialDelay, period);
}

哪种方法更好?此外,jList 每 5 秒闪烁 5 次,我该如何避免这种情况?

最佳答案

Swing Timer 在事件调度线程的上下文中执行通知,使其适合更新 UI,但不适合执行长时间运行或 block 任务(例如查询数据库)

Thread 适合执行长时间运行或 block 任务,但不应该用于更新 UI,因为 Swing 不是线程安全的,并且只能在该上下文中修改 UI。美东时间。当然,您可以使用 EventQueue.invokeLater,但是如果您需要向这些调用提供参数,就会变得很痛苦...

您可以使用 SwingWorker 来代替,它能够在后台(EDT 之外)执行长时间运行或阻塞任务,但提供了更新 UI 的方法,通过它的发布/处理完成和/或PropertyChange支持

看一下:

了解更多详情

Which is the better method

您的第一个示例违反了 Swing 的单线程规则,因此它没有多大用处...

你的第二个例子已经结束了,TimerTask已经在后台运行,你实际上最好使用ScheduledExecutorService并向其传递 SwingWorker 的实例...

已更新

这不是我希望的工作方式,但是......

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

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 JLabel label;
private int count;
private ScheduledExecutorService service = Executors.newScheduledThreadPool(1);

public TestPane() {
setLayout(new GridBagLayout());
label = new JLabel("...");
add(label);

service.schedule(new CounterWorker(), 1, TimeUnit.SECONDS);
}

public class CounterWorker extends SwingWorker<Integer, Integer> {

@Override
protected Integer doInBackground() throws Exception {
System.out.println("Tick");
for (int index = 0; index < 100; index++) {
count++;
publish(count);
Thread.yield();
}
return count;
}

@Override
protected void process(List<Integer> chunks) {
label.setText(Integer.toString(chunks.get(chunks.size() - 1)));
}

@Override
protected void done() {
service.schedule(new CounterWorker(), 1, TimeUnit.SECONDS);
}

}

}

}

关于java - Swing Worker 和普通线程之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26836742/

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