我有一个方法:Java:为什么在线程内不调用方法?
public String getPresentValue(ObjectIdentifier oid) throws Exception {
ReadPropertyRequest rpr = new ReadPropertyRequest(oid, PropertyIdentifier.presentValue);
ReadPropertyAck rpa = (ReadPropertyAck) localDevice.send(isa, null, 1476, Segmentation.segmentedBoth, rpr);
return rpa.getValue().toString();
}
,当我把它叫做线程之外完美的作品,但是当我尝试调用它的线程内,没有任何反应。
为什么会发生这种情况?
更新:
public void active(Supervisory supervisory) {
//.. my code above is just simple maths
System.out.println("presentValue in the thread" + getPresentValue(oi));
ScanAO scanAO = new ScanAO();
Thread threadAO = new Thread(scanAO);
threadAO.start();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
class ScanAO extends Thread {
public void run() {
try {
for (ObjectIdentifier oi : oisAO) {
System.out.println("presentValue in the thread" + getPresentValue(oi));
}
} catch (Exception e) {
}
}
}
最好的问候, 瓦尔特·恩里克。
通过调用线程中的方法是什么意思?我的猜测是这种方法从来没有被调用过。 – toto2 2011-06-05 18:17:43
你能提供更多的代码吗?答案几乎肯定会是你的线程没有运行......我们只需要弄清楚为什么就是这样。 – stevevls 2011-06-05 18:18:40
*所有*代码在一个线程中执行。你不能在线程之外调用一个方法。如果你可以展示一个简短但完整的程序来演示这个问题,那将会使生活变得相当容易...... – 2011-06-05 18:18:53