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