Is there a native platform-independent solution for asynchronous tasks? For example, on a PC this is SwingWorker , on Android it is AsyncTask .

Update 0

The question was frozen due to incomprehensibility; I apologize for the curved syllable, now I will try to explain: Help, please, find a cross-platform analogue of AsyncTask Android'ovskih, so that there are methods onPreExecute , doInBackground , onPostExecute and that this analogue works equally on Windows / Linux and Android . Searched in google - found all sorts of SwingWorker , but they depend on the platform. I tried to transfer the source AsyncTask , but there is also a link to the platform because of the Handler classes and the like.

Whether this analogue of AsyncTask 'a is in the form of a library or an embedded solution does not matter.

Update 1

OK, as a result of brainstorming the task is now even more simplified. We need a cross-platform method to call a method from another thread within the UI (main) stream. That is, for example, when the program is started, the interface is loaded and some kind of animation takes place, and work with the network begins in another thread. After working with the network, you need to run the method in the UI stream, for example, to change the text on the button. How can this be correctly implemented with one code so that it works on both PCs and Android?

  • I did not quite understand the question ... do you want to write one code for an asynchronous task, and that it works on all platforms? moreover, both on PC and mobile phones? - Rishka
  • And the Future<T> and everything around it does not suit you? - VladD
  • You can write your bike with the Thread class and sync. Perhaps he is already on github. - hardsky
  • " for this analogue to work the same way " - hmm. Quite the same probably will not work. For example, if on one platform successive tasks are performed in the same thread , but not on the other, is this considered “unequal”? - VladD
  • 2
    Is it not easier for you to refuse a single solution ?? If this is not a critical requirement, make the classes work separately on the platforms you need and contact the wrapper - Toly

3 answers 3

Usually this problem is solved by the bridge pattern. A separate class is being launched - an abstract implementation of asynchronous task execution. And for each platform, its concrete implementation is got. A base class is also being made for the asynchronous task itself - the task of which will be to find the right implementation and give its descendants access to it. At the same time such a scheme is useful in the case of, for example, the transition to SWT.

Here I see two problems. The first problem is to make the code that mentions SwingWorker compile under Android (and vice versa). Probably, for this purpose, it will be necessary to allocate separate subprojects - "implementation of the bridge for Android" and "implementation of the bridge for the desktop", compiled only for those platforms that support them. Do you still need to have separate compilation chains for Android and for the desktop?

Problem number two is choosing the right implementation. Here you can give the user the right (well, or need) to select the desired implementation at startup. Or you can take advantage of the fact that on each platform there will be exactly one implementation so far - and simply place them under one name. In principle, in this case, even a pattern and a separate hierarchy are not required - but SWT is no longer included in such a scheme.

  • private static final boolean isMobile = false; // ... if (isMobile) {// this is removed at compile time} stackoverflow.com/questions/1813853/ifdef-ifndef-in-java - DeKaNszn
  • @DeKaNszn, maybe the device is better to check so ? - Helisia
  • @SuperCreeper in your case the compiler will always leave only else. This "hack" works on the fact that the compiler throws out those pieces of code that the control will never fall into - DeKaNszn

Look as an example towards rxJava in conjunction with rxAndroid.
I will explain on a piece of code.

 final Subscription subscription = createObservable() //создали Observable .subscribeOn(Schedulers.newThread()) //тут указывается поток, в котором буду выполены основние действия .observeOn(AndroidSchedulers.mainThread()) //тут указывается поток, в котором будет выполнена обраотка результата. аналог onPostExecute() .subscribe(subscriber); //обработчик результата. в данном случае выполнится в основном потоке(UI) 

The example uses the class, clearly sharpened by android.
What is it for? I think that one code in this case will not be able to write completely platform-independent. But to reduce labor costs, I think you can. Those. you write the implementation for processing in the main thread separately for each platform and remove it from the kernel. Not many platforms.
I think the rxJava library is made for such purposes, and there is just no platform-independent handler for the UI stream.

If I am mistaken, I’ll read with pleasure a refutation. The topic is interesting.

    Unfortunately, I write in Java only for android and I don’t know how it works on other platforms.

    1. The most obvious solution is to write a wrapper class that will call AsyncTask in android, and SwingWorker for PC, since the functionality is the same and, as far as I can tell, it should work the same way. Than you do not triple this option?

    2. In theory, nothing prevents implement the same class in the manual. You take a clean class, you write a function onPreExecute (), which you call immediately after execute (); , then you create a new thread from which you call doInBackground () of your class. onPostExecute () and onProgress () you call in the UI. In theory should work. Implement in practice, unfortunately, no time.