2012-02-23 31 views
-1

我不知道這是否是最好的方法,這就是爲什麼我問你的幫助傢伙。Java:如何在線程中運行進程並讀取它的值?

這是我的問題,我開發一個應用程序,你必須在「讀」按鈕,當用戶打這個按鈕,然後程序開始閱讀一些價值觀和我的數據庫中保存此值,好嗎?

所以我認爲,當用戶點擊「讀」我啓動一個線程,這是因爲如果他想要,沒有應用程式被凍結的用戶可以做的另一件事。 但我無法訪問此線程讀取的值。

還有另一種方法可以做到這一點?

編輯:

private void jtb_readerItemStateChanged(java.awt.event.ItemEvent evt) {            
     // getting some values provided by the user  

     if (buttonReaderState()){ 
      if (supervisory == null) 
       supervisory = new Supervisory(ip, broadcast, deviceID); 
       supervisory.start(); 
     } 
    }           

// Supervisory class 
    public void start(){ 
     Scan scan = new Scan(); 
     Thread t = new Thread(scan); 
     t.start(); 
     threadState = true; 
    } 

    class Scan extends Thread{ 
     public void run(){ 
      // inside the tread I have to initiate another 'supervisory' object, is that right ? 
      Supervisory s = new Supervisory(ip, broadcast, deviceID); 
      while (threadState){ 
       try { 
        s.active(); 

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

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

    public void active() throws IOException, Exception { 
     // getting this values from my hardware like temperature and anothers things. 

     for (int i = 0; i < ois.size(); i++) { 
      ObjectIdentifier oi = ois.get(i); 

      //analog input 
      if (i == 1) 
       aI = Float.parseFloat(getPresentValue(localDevice, oi)); 
      //analog output 
      if (i == 2) 
       aO = Float.parseFloat(getPresentValue(localDevice, oi)); 
      //binary input 
      if (i == 3) 
       bI = getBinaryValue(getPresentValue(localDevice, oi)); 
      //binary output 
      if (i == 4) 
       bO = getBinaryValue(getPresentValue(localDevice, oi)); 
     } 

    } 

閱讀本值後,我想,以顯示我正在創建界面這個值,但似乎我不能訪問這些值(AI,AO,BI ,BO)。

+1

我不明白這個問題。 「無法訪問此線程讀取的值」是什麼意思? – m0skit0 2012-02-23 11:18:17

+0

@ m0skit0我更新我的帖子,希望我現在更清楚;) – 2012-02-23 11:29:42

回答

2

將參考傳遞給您擁有的接口。例如。您可以將一個JFrame owner字段添加到Supervisory類並將您的值傳遞給那裏。

+0

它的工作原理! 謝謝。 – 2012-02-29 19:28:06

相關問題