Java5 多線程實踐 — java.util.concurrent

來源:互聯網
上載者:User
Java5增加了新的類庫並發集java.util.concurrent,該類庫為並發程式提供了豐富的API多線程編程在 Java 5中更加容易,靈活。本文通過一個網路伺服器模型,來實踐Java5的多線程編程,該模型中使用了Java5中的線程池,阻塞隊列,可重新進入鎖等,還實踐了 Callable, Future等介面,並使用了Java 5的另外一個新特性泛型。

簡介

原文:http://www.ibm.com/developerworks/cn/java/j-zhanghz/

本文將實現一個網路伺服器模型,一旦有用戶端串連到該伺服器,則啟動一個新線程為該串連服務,服務內容為往用戶端輸送一些字元資訊。一個典型 的網路伺服器模型如下:

1. 建立監聽連接埠。

2. 發現有新串連,接受串連,啟動線程,執行服務線程。

3. 服務完畢,關閉線程。

這個模型在大部分情況下運行良好,但是需要頻繁的處理使用者請求而每次請求需要的服務又是簡短的時候,系統會將大量的時間花費線上程的建立銷 毀。Java 5的線程池克服了這些缺點。通過對重用線程來執行多個任務,避免了頻繁線程的建立與銷毀開銷,使得伺服器的效能方面得到很大提高。因此,本文的網路伺服器 模型將如下:

1. 建立監聽連接埠,建立線程池。

2. 發現有新串連,使用線程池來執行服務任務。

3. 服務完畢,釋放線程到線程池。

下面詳細介紹如何使用Java 5的concurrent包提供的API來實現該伺服器。

初始化

初始化包括建立線程池以及初始化監聽連接埠。 建立線程池可以通過調用java.util.concurrent.Executors類裡的靜態方法newChahedThreadPool或是 newFixedThreadPool來建立,也可以通過建立一個java.util.concurrent.ThreadPoolExecutor執行個體 來執行任務。這裡我們採用newFixedThreadPool方法來建立線程池。

ExecutorService pool = Executors.newFixedThreadPool(10);
表示建立了一個線程池,線程池裡面有10個線程為任務佇列服務。

使用ServerSocket對象來初始化監聽連接埠。

private static final int PORT = 19527;
serverListenSocket = new ServerSocket(PORT);
serverListenSocket.setReuseAddress(true);
serverListenSocket.setReuseAddress(true);

服務新串連

當有新串連建立時,accept返回時,將服務任務提交給線程池執行。

while(true){
Socket socket = serverListenSocket.accept();
pool.execute(new ServiceThread(socket));
}

這裡使用線程池對象來執行線程,減少了每次線程建立和銷毀的開銷。任務執行完畢,線程釋放到線程池。

服務任務

服務線程ServiceThread維護一個count來記錄服務線程被調用的次數。每當服務任務被調用一次時,count的值自增1,因此 ServiceThread提供一個increaseCount和getCount的方法,分別將count值自增1和取得該count值。由於可能多個 線程存在競爭,同時訪問count,因此需要加鎖機制,在Java 5之前,我們只能使用synchronized來鎖定。Java 5中引入了效能更加粒度更細的重入鎖ReentrantLock。我們使用ReentrantLock保證代碼安全執行緒。下面是具體代碼:

private static ReentrantLock lock = new ReentrantLock ();
private static int count = 0;
private int getCount(){
 int ret = 0;
 try{
  lock.lock();
  ret = count;
 }finally{
  lock.unlock();
 }
 return ret;
}
private void increaseCount(){
 try{
  lock.lock();
  ++count;
 }finally{
  lock.unlock();
 }
}

服務線程在開始給用戶端列印一個歡迎資訊,

increaseCount();
int curCount = getCount();
helloString = "hello, id = " + curCount+"\r\n";
dos = new DataOutputStream(connectedSocket.getOutputStream());
dos.write(helloString.getBytes());

然後使用ExecutorService的submit方法提交一個Callable的任務,返回一個Future介面的引用。這種做法對費 時的任務非常有效,submit任務之後可以繼續執行下面的代碼,然後在適當的位置可以使用Future的get方法來擷取結果,如果這時候該方法已經執 行完畢,則無需等待即可獲得結果,如果還在執行,則等待到運行完畢。

ExecutorService executor = Executors.newSingleThreadExecutor();
Future <String> future = executor.submit(new TimeConsumingTask());

dos.write("let's do soemthing other".getBytes());
String result = future.get();

dos.write(result.getBytes());

其中TimeConsumingTask實現了Callable介面
class TimeConsumingTask implements Callable <String>{
 public String call() throws Exception {
  System.out.println
  ("It's a time-consuming task,
  you'd better retrieve your result in the furture");
  return "ok, here's the result: It takes me lots of time to produce this result";
 }
}

這裡使用了Java 5的另外一個新特性泛型,聲明TimeConsumingTask的時候使用了String做為型別參數。必須實現Callable介面的call函數, 其作用類似與Runnable中的run函數,在call函數裡寫入要執行的代碼,其傳回值類型等同於在類聲明中傳入的類型值。在這段程式中,我們提交了 一個Callable的任務,然後程式不會堵塞,而是繼續執行dos.write("let's do soemthing other".getBytes());當程式執行到String result = future.get()時如果call函數已經執行完畢,則取得傳回值,如果還在執行,則等待其執行完畢。

伺服器端的完整實現

伺服器端的完整實現代碼如下:

package com.andrew;import java.io.DataOutputStream;import java.io.IOException;import java.io.Serializable;import java.net.ServerSocket;import java.net.Socket;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.RejectedExecutionHandler;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.ReentrantLock;public class Server { private static int produceTaskSleepTime = 100; private static int consumeTaskSleepTime = 1200; private static int produceTaskMaxNumber = 100; private static final int CORE_POOL_SIZE = 2; private static final int MAX_POOL_SIZE = 100; private static final int KEEPALIVE_TIME = 3; private static final int QUEUE_CAPACITY = (CORE_POOL_SIZE + MAX_POOL_SIZE) / 2; private static final TimeUnit TIME_UNIT = TimeUnit.SECONDS; private static final String HOST = "127.0.0.1"; private static final int PORT = 19527; private BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(   QUEUE_CAPACITY); //private ThreadPoolExecutor serverThreadPool = null;  private ExecutorService pool = null; private RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.DiscardOldestPolicy(); private ServerSocket serverListenSocket = null; private int times = 5; public void start() {  // You can also init thread pool in this way.  /*serverThreadPool = new ThreadPoolExecutor(CORE_POOL_SIZE,    MAX_POOL_SIZE, KEEPALIVE_TIME, TIME_UNIT, workQueue,    rejectedExecutionHandler);*/  pool = Executors.newFixedThreadPool(10);  try {   serverListenSocket = new ServerSocket(PORT);   serverListenSocket.setReuseAddress(true);   System.out.println("I'm listening");   while (times-- > 0) {    Socket socket = serverListenSocket.accept();    String welcomeString = "hello"; //serverThreadPool.execute(new ServiceThread(socket, welcomeString));    pool.execute(new ServiceThread(socket));   }  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }  cleanup(); } public void cleanup() {  if (null != serverListenSocket) {   try {    serverListenSocket.close();   } catch (IOException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }  }  //serverThreadPool.shutdown();  pool.shutdown(); } public static void main(String args[]) {  Server server = new Server();  server.start(); }}class ServiceThread implements Runnable, Serializable { private static final long serialVersionUID = 0; private Socket connectedSocket = null; private String helloString = null; private static int count = 0; private static ReentrantLock lock = new ReentrantLock(); ServiceThread(Socket socket) {  connectedSocket = socket; } public void run() {  increaseCount();  int curCount = getCount();  helloString = "hello, id = " + curCount + "\r\n";  ExecutorService executor = Executors.newSingleThreadExecutor();  Future<String> future = executor.submit(new TimeConsumingTask());  DataOutputStream dos = null;  try {   dos = new DataOutputStream(connectedSocket.getOutputStream());   dos.write(helloString.getBytes());   try {    dos.write("let's do soemthing other.\r\n".getBytes());    String result = future.get();    dos.write(result.getBytes());   } catch (InterruptedException e) {    e.printStackTrace();   } catch (ExecutionException e) {    e.printStackTrace();   }  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();  } finally {   if (null != connectedSocket) {    try {     connectedSocket.close();    } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }   }   if (null != dos) {    try {     dos.close();    } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }   }   executor.shutdown();  } } private int getCount() {  int ret = 0;  try {   lock.lock();   ret = count;  } finally {   lock.unlock();  }  return ret; } private void increaseCount() {  try {   lock.lock();   ++count;  } finally {   lock.unlock();  } }}class TimeConsumingTask implements Callable<String> { public String call() throws Exception {  System.out    .println("It's a  time-consuming task,    you'd better retrieve your result in the furture");  return "ok, here's the result: It takes me lots of time to produce this result"; }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.