2011-03-22 79 views
0

我是线程新手,这是我在主要方法中的代码,我有一个线程处理一些值,然后我将值设置为getResult()方法。现在我试图获取的价值,但我得到空java线程,线程返回值

RS232Example rs232= new RS232Example(); 
rs232.main() 

System.out.println("value after RS232::"+rs232.getResult()) 

结果是

value after RS232::null 
call 
call 
call 
call 
call 
call 
call 
call 
call 
call 
call 
0 
:: 0 
::::?? 0 
call 




public class RS232Example implements rs232Weight{ 

    private String threadResult; 

    public void Result(String result) { 
     threadResult = result; 

    } 
    public String getResult() { 
      return threadResult; 
    } 

     public synchronized void connect(String portName) throws Exception { 

      CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); 
      SerialPort serialPort=null; 
      if (!portIdentifier.isCurrentlyOwned()) { 
      serialPort = (SerialPort) portIdentifier.open("RS232Example", 2000); 

       // setup connection parameters 
       // set the parameter for machine 

       serialPort.setSerialPortParams(
         9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN); 
       // setup serial port writer 
       CommPortSender.setWriterStream(serialPort.getOutputStream()); 

      // setup serial port reader 
      CommPortReceiver obj = new CommPortReceiver(serialPort.getInputStream(),serialPort); 

      obj.start(); 

      } else { 
       // points who owns the port and connection timeout 
      System.out.println("Port in use!"); 
       try{ 
        portIdentifier=null; 
       } 
       catch(Exception e){ 
        System.out.println("error"+e); 
       } 
      } 
     } 

    public void main() throws Exception{ 

      // connects to the port which name (e.g. COM1) is in the first argument 
     connect("COM1"); 

      // send HELO message through serial port using protocol implementation 
      CommPortSender.send(new ProtocolImpl().getMessage("HELO")); 
     } 
    } 

==============

package com.indivar.cmcs.machine; 
import gnu.io.SerialPort; 
import java.io.IOException; 
import java.io.InputStream; 

    public class CommPortReceiver extends Thread{ 
    // public rs232Weight weightRs232; 
    // RS232Example rs232= new RS232Example(); 
     SerialPort serialPort=null; 
     String value; 
     InputStream in; 
     Protocol protocol = new ProtocolImpl(); 
     rs232Weight weightRs232= new RS232Example(); 
     private volatile boolean stopRequested=false; 
     public CommPortReceiver(InputStream in,SerialPort serialPort) { 
      this.in = in; 
      this.serialPort=serialPort; 

     } 
     int i=10; 
     public void stopRequest() { 
      stopRequested = true; 
      serialPort.close(); 
      } 
     public void run() { 
      try { 
       int b; 
      // System.out.println("f"); 
      while(!stopRequested) { 

        // if stream is not bound in.read() method returns -1 
        while((b = in.read()) != -1) { 
        String val=protocol.onReceive((byte) b); 

         if (val.equals("0")){ 
          // System.out.println("::::??"+val); 
         } 
         else{ 
          value=val; 
         //.setWeight(value); 
          System.out.println("::::??"+val); 
         stopRequest(); 
          weightRs232.Result(val); 
        break; 
         } 
         Thread.sleep(100); 
        //  
        } 

        protocol.onStreamClosed(); 

        // wait 10ms when stream is broken and check again 
       i--; 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
} 


================== 


RS232Example rs232= new RS232Example(); 
     rs232.main() 

     System.out.println("value after RS232::"+rs232.getResult()) 

实际上,该对象调用第一个主要方法,然后getResult,但作为主要方法有一个线程,为getReult设置VAL值,它需要一段时间之前,该jvm调用getResult方法和打印空值我希望首先完成主方法,然后调用getResult方法。或任何方式,我从我的运行方法返回价值

+1

可以请你告诉**相关代码** – 2011-03-22 05:45:16

回答

0

请你详细说明你的问题。它没有清楚地知道你有什么问题。我认为,你没有正确设定价值。由于getResult()返回一个“null”对象。让我们知道你正在做什么处理来获得价值。

+0

实际上对象调用第一主方法,然后的getResult但 – user670600 2011-03-22 07:35:46

1

您的等级RS232Example有一个成员字段String threadResult这是getResult returend。然而,这个字段只写入方法Result(顺便说一句,用大写字母开始方法名不是一个好主意),但是这个方法本身在任何代码中都不会被调用。

+0

您好,感谢乌拉圭回合的答复和建议,但我所说的功法weightRs232.Result(VAL); – user670600 2011-03-22 07:31:32

+0

但我看不到你等待你的线程完成。 – Howard 2011-03-22 07:36:00

+0

是的,我是新来的线程,你能帮我怎么做 – user670600 2011-03-22 07:52:34

0

看起来您在设置结果后,您正在访问它。即您不等待结果被设置。

我建议你考虑使用一个Callable < String>和一个ExecutorService。这将允许您使用内置库等待结果。

另一种选择是不要在后台执行此操作。你看起来并不能从多个线程中受益,它似乎只是增加了复杂性。