如何使用后台/异步任务生成线性/可读性更强的Java代码
目前我有一些类似的东西如何使用后台/异步任务生成线性/可读性更强的Java代码,java,android,android-asynctask,Java,Android,Android Asynctask,目前我有一些类似的东西 public class SomeActivity { void onCreate() { doSomething(); if (weNeedToDoSomeAsncTask()) { new SomeAsyncTask(afterSomeAsyncTask).execute(); } else { new DifferentAsyncTask(afterAnotherAsyncTask).execute(); } } PostExec
public class SomeActivity {
void onCreate() {
doSomething();
if (weNeedToDoSomeAsncTask()) {
new SomeAsyncTask(afterSomeAsyncTask).execute();
} else {
new DifferentAsyncTask(afterAnotherAsyncTask).execute();
}
}
PostExecute afterSomeAsyncTask = new PostExecute() {
void Callback(String response) {
doSomethingElse(response);
new DifferentAsyncTask(afterAnotherAsyncTask).execute();
}
}
}
从表面上看,这并不太糟糕,但我已经极大地简化了代码,您可以想象,当您有多个调用正在进行,并且一些分支开始发生时,代码会变得多么复杂
为了帮助理解上下文,实际用例通常是:
public class SomeActivity {
void onCreate() {
doSomething();
if (weNeedToDoSomeAsncTask()) {
new SomeAsyncTask(afterSomeAsyncTask).execute();
} else {
new DifferentAsyncTask(afterAnotherAsyncTask).execute();
}
}
PostExecute afterSomeAsyncTask = new PostExecute() {
void Callback(String response) {
doSomethingElse(response);
new DifferentAsyncTask(afterAnotherAsyncTask).execute();
}
}
}
- 使用sdk获取一些设备/用户信息
- 使用返回的信息对web服务进行身份验证
- 向web服务发送一些日志信息
- 从web服务获取一些数据
- 从web服务获取一些其他数据
- 使用该数据生成UI
public class SomeActivity {
void onCreate() {
doSomething();
if (weNeedToDoSomeAsncTask()) {
new SomeAsyncTask().execute();
doSomethingElse(response);
}
new DifferentAsyncTask().execute();
afterAnotherAsyncTask();
}
}
NetworkService networkService = new NetworkService();
Needle.onBackgroundThread().serially().execute(networkService);
String result = doSomethingWithResponse(networkService.response);
AnotherNetworkService anotherNetworkService = new AnotherNetworkService(result);
Needle.onBackgroundThread().serially().execute(anotherNetworkService);
我过去曾尝试过使用.get()
,但遇到了一些问题,但从未弄清原因(我可能试图在另一个异步任务中调用一个,我读到这是不可能的,但那是很久以前的事了,所以无法确定)。虽然我意识到.get
在某种程度上违背了AsyncTask
的目标,但我之所以使用它,只是因为Java迫使我这样做(用于网络通信等)。我的应用程序依赖于结果继续,在返回之前无法执行任何其他操作
我应该再次尝试.get()
技术吗?还是真的是个坏主意
我是否应该接受它并意识到代码更难阅读,我需要跳转到回调方法来尝试和理解代码流
还是有不同的选择
我还刚刚听说了Nineder,它看起来是一个相当不错的图书馆,我想我可以做得更多
public class SomeActivity {
void onCreate() {
doSomething();
if (weNeedToDoSomeAsncTask()) {
new SomeAsyncTask().execute();
doSomethingElse(response);
}
new DifferentAsyncTask().execute();
afterAnotherAsyncTask();
}
}
NetworkService networkService = new NetworkService();
Needle.onBackgroundThread().serially().execute(networkService);
String result = doSomethingWithResponse(networkService.response);
AnotherNetworkService anotherNetworkService = new AnotherNetworkService(result);
Needle.onBackgroundThread().serially().execute(anotherNetworkService);
但这可能与AsyncTask.get()
方法完全相同?TL;任何答案的答案都将是:科特林合作计划。TL;任何答案的答案都是:科特林合作计划。看看你能得到什么:
ExecutorService executor=Executors.newScheduledThreadPool(8);
安排期货的方法有很多
您的第一个示例可能如下所示:
public class SomeActivity {
void onCreate() {
doSomething();
if (weNeedToDoSomeAsncTask()) {
executor.submit(() -> /* async task */);
doSomethingElse(response);
}
executor.submit(() -> /* different async task */)
afterAnotherAsyncTask();
}
}
(尽管您需要查看块/连接替代方案的具体方法。)查看您可以获得的方法,如:
ExecutorService executor=Executors.newScheduledThreadPool(8);
安排期货的方法有很多
您的第一个示例可能如下所示:
public class SomeActivity {
void onCreate() {
doSomething();
if (weNeedToDoSomeAsncTask()) {
executor.submit(() -> /* async task */);
doSomethingElse(response);
}
executor.submit(() -> /* different async task */)
afterAnotherAsyncTask();
}
}
(尽管您需要查看块/连接替代方案的具体方法。)