0

I am struggling with the some threading stuff in android. The situation is, that I have a table, which is getting filled during ui actions of the user.

And this table is added to a queue. I chose LinkedBlockingQueue until now. (Please correct me if this is a bad Idea).

At the same time there is a background-thread, that should fetch (read) the queue and prepare another table, which itself then is passed to a method, which should be invoked on the UI thread.
(Meaning, the background-thread only collects the queued table in background in order to pass them to another method later on ).

So I read about the various approaches to do this in some ways and found myself in the AsyncTask.

But AsyncTask only allows me pre execution or post execution as options to invoke methods/code in the UI thread.

I want to decide myself, when I invoke some methods on the UI thread , because the background-thread still has to do some work after he invoked the method in UI thread.

Ah, as information:
The background-thread never stops unless the user exits the application or a special idle timeout occurred.
And : Invoking the mentioned method on UI thread will also be parametrized.

Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45
icbytes
  • 1,831
  • 1
  • 17
  • 27
  • `But async task only allows me pre execution or post execution`. Wrong. Read the documentation for the entire class, and use publishProgress. – njzk2 Dec 17 '13 at 16:21
  • Could always use a handler. http://developer.android.com/reference/android/os/Handler.html – Submersed Dec 17 '13 at 16:27
  • Yes. But the AsyncTask is one level deeper. The parent object, which holds the reference to asynctask must be announced to execute the desired method. – icbytes Dec 17 '13 at 16:29

1 Answers1

2

But async task only allows me pre execution or post execution as options to invoke methods/code in the ui thread.

No. onProgressUpdate() also runs on the UI Thread. You run this function by calling publishProgress() in doInBackground() when the UI needs to be update while background operations are still running.

Here is an answer with an example of using it

publishProgress() Docs

Make sure to read through the AsyncTask Docs thoroughly several times. They are a bit tricky at first until you get it.

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Nice. THX. But the code, which shall be run is not in my derivate of AsyncTask. It is one level higher in the object hierarchy, so I assume, I will need some kind of interface-implementing stuff in order to do something like event handling so i can announce my parent object to execute the desired method. Right ? – icbytes Dec 17 '13 at 16:27
  • I guess I'm confused on what you are doing but you say that you get the data in `doInBackground()` and pass it to another method. This can be done with this answer. I can't say for sure beyond that since I don't know what you are doing exactly but if implementing an `interface` seems to make sense to you, then sure. – codeMagic Dec 17 '13 at 16:39
  • Let me clarify this. My object hierarchy is like that: The ParentObject deals with UI ( and also cares about UI thread). The inherited AsynTask object is a member of my ParentObject and should notify my ParentObject, that not only the UI thread shall take control over the flow, BUT also the corresponding method, which resides in ParentObject. – icbytes Dec 17 '13 at 16:47
  • Ok, I'm not sure how often you run this or what but you can [see this answer](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648) about implementing an `interface` in `AsyncTask`. That example shows calling it in `onPostExecute()` which would work if you create a new instance of the task for each instance of the `Object`. – codeMagic Dec 17 '13 at 17:13
  • Thx. I already started to implement this. Tomorrow i will test it. Ah, there will be only one parent object starting only one task. But this task will run endless. Usually. Tomorrow i will post some results. – icbytes Dec 17 '13 at 17:56