gpt4 book ai didi

java - 在另一个线程运行时执行任务

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

我目前正在学习 Java 中的多线程,遇到了一个有趣的问题。我有一个“loader”类,它读取一些 CSV 文件。

public class LoaderThread implements Runnable{

@Override
public void run(){
//do some fancy stuff
}
}

此外,我有一个 SplashScreen,我想在加载数据时显示它。

import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingConstants;

public class SplashScreen extends JWindow{

JWindow jwin = new JWindow();

public SplashScreen(){

jwin.getContentPane().add(new JLabel("Loading...please wait!",SwingConstants.CENTER));
jwin.setBounds(200, 200, 200, 100);

jwin.setLocationRelativeTo(null);

jwin.setVisible(true);

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

jwin.setVisible(false);
jwin.dispose();

}
}

当用户单击按钮时,代码从我的主类运行:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)   {                                         


final Thread t = new Thread() {

@Override
public void run() {

LoaderThread myRunnable = new LoaderThread();
Thread myThread = new Thread(myRunnable);
myThread.setDaemon(true);
myThread.start();
while(myThread.isAlive()==true)
{
SplashScreen ss = new SplashScreen();
}


}
};
t.start(); // call back run()
Thread.currentThread().interrupt();

}

此设置有效,但当加载时间超过 3 秒时,消息会“闪烁”,并且显示至少 3 秒,即使加载过程可能更短。

我现在想知道是否可以在加载线程运行时显示该消息。不长也不短。

提前致谢!

最佳答案

这是观察者模式发挥良好作用的完美示例。在 Swing 中轻松完成此操作的一种方法是使用 SwingWorker 作为后台线程。执行 SwingWorker 时显示启动屏幕。在执行 SwingWorker 之前,向其添加一个 PropertyChangeListener,当它以 SwingWorker.StateValue.DONE 返回时,摆脱启动屏幕。

另外,不要像您正在做的那样在 Swing 事件线程上调用 Thread.sleep(...),因为这会导致灾难。

编辑 1
关于您的评论--

What do you mean by "Also don't call Thread.sleep on the Swing event thread..."? What do you mean by "Also don't call Thread.sleep on the Swing event thread..."?

这是在 Swing 事件线程上调用的,也称为 EDT 或 Event Dispatch Thread:

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

这将使您的整个应用程序进入休眠状态,使其完全无响应,因此建议您永远不要在 EDT 上调用 Thread.sleep(...)

编辑2
示例代码:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

public class SwingWorkerEg extends JPanel {
private static final int PREF_W = 300;
private static final int PREF_H = 200;

public SwingWorkerEg() {
add(new JButton(new ButtonAction("Press Me")));
}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

private static void createAndShowGui() {
JFrame frame = new JFrame("SwingWorkerEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SwingWorkerEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class ButtonAction extends AbstractAction {
public ButtonAction(String title) {
super(title);
}

@Override
public void actionPerformed(ActionEvent actEvt) {
final JButton source = (JButton)actEvt.getSource();
source.setEnabled(false);
MySwingWorker mySw = new MySwingWorker();
final MySplashScreen mySplash = new MySplashScreen();
mySplash.setVisible(true);

mySw.addPropertyChangeListener(new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if (SwingWorker.StateValue.DONE == pcEvt.getNewValue()) {
mySplash.setVisible(false);
mySplash.dispose();
source.setEnabled(true);
}
}
});
mySw.execute();
}
}

class MySwingWorker extends SwingWorker<Void, Void> {
private static final long SLEEP_TIME = 5 * 1000;

@Override
protected Void doInBackground() throws Exception {
Thread.sleep(SLEEP_TIME); // emulate long-running task
return null;
}
}

class MySplashScreen extends JWindow {
private static final String LABEL_TEXT = "Loading, ... please wait...";
private static final int PREF_W = 500;
private static final int PREF_H = 300;

public MySplashScreen() {
add(new JLabel(LABEL_TEXT, SwingConstants.CENTER));
pack();
setLocationRelativeTo(null);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
}

关于java - 在另一个线程运行时执行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13502080/

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