如何将 AsyncTask 从 Android 移植到 Java?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11815306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:27:59  来源:igfitidea点击:

How can port AsyncTask from Android to java?

javaandroidmultithreading

提问by R4j

AsyncTaskis very useful to synchronized between UI thread and other threads in Android. So I have read its source codeand tried porting to java classic (JDK) but no success because some classes don't exist in java (Message, Handler..).
I would like to create a class with some useful functions like AsyncTask (that can synchronized between main thread and other threads) :

AsyncTask对于UI线程和Android中其他线程之间的同步非常有用。所以我阅读了它的源代码并尝试移植到 java classic (JDK) 但没有成功,因为 java (Message, Handler ..) 中不存在某些类。
我想创建一个具有一些有用功能的类,例如 AsyncTask(可以在主线程和其他线程之间同步):

    doInBackground(Params... params)
    onProgressUpdate(Progress... values)
    onPostExecute(Result result)
    publishProgress(Progress... values)
    onPreExecute()
    onCancelled()

Is there any way to try that?

有什么办法可以试试吗?

采纳答案by Kumar Vivek Mitra

1.AsyncTaskis specially developed for android to sync the UI thread and the Non-UI thread, also known as Painless threading.....

1.AsyncTask专为android开发同步UI线程和Non-UI线程,也称为Painless threading.....

2.There is an alternative of AsyncTask in Java named as SwingWorker.

2.Java 中有一个 AsyncTask 的替代方案,名为SwingWorker

See this link for a nice basic tutorial:

请参阅此链接以获取一个不错的基本教程:

http://kodejava.org/how-do-i-use-swingworker-to-perform-background-tasks/

http://kodejava.org/how-do-i-use-swingworker-to-perform-b​​ackground-tasks/

回答by Stan Kurdziel

Agreed that SwingWorkeris the most direct analog to AsyncTask

同意这SwingWorker是最直接的模拟AsyncTask

If you aren't using Swing, another option is a ThreadPoolExecutor: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html

如果您不使用 Swing,另一个选项是ThreadPoolExecutorhttp: //docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html

Here's an article demonstrating using ThreadPoolExecutorto spawn multiple background threads: http://www.javacodegeeks.com/2013/01/java-thread-pool-example-using-executors-and-threadpoolexecutor.html

这是一篇演示使用ThreadPoolExecutor生成多个后台线程的文章:http: //www.javacodegeeks.com/2013/01/java-thread-pool-example-using-executors-and-threadpoolexecutor.html

Spawning a single background thread that runs and completes withoutprogress update (which is also a common use of AsyncTaskin Android) can be as simple as:

生成一个在没有进度更新的情况下运行和完成的单个后台线程(这也是AsyncTaskAndroid 中的常见用法)可以很简单:

Executors.newSingleThreadExecutor().execute(new Runnable() {
  @Override
  public void run() {
    // do stuff in background
  }
});

回答by Raja

I use the Observer design pattern for a similar situation. After spawning a background thread, the main thread goes into sleep mode. When the background thread finishes the asynchronous task, it wakes up the main thread with a callback message. This essentially removes a block on a synchronization object that was waiting for the callback.

我在类似的情况下使用观察者设计模式。产生后台线程后,主线程进入睡眠模式。当后台线程完成异步任务时,它会通过回调消息唤醒主线程。这实质上删除了等待回调的同步对象上的块。