Java – why does matlab control interrupt the calling thread when the agent is created?

I let my matlab control thread interrupt and found that it was interrupted the first time it ran

This is because there is an interrupt code inside getproxyrequestcallback:

private static class GetProxyRequestCallback implements RequestCallback
{
    private final Thread _requestingThread;
    private volatile MatlabProxy _proxy;

    public GetProxyRequestCallback()
    {
        _requestingThread = Thread.currentThread();
    }

    @Override
    public void proxyCreated(MatlabProxy proxy)
    {
        _proxy = proxy;

        _requestingThread.interrupt();
    }

    public MatlabProxy getProxy()
    {
        return _proxy;
    }
}

Is there any reason to interrupt the calling thread or is this just an error?

Solution

RemoteMatlabProxyFactory. The getproxy () method creates a getproxyrequestcallback instance, then sleeps and waits for the proxycreated (...) method to be called Therefore, if proxycreated() does not interrupt the thread that originally created the request, the thread will wait until the timeout is reached

In my opinion, this is a defect in the MATLAB Control Library: thread Interrupt () should not be abused for this purpose because the interrupted thread may have different reasons and should not be used for anything other than the signal that the thread should stop

This should fix the problem in the MATLAB control library by waiting for mutexes

For example:

class RemoteMatlabProxyFactory implements ProxyFactory {
    // [...]

    @Override
    public MatlabProxy getProxy() throws MatlabConnectionException {
        GetProxyRequestCallback callback = new GetProxyRequestCallback();
        Request request = this.requestProxy(callback);
        return callback.getProxy(_options.getProxyTimeout());
    }

    // [...]
}

private static class GetProxyRequestCallback implements RequestCallback {
    private final Object _lock = new Object();
    private MatlabProxy _proxy;

    @Override
    public void proxyCreated(MatlabProxy proxy) {
        _proxy = proxy;

        _requestingThread.interrupt();
    }

    public MatlabProxy getProxy(long timeout) throws MatlabConnectionException {
        synchronized (_lock) {
            if (_proxy != null) {
                return _proxy;
            }
            try {
                _lock.wait(timeout);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new MatlabConnectionException("Thread was interrupted while waiting for MATLAB proxy",e);
            }
            if (_proxy == null) {
                throw new MatlabConnectionException("MATLAB proxy Could not be created in " + timeout + " milliseconds");
            }
            return _proxy;
        }
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>