Je suis nouveau dans le threading, ceci est mon code dans la méthode principale J'ai un thread qui traite une valeur puis je mets la valeur à la méthode getResult(). maintenant je suis essayer d'obtenir cette valeur, mais je reçois nullethread java, valeur de retour à partir du thread
RS232Example rs232= new RS232Example();
rs232.main()
System.out.println("value after RS232::"+rs232.getResult())
résultat est
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())
En fait, l'objet appelle d'abord la méthode principale, puis getResult mais comme la méthode principale a un thread qui définit vale pour getReult, il faut un certain temps avant que jvm appelle getResult et imprime la valeur null. Je veux d'abord que la méthode principale se termine. ou de toute façon dont je retourne la valeur de ma méthode de course
pouvez-vous s'il vous plaît montrer ** code pertinent ** –