Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2003

Tip: Looking for answers? Try searching our database.

How to create multiple light-weight process in Java

Thread view: 
Song Ching Koh - 31 Oct 2003 15:43 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?

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.