Solved

can i write this in other format?

Posted on 2004-04-10
5
154 Views
Last Modified: 2010-03-31
thx..

here is the code?
can i write the function in void process(..) in other format?

----------------------
class Server
{

void Do(String w) { . . . }

void Process(final String line)
{ Thread t = new Thread() { public void run() { Do(line);
}
};
t.start();
}
}

Server (InputStream instream) { BufferedReader in = new BufferedReader(new InputStreamReader(instream));
String line;
try { while ((line = in.readLine()) != null) { Process(line);
}
}catch (IOException e) { }
}
}
0
Comment
Question by:tom_mk
5 Comments
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 250 total points
Comment Utility
You can do what you're thinking, but I would strongly recommend against as you would be starting a thread per line in a file. This is bad as threads are expensive to create, and switch between.
0
 

Author Comment

by:tom_mk
Comment Utility
example?
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 250 total points
Comment Utility
If you think that the processing of those data must be done parallel, the only reason could be
that you're on a system with, say, 16 CPU's. That makes sense.
However, if the # of lines in the file is much greater than 16, you will waste a lot of time with all those
threads.

I had to write something similar, well, you can create a collection (synchronised like Vector)
where threads may pick one line by one line to do their work.
Then you start as many threads as makes sense (16 in the case above).

Now, every thread can loop: pick a line, process it, pick a line .... until exhausted.
That will be very efficient.

In my case I had to start a thread on every single data set, but then I linked the threads also in a vector.
Every thread started looked in that vector to see if its rank was higher than the # of CPU's.
If so it slept for a while; this way no more threads were running than CPU's available,
still all datasets had a thread and so a run-status that could be retrieved.

You may choose.

;JOOP!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now