Calling wait() in AsyncTask causes IllegalMonitorStateExcept

Put your problem here if it does not fit any of the other categories.

Calling wait() in AsyncTask causes IllegalMonitorStateExcept

Postby chousein » Sat Jun 05, 2010 9:29 pm

Hi,

I have two AsyncTasks running; let's say A1 and A2. A2 is manipulating the data produced by A1 (in fact this is a typical producer-consumer scenario). It's very tempting to use wait() inside doInBackground() implementation of A2, BUT this causes an IllegalMonitorStateException. Is it really not possible to "wait()" inside an AsyncTask? What to do when you have an AsyncTask which needs to wait for something?

Thank you in advance,

-Ali
User avatar
chousein
Developer
Developer
 
Posts: 32
Joined: Wed Feb 24, 2010 10:09 pm

Top

Re: Calling wait() in AsyncTask causes IllegalMonitorStateEx

Postby padde » Sat Jun 05, 2010 9:34 pm

Well you can use Thread.sleep()..but you should rethink your architecture.
Btw. AsyncTask is finished when onPostExecute is called... so no need to wait.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Calling wait() in AsyncTask causes IllegalMonitorStateEx

Postby chousein » Sat Jun 05, 2010 9:50 pm

Hi, thank you for the reply and the suggestion. In my case it was the best solution to have both threads run concurrently, and let A2 consume what is produced by A1 on the fly. A1 runs slower than A2, so sometimes A2 had to wait for A1. But apparently it's not possible to wait() in an AsyncTask. I'll change my architecture, but still I like the first solution better. As I wrote in another forum as well, until tonight I used to like the idea of AsyncTask. I simply don't understand why the creators of Android don't make such handy ideas more generic.
User avatar
chousein
Developer
Developer
 
Posts: 32
Joined: Wed Feb 24, 2010 10:09 pm

Re: Calling wait() in AsyncTask causes IllegalMonitorStateEx

Postby padde » Sat Jun 05, 2010 11:35 pm

AsyncTask is just a handy solution for very basic thread work and most of all for changing UI based
on thread results. What you need is beyond the scope for what AsyncTask is designed.
But you can always do your thread and handler stuff yourself.. so its just a loss of comfort.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests