How to create multiple light-weight process in Java

S

Song Ching Koh

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
 
G

Gordon Beaton

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.

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
 
N

Nils O. =?iso-8859-1?Q?Sel=E5sdal?=

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top