Android forced asynchronous conversion to synchronization is for your reference. The specific content is as follows
The Android system stipulates that time-consuming tasks need to be performed in asynchronous threads, especially network requests must be performed in asynchronous threads, otherwise they will be thrown.NetworkOnMainThreadException,However, in some special circumstances, we need to ensure that the remaining operations are performed after obtaining the network request result. At this time, we need to perform related operations in UiThread.
This requires the principle of thread blocking, which can be achieved by using Callable or FutureTask.
public static String getSyncBusiness(final String url){ try { FutureTask<String> task = new FutureTask<String>(new Callable<String>() { @Override public String call() throws Exception { URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) (); (true); ("GET"); (); InputStream in = (); BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf8")); final StringBuilder sb = new StringBuilder(); String line = null; while((line = ())!=null){ (line); } return (); } }); new Thread(task).start(); return (); } catch (Exception e) { (); throw new RuntimeException("Network Access Error"); } }
The FutureTask interface is a generic interface that can set the type that needs to be returned. It can be seen that in order to facilitate programmers' operations, this method cleverly ensures that the method performs unified judgment by throwing a total Exception, which is similar to the Exception mechanism of RxJava. And it can be seen from the source code that the interface implements theRunnableFuture<>interface, andRunnableFutureThe interface is inherited fromRunnableThis explains why you can directly pass the Task object in new Thread.
It can be understood that FuturTask drives child threads by implementing Runnable, performs asynchronous network requests and other operations.However, no asynchronous concurrent operations were performed, but the UI thread was blocked at the same time until the child thread was executed.
In addition to the above, you can also use it directlyCallableInterface, you need to note that Callable requires a thread poolExecutorServiceOnly then can drive it.
public static String getSyncBusiness2(final String url){ try { Callable<String> callable = new Callable<String>() { @Override public String call() throws Exception { URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) (); (true); ("GET"); (); InputStream in = (); BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf8")); final StringBuilder sb = new StringBuilder(); String line = null; while((line = ())!=null){ (line); } return (); } }; ExecutorService exec = (1); Future<String> task = (callable); return (); } catch (Exception e) { (); throw new RuntimeException("Network Access Error"); } }
Since the principle is ultimately blocking the UI thread, it is not recommended to use it unless it is specifically needed. If the blocking time is too long, ANR will also be triggered.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.