2013-10-09 4 views
0

스레드 응용 프로그램을 만들고 있는데, 메인 클래스도 실행 가능한 클래스입니다. 어떤 이유로 든 내 스레드가 시작되지 않습니다. 왜 어떤 생각? 어떤 도움을 크게 감상 할 수왜 내 스레드가 시작되지 않습니까?

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(); 
    } 
} 

: updateThread 여기

코드입니다 ... 범인입니다.

추신 : 내 인터넷이 꺼져있어 내일까지 응답이 지연 될 수 있습니다.

+2

이것은 통계학의 끔찍한 사용이며 꼬리에 물린 것일 수 있습니다. d를 새 Display 객체에 할당 한 다음 동일한 변수를 Display 생성자 내부에서 사용하고 있습니까? 이것은 나의 머리를 회전시킨다. 당신은 건설 전에 물건을 사용하여 이것을해서는 안됩니다. –

+0

좋아, 너 무슨 뜻인지 모르겠다. 롤. 또한, 왜 내 스레드가 시작되지 않을까요? 그게 내 문제 야. – PulsePanda

+3

맨 먼저 - 사용하고있는 모든 ** 정적 변수를 제거하십시오. 모두들. 그런 다음 정적 변수가 필요하지 않도록 코드를 수정하십시오. –

답변

1

짧은 버전 d에 대한 참조가 당신의 Display, 당신은 여전히 ​​그 시점에서 현재 null의 d에 정적 참조를 새 스레드 객체를 생성하고 전달하는에, 그래서 생성자가 종료 될 때까지 설정되지 않는 것입니다 . new Thread(d) 줄과 .start() 줄을 생성자에서 제거하고 기본 메서드에서 d.start()을 호출하기 만하면됩니다.

+1

나는 문제를 본다. .. 당신에게 감사 할 것 XD 나는 그것을 생각하지 않고 있었다. ... :) 내가 할 수있을 때 나는 이것을 받아 들일 것이다. – PulsePanda

관련 문제