7

I'm curious to know if there's something similar to the Task Parallel Library from C# in Java and/or the Android SDK. Coming from a C# background, we're taught that making a new thread is a relatively heavyweight operation, and are instructed to use the threadpool, or more recently, Tasks.

So in my mind, the level of abstraction that Tasks bring would be ideal ... is there anything like that, or even the threadpool? or does it all just involve making a new Thread or making my own threadpool

Community
  • 1
  • 1
Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
  • Regarding the "heavyweight operation" statement, see http://stackoverflow.com/questions/2117072/java-thread-creation-overhead – skaffman Jun 30 '11 at 20:00
  • Seeing that this question is 3 years old - anyone have an update on the state of the art today? Are we still using Executors and the Java 5 Concurrency stuff? Or have there come new implementations like stuff in Java 8 or RxJava which solves this better? – Nilzor Oct 29 '14 at 15:32

6 Answers6

3

Sure it does. You can read more about it here: Executors

Also, you could overview the whole concurrency topic at the same page: Concurrency

bezmax
  • 25,562
  • 10
  • 53
  • 84
  • Unless I'm missing something, this does not provide continuation support. Not saying you couldn't implement it on top of this, but I suspect it would be pretty difficult to get right and would be pretty nasty syntactically too. – Kent Boogaart May 10 '13 at 12:15
  • @KentBoogaart Yes, Java does not provide anything similar to continuation tasks, you have to code it all by yourself. Personally, I see continuation tasks a bad touch, but if you are into it, you could look into Lightwolf framework: http://lightwolf.sourceforge.net/index.html – bezmax May 10 '13 at 12:51
  • Been reading more. Seems like I could use the fork/join stuff to achieve something similar in nature, though not sure how elegant/maintainable it would be. – Kent Boogaart May 10 '13 at 12:52
  • I'd only be interested in implementing an asynchronous workflow if the code is still maintainable. TPL in .NET allows this (especially in C# 5 with `async`/`await` or even more naturally in F#) but it seems as though it would be messy in Java, and probably not worth the burden in my case. – Kent Boogaart May 10 '13 at 12:56
  • @KentBoogaart Hm, I think you could also look into JBPM. It's an open-source process modeling engine which allows you to do exactly what you need. The only problem is, it may become smashing a nail with a microscope problem - JBPM is designed for managing business processes and contains lots of features you will never need. Also it has quite a learning curve. In essence, you define a set of tasks in it in some process, attach some Java code for each "task", push/pull results of calculations into your context, and run it all to see results. – bezmax May 10 '13 at 13:02
  • Executors are .NET Tasks in the way Carob is Chocolate. – Tony B Jun 26 '19 at 03:04
1

See http://developer.android.com/reference/java/util/concurrent/package-summary.html for threadpool info.

Femi
  • 64,273
  • 8
  • 118
  • 148
1

I think that the java.util.concurrent package has most of the classes/functionality you are looking for.

Specifically, take a look at these:

  1. ThreadPoolExecutor
  2. Executors
  3. CompletionService
nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
1

According to Java developers, Java concurrency programming should be moving towards the usage of the new Concurrency API.

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

jjmontes
  • 24,679
  • 4
  • 39
  • 51
1

Android has support for Javas concurrency library but you should look into AsyncTask which supports running operations on both on the UI thread and in the background.

Here is an 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");
Andreas Holstenson
  • 1,428
  • 9
  • 7
0

Look at the Task4Java framework at https://github.com/dtag-dbu/task4java/.

This framework was build on standard Java libraries and runs also on Android. Currently we are creating a sample application to show the power of this framework.