简体   繁体   中英

Java program “freezes shortly”

I am new at java and its not my first programming language. I actually try to code a downloader which downloads all files until the version on the server is the same as the client ones.

The program runs good but the is a thing i cant solve.

If i run this code :

private void updateContent()
{
    DownloadButton.setEnabled(false);
    SaveLocationButton.setEnabled(false);
    UpdateText.setText("Update to Version " + ServerVersionNumber + "...");
    UpdateText.setForeground(new Color(200, 150, 0, 255));
    ProgressText.setText("(1/2) Files will be downloaded");
    ProgressText.setForeground(new Color(200, 150, 0, 255));
    LoadingBar.setStringPainted(true);

    for(int i = 1; i < (ServerVersionNumber - LocalVersionNumber + 1); i++)
    {
        int CurrentUpdateFile = LocalVersionNumber + i;


        try 
        {
            URL FileURL = new URL(Host + CurrentUpdateFile + ".zip");

            try {
                FileURL.openConnection();


                InputStream reader = FileURL.openStream();
                FileOutputStream writer = new FileOutputStream(ContentSaveDirectory + CurrentUpdateFile + ".zip");

                byte[] buffer = new byte[153600];
                int bytesRead = 0;


                while ((bytesRead = reader.read(buffer)) > 0)
                {  
                    writer.write(buffer, 0, bytesRead);
                    buffer = new byte[153600];
                }

                writer.close();
                reader.close();

            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }

        } 
        catch (MalformedURLException e) 
        {
            e.printStackTrace();
        }
    }
}

all things like DownloadButton.setEnabled(false); and a few lines under will be changed after all file are downloaded could anyone say me why?

If you are calling this method from an event handler, it is executed in the event dispatch thread, and the download blocks repaints and event handling. On the other hand, updates to the GUI should be done only from the event dispatch thread. See http://docs.oracle.com/javase/tutorial/uiswing/concurrency/

The simplest fix is starting a new thread for the download, like below. For better integration with the GUI, like updating it when the download completes, refer to the tutorial.

private void updateContent()
{
    DownloadButton.setEnabled(false);
    SaveLocationButton.setEnabled(false);
    UpdateText.setText("Update to Version " + ServerVersionNumber + "...");
    UpdateText.setForeground(new Color(200, 150, 0, 255));
    ProgressText.setText("(1/2) Files will be downloaded");
    ProgressText.setForeground(new Color(200, 150, 0, 255));
    LoadingBar.setStringPainted(true);

    new Thread() {
        public void run() {
            for(int i = 1; i < (ServerVersionNumber - LocalVersionNumber + 1); i++)
            {
                // the rest
            }
        }
    }.start();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM