Threads question

N

newsuser

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.
 
M

Matt Humphrey

newsuser said:
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 (e-mail address removed) http://www.iviz.com/
 
N

newsuser

Hi, I have a doubt about how to use threads.
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!
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!
 
M

Matt Humphrey

such

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 (e-mail address removed) http://www.iviz.com/
 

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

Similar Threads

Threads 5
Threads question 4
threads and GUIs 18
Threads and UI in Android 17
Error with server 3
Anaconda Alternative Question 1
JNI, threads are still running after DestroyJavaVM() 2
synchronize threads 3

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top