fixed redundant thread creation

This commit is contained in:
Sunmisc
2022-10-05 15:49:25 +03:00
parent 98bbdf872b
commit b9a55799fe
2 changed files with 25 additions and 22 deletions
@@ -15,24 +15,32 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public final class NetCall { public final class NetCall {
private final NetRequest req; private final NetRequest req;
private final NetClient cl; private final NetClient cl;
private final Executor executor;
protected NetCall(NetClient cl, NetRequest req) { protected NetCall(NetClient cl, NetRequest req, Executor executor) {
this.req = req; this.req = req;
this.cl = cl; this.cl = cl;
this.executor = executor;
} }
private static String constructArgs(Map<String, String> args) throws UnsupportedEncodingException { private static String constructArgs(Map<String, String> args) throws UnsupportedEncodingException {
args = new TreeMap<>(args); args = new TreeMap<>(args);
String str = ""; StringBuilder str = new StringBuilder();
for (Map.Entry<String, String> en : args.entrySet()) { for (Map.Entry<String, String> en : args.entrySet()) {
if (!str.isEmpty()) str += "&"; if (str.length() > 0)
str += URLEncoder.encode(en.getKey(), "UTF-8") + "=" + URLEncoder.encode(en.getValue(), "UTF-8"); str.append("&");
str.append(URLEncoder.encode(en.getKey(), "UTF-8"))
.append("=")
.append(URLEncoder.encode(en.getValue(), "UTF-8"));
} }
return str; return str.toString();
} }
public NetRequest request() { public NetRequest request() {
@@ -77,22 +85,13 @@ public final class NetCall {
} }
public void enqueue(NetCallback cb) { public void enqueue(NetCallback cb) {
new Thread() { executor.execute(() -> {
@Override try {
public void run() { NetResponse resp = execute();
try { cb.onResponse(NetCall.this, resp);
NetResponse resp = execute(); } catch (IOException e) {
cb.onResponse(NetCall.this, resp); cb.onFailure(NetCall.this, e);
} catch (IOException e) {
cb.onFailure(NetCall.this, e);
}
try {
interrupt();
join();
} catch (Exception e) {
Log.e("NetCall", "Failed to interrupt and join thread", e);
}
} }
}.start(); });
} }
} }
@@ -2,12 +2,16 @@ package ru.vtosters.lite.net;
import java.net.PasswordAuthentication; import java.net.PasswordAuthentication;
import java.net.Proxy; import java.net.Proxy;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class NetClient { public class NetClient {
private final long timeout; private final long timeout;
private final Proxy proxy; private final Proxy proxy;
private final PasswordAuthentication authenticator; private final PasswordAuthentication authenticator;
private final Executor executor
= Executors.newSingleThreadExecutor();
protected NetClient(Builder b) { protected NetClient(Builder b) {
timeout = b.timeout; timeout = b.timeout;
@@ -24,7 +28,7 @@ public class NetClient {
} }
public NetCall newCall(NetRequest req) { public NetCall newCall(NetRequest req) {
return new NetCall(this, req); return new NetCall(this, req, executor);
} }
public PasswordAuthentication getAuthenticator() { public PasswordAuthentication getAuthenticator() {