Asynchronous external calling of a Runnable's method?

C

Chris Cornell

Hello,

I have a question regarding threads or runnable's in Java. I
implemented a class Foo() that implements Runnable that basically has
a method called sync() that when called loads a URL and reads the data
from it and stores it in a Hashtable that is also a private member of
the class. The class' run() method simply calls sync() once and
that's it. There are other methods that are present that extract the
data from the Hashtable and returns it.

My question is, certain external events (maybe a user event) triggers
the need to call sync() again to re-synchronize the data in the
Hashtable with the authoritative data from the webpage. Is this call
asynchronous? Meaning, am I implementing it just the same as having
my class Foo as a regular class? I'm instantiating Foo in my main
application like:

Foo f = new Foo();

new Thread(f).start();


The reason why i wanted to create Foo as a thread is so that I can
periodically just tell it re-synchronize in the background while my
busy application can keep working. If my implementation is poor, can
anyone suggest a better approach to synchronize data without blocking
the entire main execution thread? I understand that
thread-synchronization is necessary while my Foo object is
synchronizing and I can take care of that.


Thanks in advance,


Chris
 
E

Eric Sosman

Chris said:
Hello,

I have a question regarding threads or runnable's in Java. I
implemented a class Foo() that implements Runnable that basically has
a method called sync() that when called loads a URL and reads the data
from it and stores it in a Hashtable that is also a private member of
the class. The class' run() method simply calls sync() once and
that's it. There are other methods that are present that extract the
data from the Hashtable and returns it.

My question is, certain external events (maybe a user event) triggers
the need to call sync() again to re-synchronize the data in the
Hashtable with the authoritative data from the webpage. Is this call
asynchronous?

No. The thread that calls `theFoo.sync()' executes all
the code in that method and in any other methods it calls.
The next expression or statement in the caller will not execute
until sync() returns.
Meaning, am I implementing it just the same as having
my class Foo as a regular class? I'm instantiating Foo in my main
application like:

Foo f = new Foo();

new Thread(f).start();


The reason why i wanted to create Foo as a thread is so that I can
periodically just tell it re-synchronize in the background while my
busy application can keep working. If my implementation is poor, can
anyone suggest a better approach to synchronize data without blocking
the entire main execution thread? I understand that
thread-synchronization is necessary while my Foo object is
synchronizing and I can take care of that.

If you want the thread to run its activity several times,
don't return from run() after just one sync() call. Instead,
have run() wait for a while and call sync() a second time, then
wait again and sync() a third time, and so on until you get
sick of it.

If the wait is for a specified interval, Thread.sleep()
is easy to use. If the moment to re-sync() is determined by
an external stimulus, you can use wait() and notify(). And
for the "I'm sick of it" eventuality, set a flag that tells
run() to return instead of sync()ing, then awaken it in the
ordinary manner. You may or may not desire to join() the
thread after telling it to cease hostilities.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top