Threads and animation

1 view
Skip to first unread message

mk

unread,
Jan 23, 2008, 5:18:46 PM1/23/08
to WHERE GPS Developers
I'm trying to do some animation while
the user waits for a response to a query.
I have one thread that changes an <img> every
second, and another that does a form.fetch() every
three seconds, and then moves to new page when it gets
the response it wants. All the images are preloaded.

Sometimes the animation gets bumpy, i.e.
there's no image change for a few seconds,
then we suddenly flip through a few iterations.

My guess is that threads in Jin actually block one
another, so the animation thread isn't fired until the
form.fetch() returns. Is that correct? If not,
what do you think is going on?

Any workarounds?

Thanks,
MK

Gil Cunningham

unread,
Jan 23, 2008, 5:51:26 PM1/23/08
to WHERE GPS Developers
mk-
It sounds like your are doing the right thing on the animation side by
preloading the images ... http://where.com/create/reference/globalfunctions.php#loadImage.

What are you doing to redraw ? document.redraw() ?

The fetch() method is synchronous. So you are correct, it is
blocking. That said, animation and networking are probably the 2
biggest CPU intensive activities for the device.

You can try placing the fetch() into another thread like so (I have
debugged any of this, but hopefully you get the idea) ...

<script>
var animThread;
var fetchThread;

function doTheFetch() {

animThread = new Thread('doAnimation()', 1000);
animThread.start();

fetchThread = new Thread('doRealFetch()', 10000);
fetchThread.start();
}

function doAnimation() {
// same logic u have
}

// threaded now to run once
function doRealFetch() {
// I have never tried killing thread here, but it should be fine
fetchThread.stop();

var res = document.someForm.fetch();

// perform your response logic here

}

Let me know how that works. Also try messing around with your sleep
time for animation.

mk

unread,
Jan 23, 2008, 6:10:41 PM1/23/08
to WHERE GPS Developers
I did put them in separate threads; indeed my code looks just like
yours, and BTW killing a thread as soon as it's called does
seem to work just fine.

And yes, form.fetch() is synchronous. But
one rationale for threads is that a thread can
block while its siblings can still run. Is that the case here? Will
animThread get to run while fetchThread is waiting
for the fetch to return?

Thanks,
MK

On Jan 23, 5:51 pm, Gil Cunningham <gilcunning...@gmail.com> wrote:
> mk-
> It sounds like your are doing the right thing on the animation side by
> preloading the images ...http://where.com/create/reference/globalfunctions.php#loadImage.

Gil Cunningham

unread,
Jan 23, 2008, 6:24:11 PM1/23/08
to WHERE GPS Developers
MK-
We are ensuring that only a single thread executes at a time... yes.
A thread, will queue itself to be executed, run, and then goto sleep.
The problem here is 2 or more threads accessing a script object or
performing some DOM manipulation which leaves another thread in an
unknown state. This could be the symptom you are seeing when
performing fetch() as its "blocking" (threaded or not) for some period
of time.

Sorry about this, it would be nice to have some animation while
waiting. I agree. Most likey will be adding an animation tag in the
future so you can avoid having to create the thread etc.
> > > MK- Hide quoted text -
>
> - Show quoted text -

mk

unread,
Jan 23, 2008, 6:32:18 PM1/23/08
to WHERE GPS Developers
Got it.

Is there any reason to use more than one thread here, since
one will effectively have to wait for the other anyway, and the
reference docs seem to advise caution when using
multiple threads?

Thanks,
MK

Gil Cunningham

unread,
Jan 23, 2008, 6:46:23 PM1/23/08
to WHERE GPS Developers
I think using multiple thread isnt gonna help you here.

I hadnt thought about this previously, but you could try something
like this ...

<script>
var doAnimation = false;

function doFetch() {
doAnimation = true;
doAnimation();

var res = form.fetch();

// response logic here

// turn off animation
doAnimation = false;
}

function doAnimation() {
while (doAnimation) {
// animation logic here

sleep(1000);
}
}

... in this case you are letting the JVM figure out the threading.
> > > - Show quoted text -- Hide quoted text -

Gil Cunningham

unread,
Jan 23, 2008, 6:46:53 PM1/23/08
to WHERE GPS Developers
In case you hadnt seen it ...

http://where.com/create/reference/globalfunctions.php#sleep
> > > - Show quoted text -- Hide quoted text -

mk

unread,
Jan 23, 2008, 7:53:51 PM1/23/08
to WHERE GPS Developers
The sleep() stuff is very cool.

Can't quite get it to work here, though,
because what we need isi for the fetch() call to
yield control while it's waiting for a response,
not for the animation loop to yield control.

Thanks for all your help,
MK

Gil Cunningham

unread,
Jan 23, 2008, 8:48:25 PM1/23/08
to WHERE GPS Developers
MK-
Sorry that didnt work. I understand the problem here though. Good
news is Im working on next version and along with alot of other
improvements, I will add this to the list. No guarantees though,
network and animation will always battle each other.
Reply all
Reply to author
Forward
0 new messages