Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用新的同步线程方法_Java_Android - Fatal编程技术网

Java 如何使用新的同步线程方法

Java 如何使用新的同步线程方法,java,android,Java,Android,我有一个类似于下面的实现,它将动态创建的js返回到webview shouldInterceptRequest方法,但我需要在返回inputStream之前对其进行请求并等待其回答。我怎么能让它等这个呢 我可以准备和同步这个请求,它解决了这个问题,但我只是想知道有没有一种方法可以在新的线程中这样做 MRURLProtocol.mapDomain("gamification.fs", new MRProtocolResponseHandler() { @Override

我有一个类似于下面的实现,它将动态创建的js返回到webview shouldInterceptRequest方法,但我需要在返回inputStream之前对其进行请求并等待其回答。我怎么能让它等这个呢

我可以准备和同步这个请求,它解决了这个问题,但我只是想知道有没有一种方法可以在新的线程中这样做

MRURLProtocol.mapDomain("gamification.fs", new MRProtocolResponseHandler() {
            @Override
            public InputStream handle(Uri uri) {
             InputStream stream = null;
                myApplication.getChamp().getImpulser().invokeRequest(method, body,new OnWebApiResponseArrived() {
                    @Override
                    public void OnSuccess(Object obj) {
                        String configString = "(function () { if (typeof(window) === 'undefined') ..... %s ....";
                        configString = String.format(configString, obj);
                        stream = new ByteArrayInputStream(configString.getBytes());
                    }

                    @Override
                    public void OnFail(String errMsg) {
                        String configString = "(function () { if (typeof(window) === 'undefined')..... %s ....";
                        configString = String.format(configString, errMsg);
                        stream = new ByteArrayInputStream(configString.getBytes());
                    }
                }); 
                // NEED TO WAIT FOR stream before return

                return stream;
            }
        });
调用请求以显示它正在新线程上工作


我用倒计时锁解决了这个问题

private final CountDownLatch responseLatch = new CountDownLatch (1);

MRURLProtocol.mapDomain("gamification.fs", new MRProtocolResponseHandler() {
            @Override
            public InputStream handle(Uri uri) {
             InputStream stream = null;
                myApplication.getChamp().getImpulser().invokeRequest(method, body,new OnWebApiResponseArrived() {
                    @Override
                    public void OnSuccess(Object obj) {
                        String configString = "(function () { if (typeof(window) === 'undefined') ..... %s ....";
                        configString = String.format(configString, obj);
                        stream = new ByteArrayInputStream(configString.getBytes());

                        responseLatch.countDown();
                    }

                    @Override
                    public void OnFail(String errMsg) {
                        String configString = "(function () { if (typeof(window) === 'undefined')..... %s ....";
                        configString = String.format(configString, errMsg);
                        stream = new ByteArrayInputStream(configString.getBytes());

                        responseLatch.countDown();
                    }
                }); 

                try {
                    responseLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                return stream;
            }
        });
private final CountDownLatch responseLatch = new CountDownLatch (1);

MRURLProtocol.mapDomain("gamification.fs", new MRProtocolResponseHandler() {
            @Override
            public InputStream handle(Uri uri) {
             InputStream stream = null;
                myApplication.getChamp().getImpulser().invokeRequest(method, body,new OnWebApiResponseArrived() {
                    @Override
                    public void OnSuccess(Object obj) {
                        String configString = "(function () { if (typeof(window) === 'undefined') ..... %s ....";
                        configString = String.format(configString, obj);
                        stream = new ByteArrayInputStream(configString.getBytes());

                        responseLatch.countDown();
                    }

                    @Override
                    public void OnFail(String errMsg) {
                        String configString = "(function () { if (typeof(window) === 'undefined')..... %s ....";
                        configString = String.format(configString, errMsg);
                        stream = new ByteArrayInputStream(configString.getBytes());

                        responseLatch.countDown();
                    }
                }); 

                try {
                    responseLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                return stream;
            }
        });