이 코드를 이전 개발자 (lol)로부터 상속 받았다. 청취자 종류의 콜백을 사용하는 대신 조인을 지원하도록 변경하는 것을 고려하고 있습니다.청취자 대 참여자가있는 싱글 톤
내 요구 사항 : 1. DoMath 클래스 스레드가 완료 될 때까지 호출 스레드를 대기시켜야합니다. 2. 다른 스레드가 호출하지 못하도록해야합니다. 다른 스레드에서
이
(수업) - :이 호출 할 때DoMath.getInstance().performMathCalc();
그것은 기다리거나 물론 잠을하지 않습니다 그래서
public class DoMath {
protected math calc() {
}
public static DoMath getInstance() {
if(_instance == null) {
_instance = new DoMath();
}
return _instance;
}
// perform a synchronous math calc, and return a boolean indicating success or failure.
public boolean performMathCalc() {
MathEngine.setApplicationMode(MathEngine.AUTO);
MathEngine.getInstance().StartMathCalc(MathEngine.DIVISION);
return true;
}
// perform an async math calc, and call back the listener when done
public void performMathCalc(final listener client) {
Thread mathThread = new Thread(new Runnable() {
public void run() {
boolean result = performMathCalc();
client.mathFinished(result);
}
});
mathThread.setDaemon(true);
mathThread.start();
}
public static interface listener {
public void mathFinished(boolean success);
}
protected static DoMath _instance;
}
을, 그냥를하는 것이 좋습니다 리스너를 사용하거나 호출 클래스에서 조인을 구현할 수 있습니까?
메인 스레드를 대기, 절전, 가입 또는 차단하지 않습니다. 이제까지. – njzk2
@ njzk2 "항상"을 "비동기 *로 광고되는 작업을 수행 할 때"로 변경하면 좋은 조언이됩니다 (이 경우에는 확실히 적용됩니다). –
aysnc 코드에는 버그가있을 수 있습니다. 리스너는 performMathCalc를 호출 한 스레드에서 호출되지 않고 대신 백그라운드 스레드에서 호출됩니다. – cyngus