Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8862209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:40:23+00:00 2026-06-14T15:40:23+00:00

I am currently learning about multithreading in Java and ran into an interesting problem.

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:40:24+00:00Added an answer on June 14, 2026 at 3:40 pm

    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);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently learning about basic networking in java. I have been playing around
I am currently learning Objective-C. I have created a class to hold information about
I'm currently learning about class inheritance in my Java course and I don't understand
I am currently learning C# and LINQ. I have lots of questions about them.
im currently learning python (in the very begining), so I still have some doubts
im currently learning stacks in java and have a quick question. what will the
Some background first. I am currently learning some stuff about monadic parser combinators. While
I'm currently learning about pointers in my C++ Algorithms class and while I understand
I'm currently learning about structs, so I have the following exercise: Set a Struct
I'm currently taking a class where we are learning about synchronization of threads. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.