线程和任务问题

时间:2015-11-25 05:43:17

标签: java multithreading task

我目前正在学习Java。我正在创建一个项目,从用户获取信息,然后创建SQL文件。在那个(漫长的)过程中,我使用“任务”和“线程”显示一个显示进度的面板。但是,我发现如果我在开始时等待10秒,代码会按预期执行,但如果没有睡眠,程序会立即退出。非常感谢任何信息!

    package GUI;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;

    public class GUIMainPanel implements ActionListener
    {
        private static JFrame frame;

    public GUIMainPanel()
    {
            // Code that creates a panel, getting information from the user
    }

    public static void main(String[] args)
    {
        GUIMainPanel gui = new GUIMainPanel();
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand() == "Continue")
        {
            Runnable r1 = new Runnable()
            {
                public void run()
                {
                    // Progress Monitor frame
                    ProgressMonitor pbd = new ProgressMonitor();
                }
            };

            Runnable r2 = new Runnable()
            {
                public void run()
                {
                    // Code that creates files and changes the progress
                }
            };

            Thread thread1 = new Thread(r1);
            Thread thread2 = new Thread(r2);

            thread1.start();
            thread2.start();
        }
    }
}

    package GUI;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import Queries.CreateListsQueries;

import java.beans.*;

public class ProgressMonitor implements PropertyChangeListener
{
    private static JProgressBar progressBar; // Progress bar showing the progress of the query creation process
    private static JLabel creatingQueriesLabel; // Label showing information about the current state of the query
                                                    // creation process
    private Task task; // Task used to update the progress bar and label based on the current state of the query
                         // creation process
    private static JFrame frame;

    /**
     * Constructor
     */
    public ProgressMonitor()
    {
        // Code creating the Progress Monitor Panel and displaying it

        // Instances of javax.swing.SwingWorker are not reusuable, so
        // we create new instances as needed.
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }

    class Task extends SwingWorker<Void, Void>
    {
        /*
         * Main task. Executed in background thread.
         */
        @Override
        public Void doInBackground()
        {
            int progress = 0;
            double tempProgress = 0;
            String currentQueryText = "Test";

            try
            {
                // Infamous 10 seconds wait here
                Thread.sleep(10000);
            }
            catch (InterruptedException ignore)
            {
                ignore.printStackTrace();
            }

            // Initialize progress property.
            setProgress(0);

            while (progress < 100)
            {
                progress = GUIMainPanel.getProgress();
                setProgress(progress);
            }

            return null;
        }

        /*
         * Executed in event dispatching thread
         */
        @Override
        public void done()
        {
            Toolkit.getDefaultToolkit().beep();

            frame.setCursor(null); // turn off the wait cursor

            System.exit(0);
        }
    }

    /**
     * Invoked when task's progress property changes.
     */
    public void propertyChange(PropertyChangeEvent evt)
    {
        if ("progress".equals(evt.getPropertyName()))
        {
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
        }
        if ("currentQueryText".equals(evt.getPropertyName()))
        {
            String text = (String) evt.getNewValue();
            creatingQueriesLabel.setText(text);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

终于找到了它:)。

一旦我创建了多个任务(见下文),一切都进展顺利。感谢大家的帮助:)。

while (GUIMainPanel.getProgress() < 100)
{
    try
    {
        Thread.sleep(100);
    }
    catch (InterruptedException ignore)
    {
        ignore.printStackTrace();
    }

    task = new Task();
    task.addPropertyChangeListener(this);
    task.execute();
}