开发者

Multi Processing in JAVA

I have a Process in Java, tat takes 4 mins to complete. 10 such process lin开发者_如何转开发ed up takes around 40 mins. If i Thread the processes like One thread for a process, all the 10 process would be running parallely, it takes around 20 mins to complete. Isn't it suppose to take 4 mins to complete since all the process are running in parallel?


If all those threads are running on a single processor the operating system has to time slice and context switch. That overhead adds time.

There is no parallelism until you have multiple processors to split the work. Threads can't reduce the work on a single processor.


Isn't it suppose to take 4 mins to complete since all the process are running in parallel?????

Only if you actually had 10 cores and the program is completely CPU-bound. Either your machine has only 2 cores, or the program is partially IO- or memory-bound. All of those are quite common.


The actual number of threads running at the same time typically depends on the number of cores your CPU has. I guess you have a dual-core CPU, hence the times.


The 10 processes can only run parallel if you have 10 cores on your CPU. But they do run concurrently.


Only if you have 10 processors, 10 IO systems etc.

Multiple threads need to share resources


It's like divididing up a contracting job into 5 contracts - electrical, carpententry, painting, landscape, and flooring. It will take you 1/5 the time if you hire 5 people, but if you just have one it takes the same amount of time.


One additional thought, multi threading will only improve the time to solve a problem if the problem is parallelizable and there are no shared resources that can bottleneck the process. I've found that if something that should be parallelizable exhibits poor performance when parallelized its due to access to a shared resource.


If only life were so simple. First, assume you have just one CPU with no hyperthreading magic, so on your machine only 1 thread is running at any given time. Now consider two extremes in your possible task profile:

  1. CPU-bound - your task does extemely heavy math calculation, it takes four minutes to complete.

  2. IO-bound (or network if you like) - each task goes off to a synchronous web service that take 4 minutes to provide a result.

In case 1, adding threads does not help. You still need the same amount of time for your single core to do all that math. You absolutely must have more CPUs to make this go faster.

In case 2, assuming the website reponse time does not degrade under load, you might more reasonably expect total time of 4 minutes. Each thread kicks off its request, and then it is in a wait state until the web response arrives, allowing the other threads to kick off their own requests.

In practice, your task likely has some mix of I/O and CPU delays that combine to produce the "in-between 1 and 2" total execution time you are actually seeing.

You also must not forget the overhead of managing other processes on the machine (even system processes have to be managed), and operating system management of your process - which will grow just a little for each new thread you include in your process.


It depends on what your processes are doing. If e.g. one process does a lot of IO operation while the others is doing a lot of calculation operations, they would indeed only take the 4 together.

If both process are taking the same resources, they take longer because one has to wait for the resource to be available.

How long a threaded process takes cant be said if you don't tell us what the process does, where it is running, what else is running, etc. etc. etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜