JAVA多線程的控制

來源:互聯網
上載者:User

B/S結構的項目中有報表產生的處理,由於處理時間過長,採用多線程的方式做成。點擊“報表產生”按鈕提交多線程請求,服務端啟動一個線程就立刻Response,提示客戶到報表一覽畫面察看結果。原來對於用戶端啟用多線程並沒有加以限制,實際運用中,客戶可以不斷的點擊按鈕提交線程請求,服務端就不斷的產生子線程,有可能導致嚴重的後果。因此,考慮對用戶端的請求加以限制,如果超過該機能的線程限制就必須等待,直到當前子線程運行完畢之後再請求新的子線程。

類中新增成員變數:

 private static HashMap oThreadCount = new HashMap();
 
 private final static int THREAD_LIMIT = 3;

在建構函式裡,以當前的類的類名作為Map的Key值,保證多個報表類使用同一個“池”

 public KjnBaseReport(String strReportID, String strTempFileName) {
  
      String strClassName = this.getClass().getName();

      // 一個用戶端一個請求,每次請求時都會 New 一個執行個體, 必須保證同一個類的執行個體使用同一個計數器
      if(!oThreadCount.containsKey(strClassName))
             oThreadCount.put(strClassName, String.valueOf(0));
       ...
 }

追加兩個同步方法,一個增加,一個減少。保證對計數器的操作是同步的,是安全執行緒的。

 private synchronized boolean IncreaseThreadCount()  {
  String strClassName = this.getClass().getName();
  int nThreadCount = 0;
  if(oThreadCount.containsKey(strClassName)) {
   nThreadCount = Integer.parseInt(oThreadCount.get(strClassName).toString());
   if(nThreadCount < THREAD_LIMIT) {
    nThreadCount++;
    oThreadCount.put(strClassName, String.valueOf(nThreadCount));
    return true;
   }
  }
  return false;
 }
 
 private synchronized boolean DecreaseThreadCount() {
  String strClassName = this.getClass().getName();
  int nThreadCount = 0;
  if(oThreadCount.containsKey(strClassName)) {
   nThreadCount = Integer.parseInt(oThreadCount.get(strClassName).toString());
   if(nThreadCount > 0) {
    nThreadCount--;
    oThreadCount.put(strClassName, String.valueOf(nThreadCount));
    return true;
   }
  }
  return false;
 }

修改非同步呼叫“產生報表”的介面:

public synchronized void asyncExport(IStvDataSet oDataSet) {
  if(this.oThread == null) {
   this.oDataSet = oDataSet;

   // 判斷增加線程數目是否超過限制?
   if(this.IncreaseThreadCount()) {
    this.oThread = new Thread(this);
    this.oThread.start();
   }

   // 如果超過限制,則拋出受限的異常(為了減少代碼的修改量不改變方法的簽名,只好拋出異常)
   else
    this.oException = new Exception("Thread Limit");
  }
 }

這樣在Action中,如果該方法拋出異常(可能還要對Exception類型進行細分)就在jsp上註冊另一段javascript提示使用者當前系統正忙,請稍後再請求。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.