Asynchronous external calling of a Runnable's method?

Discussion in 'Java' started by Chris Cornell, Jun 14, 2004.

  1. 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
    Chris Cornell, Jun 14, 2004
    #1
    1. Advertising

  2. Chris Cornell

    Eric Sosman Guest

    Chris Cornell wrote:
    > 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.

    --
    Eric Sosman, Jun 14, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olivier Ricou

    RMI, Exporter and Runnable

    Olivier Ricou, Jul 29, 2003, in forum: Java
    Replies:
    3
    Views:
    6,129
  2. Renato Battaglia
    Replies:
    3
    Views:
    2,789
    Jon Skeet
    Oct 24, 2003
  3. Chris Miller
    Replies:
    4
    Views:
    13,282
    Chris Smith
    Nov 22, 2003
  4. Richard A. DeVenezia

    Thread (Runnable) and action requests

    Richard A. DeVenezia, May 5, 2004, in forum: Java
    Replies:
    0
    Views:
    405
    Richard A. DeVenezia
    May 5, 2004
  5. iksrazal

    TimerTask inside Runnable?

    iksrazal, Jun 29, 2004, in forum: Java
    Replies:
    5
    Views:
    5,222
    iksrazal
    Jul 1, 2004
Loading...

Share This Page