Hi,
Is there anyway to create multiple light-weight process in Java? Or is
there anyway that I could make a program read from a file while
executing something else in the same time?
I tried to create a program that could read data from a file while
analyzing previous data that I fetch before in a multi-threaded
environment. However, I realized that JVM is a single lightweight
process that blocked other threads when one of the thread makes a 'read'
or 'write' call.
Thank you!
Song
Gordon Beaton - 31 Oct 2003 15:48 GMT
> Is there anyway to create multiple light-weight process in Java? Or
> is there anyway that I could make a program read from a file while
[quoted text clipped - 5 lines]
> process that blocked other threads when one of the thread makes a 'read'
> or 'write' call.
Define a class for your task that implements java.lang.Runnable, then
create an instance of your class and a java.lang.Thread to run it:
MyTask task = new MyTask();
Thread t = new Thread(task);
t.start();
At this point, the task's run method will execute independently of the
main thread where you did the above steps. Your code will continue
with the next statement after t.start(), which doesn't block.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Nils O. =?iso-8859-1?Q?Sel=E5sdal?= - 31 Oct 2003 15:51 GMT
> Hi,
>
> Is there anyway to create multiple light-weight process in Java? Or is
> there anyway that I could make a program read from a file while
> executing something else in the same time?
You can easily create threads in java, that's what people have been
doing for solving the "problem" you describe for many years.
See e.g. http://java.sun.com/docs/books/tutorial/essential/threads/
For java v 1.4 and later take a look at java.nio.* package, it
provides a select/poll like methods if you're familiar with those
from e.g. unix.
http://java.sun.com/j2se/1.4.1/docs/guide/nio/
> I tried to create a program that could read data from a file while
> analyzing previous data that I fetch before in a multi-threaded
> environment. However, I realized that JVM is a single lightweight
> process that blocked other threads when one of the thread makes a 'read'
> or 'write' call.
If this is the case, you must have a very odd Java Virtual machine,
certanly not a standard one.

Signature
Vennlig hilsen/Best Regards
Nils Olav Selåsdal <NOS at Utel.no>
System Engineer
UtelSystems a/s