[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

can i write this in other format?

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
tom_mk
Asked:
tom_mk
2 Solutions
 
Tommy BraasCommented:
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
 
tom_mkAuthor Commented:
example?
0
 
sciuriwareCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now