Java 为什么我的线程没有开始?

Java 为什么我的线程没有开始?,java,multithreading,user-interface,Java,Multithreading,User Interface,我正在制作一个线程应用程序,主类也是runnable类。由于某种原因,我的线程无法启动。知道为什么吗?updateThread是罪魁祸首 代码如下: package avtech.software.bitprice.display; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import avtech.soft

我正在制作一个线程应用程序,主类也是runnable类。由于某种原因,我的线程无法启动。知道为什么吗?updateThread是罪魁祸首

代码如下:

package avtech.software.bitprice.display;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import avtech.software.bitprice.dialogs.*;
import avtech.software.bitprice.listeners.*;

@SuppressWarnings("serial")
public class Display extends JFrame implements Runnable {

    /**
     * variables
     */
    private static final int WIDTH = 200, HEIGHT = 200;
    private static double currentPrice = 0.0;
    private static double pauseLength;
    private static boolean running = false;
    private static String greaterLess = "";
    /**
     * objects
     */
    private static Display d;
    private static Thread updateThread;
    private static JLabel currentPriceLabel = new JLabel("Current Price: $" + currentPrice);
    private static JTextField pullDelayArea = new JTextField("Price Pull Delay In Minutes...");
    private static JTextField priceValueArea = new JTextField("Price You Are Waiting For...");
    private static JButton update = new JButton("UPDATE DATA");

    public Display() {
        running = true; // program is now running
        updateThread = new Thread(d); // create the local thread

        /**
         * create the frame
         */
        setLayout(null);
        setSize(WIDTH, HEIGHT);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("BitPrice");

        /**
         * set bounds of the components
         */
        currentPriceLabel.setBounds(((WIDTH / 2) - (currentPriceLabel.toString().length() / 5)), 5, WIDTH, 50);
        pullDelayArea.setBounds(10, 40, WIDTH - 25, 25);
        priceValueArea.setBounds(10, 70, WIDTH - 25, 25);
        update.setBounds(10, 100, WIDTH - 25, 25);

        /**
         * set up the listeners to the components
         */
        pullDelayArea.addMouseListener(new PullDelayAreaListener(pullDelayArea));
        pullDelayArea.addActionListener(new PullDelayAreaListener(pullDelayArea));
        priceValueArea.addMouseListener(new PriceValueAreaListener(priceValueArea));
        priceValueArea.addActionListener(new PriceValueAreaListener(priceValueArea));
        update.addActionListener(new UpdateButtonListener());

        /**
         * add everything
         */
        add(currentPriceLabel);
        add(pullDelayArea);
        add(priceValueArea);
        add(update);

        setLocationRelativeTo(null);
        setVisible(true);
        requestFocus();

        /**
         * start the update process
         */
        System.out.println("hi");
        updateThread.start();
    }

    /**
     * everything that happens when the thread is updated, including updating
     * the price label, and all that good stuff :)
     */
    public static void update() {
        /**
         * make sure that there is data in the boxes taht is valid
         */
        if (pullDelayArea.getText().equals("Price Pull Delay In Minutes...") || pullDelayArea.getText().equals("")) {
            new MessageDialog(d, "Please enter a valid number into Price Pull Delay.");
        }
        if (priceValueArea.getText().equals("Price You Are Waiting For...") || priceValueArea.getText().equals("")) {
            new MessageDialog(d, "Please enter a valid number into Price Value.");
        }

        // set the new price double from the website

        // update the label
        currentPriceLabel.setText("Current Price: $" + currentPrice);
        currentPriceLabel.setBounds(((WIDTH / 2) - (currentPriceLabel.toString().length() / 5)), 5, WIDTH, 50);
        currentPriceLabel.repaint();

        /**
         * check to see if the current value is what the client is waiting for
         */
        String priceValueString = priceValueArea.getText();
        double priceValue = Double.parseDouble(priceValueString);

    }

    /**
     * this thread is checking the price of the Bitcoin, and will send out the
     * notification if it reaches a certain price
     */
    public void run() {
        System.out.println("started");
        /**
         * initial pause, letting the client get all set up
         */
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            new ErrorDialog(this, "Error delaying the pull request. Please restart client.");
        }
        System.out.println("started");
        while (running) {
            update();

            /**
             * add a pause to not just destroy internet speeds, pause settable
             * through the GUI
             */
            try {
                Thread.sleep((long) (pauseLength * 1000000)); // 1 million cause
                                                                // its in
                // milliseconds
            } catch (InterruptedException e) {
                new ErrorDialog(this, "Error delaying the pull request. Please restart client.");
            }
        }
    }

    public static void main(String[] args) {
        d = new Display();
    }
}
任何帮助都将不胜感激


PS:我的internet即将关闭,因此响应可能会延迟到明天。

简短的版本是在构造函数退出之前不会设置对
d
的引用,因此在
显示中,您正在创建一个新的线程对象,并将静态引用传递给
d
,目前在那一点上仍然是空的。我会从构造函数中删除
新线程(d)
行和
.start()
行,然后从main方法中简单地调用
d.start()

这是一个非常糟糕的静态用法,它可能会咬到你的尾巴。将d赋值给一个新的显示对象,然后在显示构造函数中使用相同的变量?这让我头晕目眩。你不应该这样做,在构建对象之前使用它。好吧,我不太清楚你的意思。。。。哈哈。还有,这可能就是我的线程没有启动的原因吗?因为这是我的问题……首先也是最重要的——去掉你正在使用的每个静态变量。每个人。然后修复你的代码,这样你就不需要静态变量了。不要提一个违反Swing:PFine单线程规则的好例子,但是不要在轮询中拉入JTextField文本。相反,让ActionListener设置线程或计时器内部使用的字符串字段。。。好吧,谢谢XD,我不会想到的我能接受的时候就接受