我有一个需要从服务器请求数据的应用程序,有时是一次性请求,其他时候它需要以固定的速率轮询。
对于一次关闭的请求,我只使用如下线程:
public void request() {
Thread requestThread = new Thread(this);
requestThread.start();
}
这些没问题,它们执行后就完成了。
但是,对于长时间的轮询任务,我似乎永远无法一次只执行其中之一。
我想要的是以下方法:
public void longPoll() {
try {
pollingScheduledExecutor.scheduleAtFixedRate(this, 0, 3, TimeUnit.SECONDS);
} catch (Exception ignored) {
}
}
将永远只能存在一次,但是现在发生的是,当我从第一个类调用它时,它通常开始,但是在第二个任务调用后它永远不会停止。
如何强制执行此ScheduledExecutorService
仅一个全局实例?
对于上下文,这是这些方法所在的类:
public abstract class AbstractCommunicationChannel implements Runnable {
static String SERVER_ADDRESS = "http://0.0.0.0";
private URL url;
private JSONObject requestObject;
private JSONObject responseObject;
private volatile ScheduledExecutorService pollingScheduledExecutor = Executors.newSingleThreadScheduledExecutor();
public void longPoll() {
try {
pollingScheduledExecutor.scheduleAtFixedRate(this, 0, 3, TimeUnit.SECONDS);
} catch (Exception ignored) {
}
}
public void request() {
Thread requestThread = new Thread(this);
requestThread.start();
}
AbstractCommunicationChannel(URL url, JSONObject requestObject) {
this.url = url;
this.requestObject = requestObject;
}
/**
* This is the general purpose tool for hitting the server and getting a response back.
*/
public void run() {
Log.i("requestObject", requestObject.toString());
try {
HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpUrlConnection.getOutputStream());
outputStreamWriter.write(requestObject.toString());
outputStreamWriter.flush();
outputStreamWriter.close();
/* * */
InputStream inputStream = httpUrlConnection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int result = bufferedInputStream.read();
while (result != -1) {
byteArrayOutputStream.write((byte) result);
result = bufferedInputStream.read();
}
responseObject = new JSONObject(byteArrayOutputStream.toString("UTF-8"));
httpUrlConnection.disconnect();
} catch (Exception ignored) {
}
processResponse(responseObject);
}
protected abstract void processResponse(JSONObject responseObject);
}
答案 0 :(得分:1)
又是我。
如果只需要一个正在运行的轮询任务,则必须取消前一个任务(如果已存在)。
示例:
private static final Object lock = new Object();
private static ScheduledFuture<?> currentRunningTask;
public void longPoll() {
synchronized (lock) {
if (currentRunningTask != null) {
currentRunningTask.cancel(true);
}
try {
currentRunningTask = pollingScheduledExecutor.scheduleAtFixedRate(this, 0, 3, TimeUnit.SECONDS);
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
}