(Java Concurrency)Multithreading Benefits

原文链接: http://tutorials.jenkov.com/java-concurrency/benefits.html

Some benefits are:

  1. Better resource utilization.
  2. Simpler program design in some situations.
  3. More responsive programs.

Better resource utilization

Imagine an application that reads and processes files from the local file system. Let’s say that reading of file from disk takes 5 seconds and processing it takes 2 seconds. Processing two files then takes

  5 seconds reading file A
  2 seconds processing file A
  5 seconds reading file B
  2 seconds processing file B
-----------------------
 14 seconds total

When reading the file from disk most of the CPU time is spent waiting for the disk to read the data. The CPU is pretty much idle during that time. It could be doing something else. By changing the order of the operations, the CPU could be better utilized. Look at this ordering:

 5 seconds reading file A
  5 seconds reading file B + 2 seconds processing file A
  2 seconds processing file B
-----------------------
 12 seconds total

In general, the CPU can be doing other things while waiting for IO. It doesn’t have to be disk IO. It can be network IO as well, or input from a user at the machine. Network and disk IO is often a lot slower than CPU’s and memory IO.

Simpler Program Design

If you were to program the above ordering of reading and processing by hand in a singlethreaded application, you would have to keep track of both the read and processing state of each file. Instead you can start two threads that each just reads and processes a single file. Each of these threads will be blocked while waiting for the disk to read its file. While waiting, other threads can use the CPU to process the parts of the file they have already read. The result is, that the disk is kept busy at all times, reading from various files into memory. This results in a better utilization of both the disk and the CPU. It is also easier to program, since each thread only has to keep track of a single file.

More responsive programs

Another common goal for turning a single-threaded application into a multithreaded application is to achieve a more responsive application. Imagine a server application that listens on some port for incoming requests. when a request is received, it handles the request and then goes back to listening. The server loop is sketched below:

  while(server is active){
    listen for request
    process request
  }

If the request takes a long time to process, no new clients can send requests to the server for that duration. Only while the server is listening can requests be received.

An alternate design would be for the listening thread to pass the request to a worker thread, and return to listening immediately. The worker thread will process the request and send a reply to the client. This design is sketched below:

  while(server is active){
    listen for request
    hand request to worker thread
  }

This way the server thread will be back at listening sooner. Thus more clients can send requests to the server. The server has become more responsive.

Reference

[1] http://tutorials.jenkov.com/java-concurrency/benefits.html

你可能感兴趣的:(Java并发)