最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java web如何解決瞬間高并發(fā)

 更新時間:2017年03月08日 09:38:05   作者:記憶八秒的魚  
這篇文章主要為大家詳細介紹了java web解決瞬間高并發(fā)的策略,具有一定的參考價值,感興趣的小伙伴們可以參考一下

1、任何的高并發(fā),請求總是會有一個順序的

2、java的隊列的數(shù)據(jù)結(jié)構(gòu)是先進先出的取值順序

3、BlockingQueue類(線程安全)(使用方法可以百度)

一般使用LinkedBlockingQueue

利用以上幾點,我們可以把高并發(fā)時候的請求放入一個隊列,隊列的大小可以自己定義,比如隊列容量為1000個數(shù)據(jù),那么可以利用過濾器或者攔截器把當前的請求放入隊列,如果隊列的容量滿了,其余的請求可以丟掉或者作出相應回復

具體實施:

利用生產(chǎn)者、消費者模型:

將隊列的請求一一處理完。

 上代碼:

/**
 * @author fuguangli
 * @description 前沿消費者類
 * @Create date:  2017/3/7
 * @using  EXAMPLE
 */
public class Customer implements Runnable{


  /**
   *     拋出異常  特殊值    阻塞     超時
   插入    add(e)  offer(e)  put(e)  offer(e, time, unit)
   移除    remove()  poll()  take()  poll(time, unit)
   檢查    element()  peek()  不可用  不可用

   */
  private BlockingQueue blockingQueue;
  private AtomicInteger count = new AtomicInteger();
  public Customer(BlockingQueue blockingQueue) {
    this.blockingQueue = blockingQueue;
  }

  /**
   * When an object implementing interface <code>Runnable</code> is used
   * to create a thread, starting the thread causes the object's
   * <code>run</code> method to be called in that separately executing
   * thread.
   * <p/>
   * The general contract of the method <code>run</code> is that it may
   * take any action whatsoever.
   *
   * @see Thread#run()
   */
  @Override
  public void run() {
    System.out.println("消費者線程啟動...");
    LockFlag.setCustomerRunningFlag(true);
    try {
      while (LockFlag.getProducerRunningFlag()){
        System.out.println(Thread.currentThread().getId()+"I'm Customer.Queue current size="+blockingQueue.size());
        String data = (String) blockingQueue.poll(10, TimeUnit.SECONDS);
        if(data!=null){
          System.out.println(Thread.currentThread().getId()+"*************正在消費數(shù)據(jù) data="+data);
        }else{
          //表示超過取值時間,視為生產(chǎn)者不再生產(chǎn)數(shù)據(jù)
          System.out.println(Thread.currentThread().getId()+"隊列為空無數(shù)據(jù),請檢查生產(chǎn)者是否阻塞");
        }
        Thread.sleep(50);
      }
      System.err.println("消費者程序執(zhí)行完畢");
    } catch (InterruptedException e) {
      e.printStackTrace();
      System.err.println("消費者程序退出");
      LockFlag.setCustomerRunningFlag(false);//異常退出線程
      Thread.currentThread().interrupt();
    }
  }
}

package com.qysxy.framework.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author fuguangli
 * @description 隊列生產(chǎn)者類
 * @Create date:  2017/3/7
 * @using    EXAMPLE
 */
public class Producer implements Runnable{


  /**
   *     拋出異常  特殊值    阻塞     超時
   插入  add(e)  offer(e)  put(e)  offer(e, time, unit)
   移除  remove()  poll()  take()  poll(time, unit)
   檢查  element()  peek()  不可用  不可用

   */
  private BlockingQueue blockingQueue;
  private AtomicInteger count = new AtomicInteger();
  public Producer(BlockingQueue blockingQueue) {
    this.blockingQueue = blockingQueue;
  }

  /**
   * When an object implementing interface <code>Runnable</code> is used
   * to create a thread, starting the thread causes the object's
   * <code>run</code> method to be called in that separately executing
   * thread.
   * <p/>
   * The general contract of the method <code>run</code> is that it may
   * take any action whatsoever.
   *
   * @see Thread#run()
   */
  @Override
  public void run() {
    System.out.println("生產(chǎn)者線程啟動...");
    LockFlag.setProducerRunningFlag(true);
    try {
      while (LockFlag.getProducerRunningFlag()){
        String data = "data:"+count.incrementAndGet();
        if(blockingQueue.offer(data,10, TimeUnit.SECONDS)){
          //返回true表示生產(chǎn)數(shù)據(jù)正確
          System.out.println("^^^^^^^^^^^^^^正在生產(chǎn)數(shù)據(jù) data="+data);
        }else {
          //表示阻塞時間內(nèi)還沒有生產(chǎn)者生產(chǎn)數(shù)據(jù)
          System.out.println("生產(chǎn)者異常,無法生產(chǎn)數(shù)據(jù)");
        }
        Thread.sleep(50);

      }
    } catch (InterruptedException e) {
      e.printStackTrace();
      System.err.println("生產(chǎn)者程序退出");
      LockFlag.setProducerRunningFlag(false);//異常退出線程
      Thread.currentThread().interrupt();
    }
  }
}

package com.qysxy.framework.queue;

/**
 * @author fuguangli
 * @description 前沿生產(chǎn)者消費者模型的鎖類
 * @Create date:  2017/3/7
 */
public class LockFlag {
  /**
   * 生產(chǎn)者互斥鎖
   */
  private static Boolean producerRunningFlag = false;
  /**
   * 消費者互斥鎖
   */
  private static Boolean customerRunningFlag = false;

  public static Boolean getProducerRunningFlag() {
    return producerRunningFlag;
  }

  public static void setProducerRunningFlag(Boolean producerRunningFlag) {
    LockFlag.producerRunningFlag = producerRunningFlag;
  }

  public static Boolean getCustomerRunningFlag() {
    return customerRunningFlag;
  }

  public static void setCustomerRunningFlag(Boolean customerRunningFlag) {
    LockFlag.customerRunningFlag = customerRunningFlag;
  }
}

package com.qysxy.framework.queue;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Queue;
import java.util.concurrent.*;

/**
 * @author fuguangli
 * @description 前沿隊列實用類,用于大量并發(fā)用戶
 * @Create date:  2017/3/7
 */
public class BlockingQueueHelper {


  private static final Integer maxQueueSize = 1000;
  private static BlockingQueue blockingQueue = new LinkedBlockingQueue(maxQueueSize);
  private static ExecutorService threadPool = Executors.newCachedThreadPool();


  public static BlockingQueue getBlockingQueue() {
    if (blockingQueue == null) {
      blockingQueue = new LinkedBlockingQueue(maxQueueSize);
    }
    return blockingQueue;
  }

  /**
   * @param o 隊列處理對象(包含request,response,data)
   */
  public static void requestQueue(Object o) {
    //檢測當前的隊列大小
    if (blockingQueue != null && blockingQueue.size() < maxQueueSize) {
      //可以正常進入隊列
      if (blockingQueue.offer(o)) {
        //添加成功,檢測數(shù)據(jù)處理線程是否正常
        if (LockFlag.getCustomerRunningFlag()) {
          //說明處理線程類正常運行
        } else {
          //說明處理線程類停止,此時,應重新啟動線程進行數(shù)據(jù)處理
          LockFlag.setCustomerRunningFlag(true);

          //example:run
          Customer customer = new Customer(blockingQueue);
          threadPool.execute(customer);

        }

      } else {
        //進入隊列失敗,做出相應的處理,或者嘗試重新進入隊列

      }
    } else {
      //隊列不正常,或隊列大小已達上限,做出相應處理

    }

  }
}

好了,這時候,利用過濾器或者攔截器將每個請求封裝成隊列元素進行處理就行。

當然了,對于多應用服務(wù)器的部署架構(gòu)來說,數(shù)據(jù)庫也需要加鎖,數(shù)據(jù)庫隔離級別下篇再說。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java簡單實現(xiàn)自定義日歷

    java簡單實現(xiàn)自定義日歷

    這篇文章主要為大家詳細介紹了java簡單實現(xiàn)自定義日歷,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 簡單了解Spring beanfactory循環(huán)依賴命名重復屬性

    簡單了解Spring beanfactory循環(huán)依賴命名重復屬性

    這篇文章主要介紹了簡單了解Spring beanfactory循環(huán)依賴命名重復2大屬性,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • Java中的五種引用類型詳解

    Java中的五種引用類型詳解

    Java中的五種引用類型:強引用、軟引用、弱引用、虛引用和Final引用,分別用于管理對象的生命周期,幫助垃圾回收器更高效地回收不再使用的對象
    2024-12-12
  • Springboot集成Proguard生成混淆jar包方式

    Springboot集成Proguard生成混淆jar包方式

    本文介紹了兩種Java代碼混淆工具:ClassFinal和ProGuard,ClassFinal是一個字節(jié)碼加密工具,但需要額外的加密包,使用復雜,ProGuard是一款開源的Java代碼混淆工具,可以有效地提高代碼的安全性,但對Spring框架的注解處理不夠完善
    2024-11-11
  • 解決Java中由于數(shù)據(jù)太大自動轉(zhuǎn)換成科學計數(shù)法的問題

    解決Java中由于數(shù)據(jù)太大自動轉(zhuǎn)換成科學計數(shù)法的問題

    今天小編就為大家分享一篇解決Java中由于數(shù)據(jù)太大自動轉(zhuǎn)換成科學計數(shù)法的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Mybatis如何從數(shù)據(jù)庫中獲取數(shù)據(jù)存為List類型(存為model)

    Mybatis如何從數(shù)據(jù)庫中獲取數(shù)據(jù)存為List類型(存為model)

    這篇文章主要介紹了Mybatis如何從數(shù)據(jù)庫中獲取數(shù)據(jù)存為List類型(存為model),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • springboot集成redis實現(xiàn)消息的訂閱與發(fā)布

    springboot集成redis實現(xiàn)消息的訂閱與發(fā)布

    本文主要介紹了springboot集成redis實現(xiàn)消息的訂閱與發(fā)布,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-05-05
  • spring-boot-maven-plugin引入出現(xiàn)爆紅(已解決)

    spring-boot-maven-plugin引入出現(xiàn)爆紅(已解決)

    這篇文章主要介紹了spring-boot-maven-plugin引入出現(xiàn)爆紅(已解決),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • Java如何獲取JSONObject內(nèi)指定字段key的value值

    Java如何獲取JSONObject內(nèi)指定字段key的value值

    這篇文章主要介紹了Java如何獲取JSONObject內(nèi)指定字段key的value值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java中的Vector和ArrayList區(qū)別及比較

    Java中的Vector和ArrayList區(qū)別及比較

    這篇文章主要介紹了Java中的Vector和ArrayList區(qū)別及比較,本文從API、同步、數(shù)據(jù)增長、使用模式4個方面總結(jié)了它們之間的不同之處,需要的朋友可以參考下
    2015-03-03

最新評論

永宁县| 两当县| 思茅市| 荣成市| 龙口市| 乌鲁木齐市| 长乐市| 涞源县| 云霄县| 龙泉市| 寿阳县| 宜川县| 香格里拉县| 呼图壁县| 平罗县| 青海省| 阜新市| 原阳县| 东丽区| 诏安县| 永州市| 九江县| 临汾市| 子洲县| 河南省| 博野县| 康保县| 四子王旗| 镇沅| 巩义市| 洛隆县| 宁安市| 莱西市| 福鼎市| 多伦县| 仙桃市| 师宗县| 宜宾市| 肥城市| 黑水县| 吴江市|