我有一個方法: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