Threads question

Discussion in 'Java' started by newsuser, Dec 22, 2003.

  1. newsuser

    newsuser Guest

    Hi, I have a doubt about how to use threads.
    Suppose I want to open, read/write and close a file,
    and let's suppose that all these 4 operations are
    lengthy. So I create a thread which will open the
    file, and then at any given time (when I need to
    read or write to that file) I ask the thread to do
    that...but how? What I have clear is that I can
    overload run() method and open the file there,
    but then what? How do I notify the thread (which
    should be sleeping) about the new task to do?
    Because doing this shouldn't work (I guess):

    Threat t = new Thread(...);
    t.start(); // this calls run() method, which will block

    and later...

    t.method();

    right? Because that would call method() but in the
    calling thread...

    Any help?

    TIA.
     
    newsuser, Dec 22, 2003
    #1
    1. Advertising

  2. "newsuser" <> wrote in message
    news:bs783o$a7bko$-berlin.de...
    > Hi, I have a doubt about how to use threads.
    > Suppose I want to open, read/write and close a file,
    > and let's suppose that all these 4 operations are
    > lengthy. So I create a thread which will open the
    > file, and then at any given time (when I need to
    > read or write to that file) I ask the thread to do
    > that...but how? What I have clear is that I can
    > overload run() method and open the file there,
    > but then what? How do I notify the thread (which
    > should be sleeping) about the new task to do?
    > Because doing this shouldn't work (I guess):
    >
    > Threat t = new Thread(...);
    > t.start(); // this calls run() method, which will block


    Why do you think it block? Start runs your run method which then does your
    reading and writing as you have put your lengthy task into the run method.
    In the mean time, your main thread goes about whatever it was doing, such as
    handling UI requests etc.

    >
    > and later...
    >
    > t.method();
    >
    > right? Because that would call method() but in the
    > calling thread...


    No. There is no t.method () and you can't call into a thread.

    What you will need next, however, will be a means for the thread to tell you
    that it's done and a way to enable your work thread and your main thread to
    access your application data in a controlled (synchronized) way so that it
    doesn't get corrupted. We would have to know more about what you're trying
    to accomplish in order to help you. In the mean time you may want to read
    up on threads at http://www.mindprod.com/jgloss/thread.html#THREAD.

    Cheers,
    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Dec 22, 2003
    #2
    1. Advertising

  3. newsuser

    newsuser Guest

    > > Hi, I have a doubt about how to use threads.
    > > Suppose I want to open, read/write and close a file,
    > > and let's suppose that all these 4 operations are
    > > lengthy. So I create a thread which will open the
    > > file, and then at any given time (when I need to
    > > read or write to that file) I ask the thread to do
    > > that...but how? What I have clear is that I can
    > > overload run() method and open the file there,
    > > but then what? How do I notify the thread (which
    > > should be sleeping) about the new task to do?
    > > Because doing this shouldn't work (I guess):
    > >
    > > Threat t = new Thread(...);
    > > t.start(); // this calls run() method, which will block

    >
    > Why do you think it block? Start runs your run method which then does

    your
    > reading and writing as you have put your lengthy task into the run method.
    > In the mean time, your main thread goes about whatever it was doing, such

    as
    > handling UI requests etc.


    Sorry for the confusion. What I was trying to say is that
    run() would block because I'd call wait.
    Let me explain it again (hope to do it better this time)
    Suppose all these 4 operations are lengthy and thus
    recommended to be called in a second thread: open,
    read, write, close.
    The problem is that *I don't know* at what time of
    the program cycle I'll need to read or write (it could
    be even done many times). So the only way (I can think
    of) is opening the file(s) needed, and waiting for the
    time when a read or write is needed. Before the program
    finishes, I should close file(s).
    That's why I mentioned than my run() would block,
    as I'd need something like this:

    1. open file(s)
    2. wait for read / write request
    3. close file(s)

    See the difference? It's not about putting all these
    in run(), because I don't know when I'll read or
    write data!

    > > and later...
    > >
    > > t.method();
    > >
    > > right? Because that would call method() but in the
    > > calling thread...

    >
    > No. There is no t.method () and you can't call into a thread.


    You're right. My mistake. What I meant was doing
    something like

    class foo extends Thread ...

    and then call F.method() where F is a foo object.

    > What you will need next, however, will be a means for the thread to tell

    you
    > that it's done and a way to enable your work thread and your main thread

    to
    > access your application data in a controlled (synchronized) way so that it
    > doesn't get corrupted. We would have to know more about what you're

    trying
    > to accomplish in order to help you. In the mean time you may want to read
    > up on threads at http://www.mindprod.com/jgloss/thread.html#THREAD.


    Yes, I know how to accomplish all this. My problem is in the "other
    direction".

    Thanks!
     
    newsuser, Dec 23, 2003
    #3

  4. > > Why do you think it block? Start runs your run method which then does

    > your
    > > reading and writing as you have put your lengthy task into the run

    method.
    > > In the mean time, your main thread goes about whatever it was doing,

    such
    > as
    > > handling UI requests etc.

    >
    > Sorry for the confusion. What I was trying to say is that
    > run() would block because I'd call wait.
    > Let me explain it again (hope to do it better this time)
    > Suppose all these 4 operations are lengthy and thus
    > recommended to be called in a second thread: open,
    > read, write, close.


    If you only have a couple of lengthy tasks the easiest thing to do is to
    create a thread for each one at the time you need it. It would look
    something like this, for example. (Code is for illustration only, don't
    assume the syntax is precise.)

    public abstract class Task implements Runnable {
    public Task (whatever you need to talk to your application model) {
    }

    public abstract void run (); // placeholder for subclass implementations
    }

    public class TaskOpen extends Task {
    public OpenTask (extra paraemeters) {
    super (base parameters);
    }

    public void run () {
    // Do whatever your opening job is
    }
    }

    Thread openThread = new Thread (new TaskOpen (parameters));
    openThread.start ();

    Make up a subclass for each task where the run method does the appropriate
    thing.

    This is a bare-bones example. It's normal to extend the Task definition
    with a boolean flag for stopping an in-progress task and with calls to
    determine where it's done or to post an event (callback) when it's finished.
    I think a full example including correctly handling race conditions is
    either in Doug Lea's book 'Concurrent Programming in Java' or Paul Hyde's
    'Java Thread Programming'

    You can even arrange for a thread's work to be made of other threads so that
    if you know you're going to open, read, write and close you don't have to
    reinvent the wheel. Also, by using a superclass you can have a consistent
    thread progress status mechanism as well as a standard way for waiting for
    completion, cancelling, recovering from exceptions and so forth.

    Is this closer to what you're looking for? An application that keeps track
    of operating threads like this is somewhat more complex and there are alot
    of issues like synchronization and error handling that you will have to
    manage.

    Cheers,
    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Dec 23, 2003
    #4
    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. Nick Marden
    Replies:
    0
    Views:
    1,084
    Nick Marden
    Jan 17, 2004
  2. yoda
    Replies:
    2
    Views:
    482
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  3. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    450
    William Ahern
    Aug 27, 2004
  4. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,525
    Arne Vajhøj
    Apr 9, 2008
  5. Une bévue
    Replies:
    0
    Views:
    187
    Une bévue
    Jun 14, 2006
Loading...

Share This Page