Equivalent to TPL for Java / Android

I am curious to find out if there is something similar to a parallel task library from C # to Java and / or Android SDK. Based on the background of C #, we are taught that creating a new thread is a relatively difficult operation, and they are encouraged to use threadpool or, more recently, Tasks.

So, in my opinion, the level of abstraction that the Tasks brings would be ideal ... is there anything similar or even a stream? or is it all just about creating a new Thread or creating my own threadpool

+6
source share
6 answers

Of course. You can read more about this here: Executors

Alternatively, you can view the entire concurrency section on the same page: Concurrency

+3
source

I think the java.util.concurrent package contains most of the classes / functions you are looking for.

In particular, look at them:

+1
source

According to Java developers, Java concurrency programming should move towards using the new concurrency API.

Note: http://download.oracle.com/javase/1.5.0/docs/guide/concurrency/index.html

+1
source

Android supports the Javas concurrency library, but you should take a peek at AsyncTask , which supports ongoing operations both on the UI thread and in the background.

Here is a short example of a task:

private class CharCountTask extends AsyncTask<String, Integer, Long> { protected Long doInBackground(String... in) { long result = 0; for(int i=0,n=in.length; i<n; i++) { result += in[i].length(); publishProgress((int) (i / (double) count) * 100); } return result; } protected void onProgressUpdate(Integer... progress) { // update progress here updateProgressBar(progress[0]); } protected void onPostExecute(Long result) { // update the UI here setTotalChars(result); } } 

To use it:

 new CharCountTask().execute("first", "second", "third"); 
+1
source

Take a look at Framework Task4Java at https://github.com/dtag-dbu/task4java/ .

This structure was built on standard Java libraries and also works on Android. We are currently creating an example application to show the power of this structure.

0
source

Source: https://habr.com/ru/post/891735/


All Articles