Reputation: 385
I am currently learning about multithreading in Java and ran into an interesting problem. I have a "loader" class which reads some CSV file.
public class LoaderThread implements Runnable{
@Override
public void run(){
//do some fancy stuff
}
}
Furthermore I have a SplashScreen which I want to be shown while the data is loading.
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();
}
}
The code is run from my main class when the user clicks on a button:
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();
}
This setup is working but the message is "blinking" when the loading takes longer than 3 secs and is shown for at least 3 secs, even though the loading process might be shorter.
I am now wondering if it is possible to show the message for as long as the loading thread is running. Not longer and not shorter.
Thanks in advance!
Upvotes: 2
Views: 1050
Reputation: 285405
This is a perfect example of where an observer pattern would work well. One way to do this easily in Swing is to use a SwingWorker for your background thread. Show the splash screen when you execute the SwingWorker. Before executing the SwingWorker, add a PropertyChangeListener to it, and when it returns with SwingWorker.StateValue.DONE, get rid of the splash screen.
Also, don't call Thread.sleep(...)
on the Swing event thread like you're doing as that's a guarantee for disaster.
Edit 1
Regarding your comment --
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..."?
This is being called on the Swing event thread, otherwise known as the EDT or Event Dispatch Thread:
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
This will put your whole application to sleep making it completely non-responsive, and so it is recommended that you never call Thread.sleep(...)
on the EDT.
Edit 2
example code:
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);
}
}
Upvotes: 5
Reputation: 496
You could use a monitor http://en.wikipedia.org/wiki/Monitor_(synchronization)
That way you can ensure that your thread is ended when the task is done and not waiting for an arbitrary sleep.
Basically you want to do this (needs to be expanded):
public class Monitor {
private boolean task = false;
public void synchronized waitForTask(){
while(!task){
wait();
}
}
public void synchronized taskIsDone(){
task = true;
notifyAll();
}
}
Both objects need a reference to this Monitor. Your view have to call waitForTask, and your task will have call taskIsDone.
Upvotes: 1
Reputation: 1994
You are creating new instances in a loop, that's why it is blinking. You'd rather create one instance, make it visible, and when the other thread finished executing, dispose it.
Upvotes: 5