1. 程式人生 > >Windows下redis執行緒池的搭建及使用

Windows下redis執行緒池的搭建及使用

redis的配置檔案為:

redis.pool.maxActive=1024

redis.pool.maxIdle=200

redis.pool.maxWait=1000

redis.pool.testOnBorrow=false

redis.pool.testOnReturn=true

#IP
redis.ip=127.0.0.1     
#Port
redis.port=6379

 其中,每一個redis服務都會有一個埠,預設為6379埠,IP則表示redis的安裝路徑IP。

 搭建Redis的執行緒池:

public class RedisPoolManager {

    private static JedisPool pool;

    static {
        ResourceBundle bundle = ResourceBundle.getBundle("redis");
        if (bundle == null)
            throw new IllegalArgumentException("[redis.properties] is not found");

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
        config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
        config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));
        config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
        config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));

        pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));
    }

    /**
     * Get Jedis resource from the pool
     * @return
     */
    public static Jedis createInstance() {
        Jedis jedis = pool.getResource();
//        jedis.auth("will");
        return jedis;
    }

    /**
     * Return the resource to pool
     * @param jedis
     */
    public static void returnResource(Jedis jedis) {
        pool.returnResource(jedis);
    }

}

  單例模式建立起Redis管理器:(此處的管理器是在jedis基礎上實現的自定義,你也可以直接使用jedis

@Component("cacheManager")
public class CacheManager {

    private volatile static CacheManager uniqueCacheManager;
    private CacheManager(){

    }
    public static CacheManager getInstance(){
        if(uniqueCacheManager == null){
            synchronized (CacheManager.class){
                if(uniqueCacheManager == null){
                    uniqueCacheManager = new CacheManager();
                }
            }
        }
        return uniqueCacheManager;
    }

    private Jedis jedis = RedisPoolManager.createInstance();

    ////////////////////Basic Functions(String related) - Start /////////////////////////////
    /**
     * If the value existed, will override the value
     * @param entries
     */
    public void set(Map<String, String> entries) {
        for (Map.Entry<String, String> entry : entries.entrySet()) {
            jedis.set(entry.getKey(), entry.getValue());
        }
    }

    /**
     * If the key exited, will override the value
     * @param key
     * @param value
     */
    public void set(String key, String value) {
        jedis.set(key, value);
    }

    /**
     *
     * @param entries
     */
    public void setnx(Map<String, String> entries) {
        for (Map.Entry<String, String> entry : entries.entrySet()) {
            jedis.setnx(entry.getKey(), entry.getValue());
        }
    }

    /**
     * Only set the value when the key not exist
     * @param key
     * @param value
     */
    public void setnx(String key, String value) {
        jedis.setnx(key, value);
    }

    /**
     * Set the value to the key and specify the key's life cycle as seconds.
     * @param key
     * @param live
     * @param value
     */
    public void setKeyLive(String key, int live, String value) {
        jedis.setex(key, live, value);
    }

    /**
     * Append the value to an existing key
     * @param key
     * @param value
     */
    public void append(String key, String value) {
        jedis.append(key, value);
    }

    /**
     * Get the value by the given key
     * @param key
     * @return
     */
    public String getValue(String key) {
        return jedis.get(key);
    }

    /**
     * Get the values of the specified keys
     * @param keys
     * @return
     */
    public List<String> getValues(String... keys) {
        return jedis.mget(keys);
    }

    /**
     * remove the value by the key from the cache
     * @param key
     * @return
     */
    public Long removeValue(String key) {
        return jedis.del(key);
    }

    /**
     * Delete the expected values from the cache
     * @param keys
     * @return
     */
    public Long removeValues(String... keys) {
        return jedis.del(keys);
    }

    /**
     * Identify whether the key in the cache or not
     * @param key
     * @return
     */
    public boolean exists(String key) {
        return jedis.exists(key);
    }

    /**
     * Release the resource
     */
    public void returnSource() {
        RedisPoolManager.returnResource(jedis);
    }

    /**
     * Clear the cache
     */
    public String clear() {
        return jedis.flushDB();
    }

    /**
     * Calculate the size of the cache
     * @return
     */
    public long calculateSize() {
        return jedis.dbSize();
    }

    /**
     * Get and update the member by the key in the cache
     * @param key
     * @param value
     * @return
     */
    public String getSet(String key, String value) {
        return jedis.getSet(key, value);
    }

    /**
     *
     * @param key
     * @param startOffset
     * @param endOffset
     * @return
     */
    public String getRange(String key, int startOffset, int endOffset) {
        return jedis.getrange(key, startOffset, endOffset);
    }

    ////////////////////Basic Functions(String related) - End /////////////////////////////



    ////////////////////List Functions - Start /////////////////////////////
    /**
     * push the value to the given list
     *
     * 將一個或多個值 value 插入到列表 key 的表頭

     *如果有多個 value 值,那麼各個 value 值按從左到右的順序依次插入到表頭:
     *比如說,對空列表 mylist 執行命令 LPUSH mylist a b c ,列表的值將是 c b a ,
     *這等同於原子性地執行 LPUSH mylist a 、 LPUSH mylist b 和 LPUSH mylist c 三個命令。

     *如果 key 不存在,一個空列表會被建立並執行 LPUSH 操作。

     *當 key 存在但不是列表型別時,返回一個錯誤。
     *
     * @param listName
     * @param values
     * @return
     */
    public long add2List(String listName, String... values) {
        return jedis.lpush(listName, values);
    }

    /**
     * get the list size
     * @param listName
     * @return
     */
    public long getListSize(String listName) {
        return jedis.llen(listName);
    }

    /**
     * Update the member on the index
     * @param listName
     * @param index
     * @param value
     */
    public void updateList(String listName, int index, String value) {
        jedis.lset(listName, index, value);
    }

    /**
     * Get the value on the index
     * @param listName
     * @param index
     * @return
     */
    public String getIndexValue(String listName, int index) {
        return jedis.lindex(listName, index);
    }

    /**
     * 根據引數 count 的值,移除列表中與引數 value 相等的元素。
     * count 的值可以是以下幾種:

     *count > 0 : 從表頭開始向表尾搜尋,移除與 value 相等的元素,數量為 count 。
     *count < 0 : 從表尾開始向表頭搜尋,移除與 value 相等的元素,數量為 count 的絕對值。
     *count = 0 : 移除表中所有與 value 相等的值。

     *
     * @param listName
     * @param count
     * @param value
     * @return
     */
    public long removeLValue(String listName, int count, String value) {
        return jedis.lrem(listName, count, value);
    }

    /**
     * Remove the value out side of the range
     *
     * @param listName
     * @param start
     * @param end
     * @return
     */
    public String removeOutterValue(String listName, int start, int end) {
        return jedis.ltrim(listName, start, end);
    }

    /**
     * Pop the lists
     * @param listName
     * @return
     */
    public String popList(String listName) {
        return jedis.lpop(listName);
    }

    /**
     * Get the specified list values
     *
     * 返回列表 key 中指定區間內的元素,區間以偏移量 start 和 stop 指定。

     * 下標(index)引數 start 和 stop 都以 0 為底,也就是說,以 0 表示列表的第一個元素,以 1 表示列表的第二個元素,以此類推。

     * 你也可以使用負數下標,以 -1 表示列表的最後一個元素, -2 表示列表的倒數第二個元素,以此類推
     *
     * 注意LRANGE命令和程式語言區間函式的區別

     假如你有一個包含一百個元素的列表,對該列表執行 LRANGE list 0 10 ,結果是一個包含11個元素的列表,
     這表明 stop 下標也在 LRANGE 命令的取值範圍之內(閉區間),  這和某些語言的區間函式可能不一致,
     比如Ruby的 Range.new 、 Array#slice 和Python的 range() 函式
     *
     * @param listName
     * @param start
     * @param end
     * @return
     */
    public List<String> getListValues(String listName, long start, long end) {
        return jedis.lrange(listName, start, end);
    }

    /**
     * Get all of the values of the list
     *
     * @param listName
     * @return
     */
    public List<String> getAllListValues(String listName) {
        return jedis.lrange(listName, 0, -1);
    }

    /**
     * Sort the list
     * @param listName
     * @return
     */
    public List<String> sort(String listName) {
        return jedis.sort(listName);
    }

    /**
     *
     * @param key
     * @param params
     * @param dstKey
     * @return
     */
    public Long sort(String key, SortingParams params, String dstKey) {
        return jedis.sort(key, params, dstKey);
    }

    ////////////////////List Functions - End /////////////////////////////


    ////////////////////Set Functions - Start /////////////////////////////
    /**
     * Identify whether the member in the given set or not
     * @param setName
     * @param member
     * @return
     */
    public boolean exists(String setName, String member) {
        return jedis.sismember(setName, member);
    }

    /**
     * Add the members to set
     * @param setName
     * @param members
     * @return
     */
    public long add2Set(String setName, String... members) {
        return jedis.sadd(setName, members);
    }

    /**
     * Get all of the values of the set
     *
     * @param setName
     * @return
     */
    public Set<String> getAllSetValues(String setName) {
        return jedis.smembers(setName);
    }

    /**
     * Remove members from the set
     *
     * @param setName
     * @param members
     * @return
     */
    public Long removeSValues(String setName, String ... members) {
        return jedis.srem(setName, members);
    }

    /**
     * Set Pop
     * @param setName
     * @return
     */
    public String popSet(String setName) {
        return jedis.spop(setName);
    }

    /**
     * 交集
     * Get the intersection
     * @param sets
     * @return
     */
    public Set<String> intersection(String... sets) {
        return jedis.sinter(sets);
    }

    /**
     * 並集
     * Get the union set of the given sets
     * @param sets
     * @return
     */
    public Set<String> union(String... sets) {
        return jedis.sunion(sets);
    }

    /**
     * 差集
     * Get the difference set of the given sets
     *
     * @param sets
     * @return
     */
    public Set<String> diff(String... sets) {
        return jedis.sdiff(sets);
    }

    ////////////////////Set Functions - End /////////////////////////////


    ////////////////////Sorted Set Functions - Start /////////////////////////////
    /**
     * 將一個或多個 member 元素及其 score 值加入到有序集 key 當中。
     * 如果某個 member 已經是有序集的成員,那麼更新這個 member 的 score 值,並通過重新插入這個 member 元素,
     * 來保證該 member 在正確的位置上。
     *
     *
     * @param ssetName - 如果 ssetName 不存在,則建立一個空的有序集並執行 ZADD 操作。
     *                     當 ssetName 存在但不是有序集型別時,返回一個錯誤。
     * @param score - 可以是整數值或者雙精度浮點數,用於排序
     * @param member
     * @return
     */
    public long add2SSet(String ssetName, double score, String member) {
        return jedis.zadd(ssetName, score, member);
    }

    /**
     * Return the size of the sorted set
     * @param sset
     * @return
     */
    public long card(String sset) {
        return jedis.zcard(sset);
    }

    /**
     * Get the sub set
     * @param sset
     * @param start
     * @param end
     * @return
     */
    public Set<String> getSubSet(String sset, long start, long end) {
        return jedis.zrange(sset, start, end);
    }

    /**
     * Get the index of the member
     * @param sset
     * @param member
     * @return
     */
    public Double getIndex(String sset, String member) {
        return jedis.zscore(sset, member);
    }

    /**
     * Remove the members
     * @param sset
     * @param members
     * @return
     */
    public Long removeSSValues(String sset, String ...members) {
        return jedis.zrem(sset, members);
    }

    /**
     * Get all of the values of the sorted set
     * @param sset
     * @return
     */
    public Set<String> getAllSSValues(String sset) {
        return jedis.zrange(sset, 0, -1);
    }

    /**
     *
     * @param sset
     * @param start
     * @param end
     * @return
     */
    public Long countRange(String sset, double start, double end) {
        return jedis.zcount(sset, start, end);
    }

    ////////////////////Sorted Set Functions - End /////////////////////////////


    ////////////////////Hash Map Functions - Start /////////////////////////////
    /**
     * Push the value to the map on the key
     * @param map
     * @param key
     * @param value
     * @return
     */
    public long push(String map, String key, String value) {
        return jedis.hset(map, key, value);
    }

    /**
     * Identify whether the key exist or not
     * @param map
     * @param key
     * @return
     */
    public boolean hexists(String map, String key) {
        return jedis.hexists(map, key);
    }

    /**
     * Get the value of the key
     * @param map
     * @param key
     * @return
     */
    public String getValue(String map, String key) {
        return jedis.hget(map, key);
    }

    /**
     * Get the values of the keys
     *
     * @param map
     * @param keys
     * @return
     */
    public List<String> getHValues(String map, String... keys) {
        return jedis.hmget(map, keys);
    }

    /**
     * Remove the values by the keys
     * @param map
     * @param keys
     * @return
     */
    public Long removeHValues(String map, String ... keys) {
        return jedis.hdel(map, keys);
    }

    /**
     * Increment the value on the key for the map
     * @param map
     * @param key
     * @param value
     * @return
     */
    public Long increment(String map, String key, long value) {
        return jedis.hincrBy(map, key, value);
    }

    /**
     * Get all of the keys of the map
     * @param map
     * @return
     */
    public Set<String> getKeys(String map) {
        return jedis.hkeys(map);
    }

    /**
     * Get all of the values of the map
     * @param map
     * @return
     */
    public List<String> getValues(String map) {
        return jedis.hvals(map);
    }

    public Long expire(String key, int seconds){
        return jedis.expire(key,seconds);
    }

    ////////////////////Hash Map Functions - End //////////////////////////////
}

 測試類為:(copy了一個網上的例子)

public class testCacheManager {

    private @Resource
    CacheManager cacheManager = CacheManager.getInstance();

    @Before
    public void init() {
        System.out.println(cacheManager.hashCode() + "");
    }

    @Test
    public void basicTest() {
        System.out.println("============= Basic1 ==========================");

        // 清空資料
        System.out.println(cacheManager.clear());

        System.out.println(cacheManager.exists("josh"));

        // 儲存資料
        cacheManager.set("josh", "WangSheng");

        System.out.println(cacheManager.exists("josh"));

        System.out.println(cacheManager.getValue("josh"));

        System.out.println("============= Basic 2 ==========================");

        // 若key不存在,則儲存
        cacheManager.setnx("josh", "wang sheng");
        System.out.println(cacheManager.getValue("josh"));

        // 覆蓋資料
        cacheManager.set("josh", "wang sheng");
        System.out.println(cacheManager.getValue("josh"));

        // 追加資料
        cacheManager.append("josh", "Lily");
        System.out.println(cacheManager.getValue("josh"));

        System.out.println("============= Basic 3 ==========================");

        // 設定key的有效期,並存儲資料
        cacheManager.setKeyLive("josh", 2, "WangSheng-Lily");
        System.out.println(cacheManager.getValue("josh"));

        try {
            Thread.sleep(3000);
            System.out.println(cacheManager.getValue("josh"));
        } catch (InterruptedException e) {
            System.out.println("Josh released  ... now ^_^");
        }



        System.out.println("============= Basic 4 ==========================");

        // 獲取並更改資料
        cacheManager.getSet("josh", "wang sheng");
        System.out.println(cacheManager.getValue("josh"));

        System.out.println("============= Basic 5 ==========================");
        // 擷取value的值
        System.out.println(cacheManager.getRange("josh", 1, 3));

        cacheManager.set("MJ", "Jordan");

        System.out.println(cacheManager.getValues("josh", "MJ"));

        System.out.println("============= Basic 6 ==========================");
        cacheManager.removeValues(new String[]{"josh", "MJ"});

        System.out.println(cacheManager.getValues("josh", "MJ"));
    }

    /**
     * List 是無序的,所以測試結果和expect的結果可能不一致
     *
     * 還是是從-1開始?
     */
    @Test
    public void listTest() {
        System.out.println("============= list1 ==========================");
        // 清空資料
        System.out.println(cacheManager.clear());

        // 新增資料
        cacheManager.add2List("ball", "Jordan");
        cacheManager.add2List("ball", "Maddie");
        cacheManager.add2List("ball", "AI");
        cacheManager.add2List("ball", "Yao");

        // The order should be : Yao, AI, Maddie, Jordan

        // 陣列長度
        System.out.println(cacheManager.getListSize("ball"));

        System.out.println(cacheManager.getAllListValues("ball"));

        System.out.println(cacheManager.getListValues("ball", 0, -1));

        System.out.println("============= list2 ==========================");

        // 修改列表中單個值
        cacheManager.updateList("ball", 1, "Allen Iversen");
        System.out.println(cacheManager.getListValues("ball", 0, 3));

        // 獲取列表指定下標的值
        System.out.println(cacheManager.getIndexValue("ball", 2));

        // 刪除列表指定下標的值
        System.out.println(cacheManager.removeLValue("ball", 1, "Yao"));
        System.out.println(cacheManager.getAllListValues("ball"));

        // 刪除區間以外的資料
        System.out.println(cacheManager.removeOutterValue("ball", 0, 1));
        System.out.println(cacheManager.getAllListValues("ball"));

        // 列表出棧
        System.out.println(cacheManager.popList("ball"));
    }

    @Test
    public void testSet() {
        System.out.println("========================= Set ====================");

        // 清空資料
        System.out.println(cacheManager.clear());

        // 新增資料
        cacheManager.add2Set("ball", "Jordan");
        cacheManager.add2Set("ball", "Maddie");
        cacheManager.add2Set("ball", "AI");
        cacheManager.add2Set("ball", "Yao");

        // 判斷value是否在列表中
        System.out.println(cacheManager.exists("ball", "AI"));
        System.out.println(cacheManager.exists("ball", "Yao "));

        // 整個列表的值
        System.out.println(cacheManager.getAllSetValues("ball"));

        // 刪除指定的元素
        System.out.println(cacheManager.removeSValues("ball", "Yao"));

        // 出棧
        System.out.println(cacheManager.popSet("ball"));

        // 整個列表的值
        System.out.println(cacheManager.getAllSetValues("ball"));

        cacheManager.add2Set("bball", "Jordan");
        cacheManager.add2Set("bball", "Maddie");
        cacheManager.add2Set("bball", "AI");
        cacheManager.add2Set("bball", "Yao");

        cacheManager.add2Set("fball", "Jordan");
        cacheManager.add2Set("fball", "Ronaldo");
        cacheManager.add2Set("fball", "Rivaldo");
        cacheManager.add2Set("fball", "Cristiano Ronaldo");
        cacheManager.add2Set("fball", "Ronaldinho");

        // 交集
        System.out.println(cacheManager.intersection("bball", "fball"));

        // 並集
        System.out.println(cacheManager.union("bball", "fball"));

        // 差集
        System.out.println(cacheManager.diff("bball", "fball"));

    }

    @Test
    public void testHash() {
        System.out.println("=============  hash ==========================");

        // 清空資料
        System.out.println(cacheManager.clear());

        // 新增資料
        cacheManager.push("hball", "Jordan", "Chicago");
        cacheManager.push("hball", "Maddie", "Houston");
        cacheManager.push("hball", "AI", "Philadelphia");
        cacheManager.push("hball", "Yao", "Houston");

        // 判斷某個值是否存在
        System.out.println(cacheManager.hexists("hball", "Yao "));
        System.out.println(cacheManager.hexists("hball", "AI"));

        // 獲取指定的值
        System.out.println(cacheManager.getValue("hball", "Jordan"));

        // 批量獲取指定的值
        System.out.println(cacheManager.getHValues("hball", "Jordan", "Maddie"));

        // 刪除指定的值
        System.out.println(cacheManager.removeHValues("hball", "Yao"));

        // 為key中的域 field 的值加上增量 increment, hash value must be a data value
        // System.out.println(cacheManager.increment("hball", "Jordan", 123l));

        // 獲取所有的keys
        System.out.println(cacheManager.getKeys("hball"));

        // 獲取所有的values
        System.out.println(cacheManager.getValues("hball"));
    }


//    @Test
//    public void test(){
//        Jedis jedis = new Jedis("121.40.158.114",6379);
//        jedis.set("11","222");
//        jedis.expire("11",5);
//        System.out.println(jedis.get("11"));
//    }
//
//    @Test
//    public void test2(){
//        Jedis jedis = new Jedis("121.40.158.114",6379);
//        System.out.println(jedis.get("11"));
//    }
}

 輸出結果為:

1338823963
============= Basic1 ==========================
OK
false
true
WangSheng
============= Basic 2 ==========================
WangSheng
wang sheng
wang shengLily
============= Basic 3 ==========================
WangSheng-Lily
null
============= Basic 4 ==========================
wang sheng
============= Basic 5 ==========================
ang
[wang sheng, Jordan]
============= Basic 6 ==========================
[null, null]
1338823963
========================= Set ====================
OK
true
false
[Maddie, Jordan, Yao, AI]
1
Jordan
[Maddie, AI]
[Jordan]
[Jordan, Maddie, Ronaldo, Rivaldo, Cristiano Ronaldo, Yao, Ronaldinho, AI]
[AI, Yao, Maddie]
1338823963
=============  hash ==========================
OK
false
true
Chicago
[Chicago, Houston]
1
[Jordan, AI, Maddie]
[Chicago, Houston, Philadelphia]
1338823963
============= list1 ==========================
OK
4
[Yao, AI, Maddie, Jordan]
[Yao, AI, Maddie, Jordan]
============= list2 ==========================
[Yao, Allen Iversen, Maddie, Jordan]
Maddie
1
[Allen Iversen, Maddie, Jordan]
OK
[Allen Iversen, Maddie]
Allen Iversen

 存在redis資料庫的資料你可以通過redis-cli.exe來進行檢視:



 

相關推薦

Windowsredis執行搭建使用

redis的配置檔案為: redis.pool.maxActive=1024 redis.pool.maxIdle=200 redis.pool.maxWait=1000 redis.pool.testOnBorrow=false redis.pool.testOnReturn=true #

Redis執行分散式

在大型的網際網路網站中,單機的Redis往往是不支援大併發的,很多時候都需要分散式以及叢集來降低伺服器崩潰造成整個網站的停止執行,所以,Redis也是需要進行分散式以及叢集去防範這種現象的發生。 我們需要一些依賴: <span style="white-space:

總結java執行的處理策略

執行緒池 任務提交給執行緒池之後的處理策略: 如果當前執行緒池中的執行緒數目<corePoolSize,則每來一個任務,就會建立一個執行緒去執行這個任務; 如果當前執行緒池中的執行緒數目>=corePoolSize,則每來一個任務,會嘗試將其新增到任務快取隊列當中,若

Windows執行通訊

1. 首先我們想到的就是共享記憶體,比如說用全域性變數,各種event,但是用這種方式要記得加鎖; 2. 用訊息,但是要先建立訊息佇列;用SendMessage,PostThreadMessage之類的函式; 在接收訊息的執行緒寫:     

c++ 網路程式設計(九)TCP/IP LINUX/windows執行超詳細教程 以及 多執行實現服務端

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <process.h> #include <winsock2.h> #include <win

c++ 網路程式設計(九)TCP/IP LINUX/windows執行超詳細教程 以及 多執行實現服務端

原文作者:aircraft 原文連結:https://www.cnblogs.com/DOMLX/p/9661012.html  先講Linux下(windows下在後面可以直接跳到後面看): 一.執行緒基本概念 前面我們講過多程序伺服器,但我們知道它開銷很大

Java執行原理使用

java中的執行緒池是運用場景最多的併發框架。在開發過程中,合理的使用執行緒池能夠帶來下面的一些好處: 1、降低資源的消耗。 2、提高響應速度。 3、提高執行緒的可管理型。 1.1、執行緒池ThreadPoolExecutor工作原理 講解之前,我們先看一張原理

ThreadPool執行使用解決主執行和子執行執行順序問題

 執行緒池建立五個執行緒,每個執行緒往list中新增100個元素。synchronized只鎖執行緒共享變數list物件,程式碼段內僅新增元素及列印資訊。設定10ms睡眠時間給其餘執行緒機會。 ExecutorService fixedThreadPool = Execut

Java併發程式設計之深入執行原理實現

Java執行緒池在實際的應用開發中十分廣泛。雖然Java1.5之後在JUC包中提供了內建執行緒池可以拿來就用,但是這之前仍有許多老的應用和系統是需要程式設計師自己開發的。因此,基於執行緒池的需求背景、技術要求瞭解執行緒池原理和實現,一方面可以更為深刻理解Java多執行緒開發,有助於解決業務系統中因為執行緒問題

java 執行 Executors ThreadPoolExecutor

Java通過Executors提供四種執行緒池,分別為:  newCachedThreadPool建立一個可快取執行緒池,如果執行緒池長度超過處理需要,可靈活回收空閒執行緒,若無可回收,則新建執行緒。  newFixedThreadPool 建立一個定長執行緒池,可控制執行緒最大併發數,超出的執行緒會在佇列

Python 執行原理實現

概述 傳統多執行緒方案會使用“即時建立, 即時銷燬”的策略。儘管與建立程序相比,建立執行緒的時間已經大大的縮短,但是如果提交給執行緒的任務是執行時間較短,而且執行次數極其頻繁,那麼伺服器將處於不停的建立執行緒,銷燬執行緒的狀態。 一個執行緒的執行時間可以分為3部分:執行緒的啟動時間、執行緒體的執

java執行介紹簡單使用舉例

多執行緒雖然能夠提升程式的效能,但其實也是一把雙刃劍。"為每一個任務分配一個執行緒"的問題在於資源管理的複雜性。當我們需要頻繁的建立多個執行緒進行耗時操作時,每次通過new Thread來建立並不是一種好的辦法。new Thread 新建和銷燬物件的效能較差,執行緒缺乏統一

Java併發核心基礎——執行使用底層實現機制詳解

Java執行緒池概述: 從使用入手: java.util.concurrent.Executosr是執行緒池的靜態工廠,我們通常使用它方便地生產各種型別的執行緒池,主要的方法有三種: 1、newS

[C++][執行][完整實現] 轉:執行原理建立(C++實現)

文章的主要框架是參考這篇文件的,http://jacky-dai.iteye.com/blog/1090285, 關於作者  張中慶,西安交通大學軟體所,在讀碩士,目前研究方向為分散式網路與移動中介軟體,對Linux極其愛好,可以通過[email protecte

windows執行同步(利用事件物件,互斥物件,關鍵程式碼段)實現

一:利用事件實現執行緒同步 1.createthread函式的用法 hThread = CreateThread(&security_attributes, dwStackSize, ThreadProc,pParam, dwFlags, &idThre

WindowsRed5流媒體伺服器搭建使用(二)

搭建好流媒體伺服器之後,如何使用呢。 1,更改IP與埠。 進入red5-server\conf資料夾下,開啟red5.properties檔案 # HTTP http.host=172.16.129.178 http.port=3002 https.port=3003

windowsphp執行安全與非執行安全的版本選擇

Windows下的PHP版本分兩種:執行緒安全版本與非執行緒安全版本。 要論兩者的區別,詳細論說起來比較麻煩,從使用者的角度,記住什麼時候用哪種版本的區別就可以了吧: 1、windows + IIS + FastCGI :使用非執行緒安全版本。

執行淺析C++程式碼實現

(1)什麼是執行緒池  執行緒池是一種多執行緒處理技術。執行緒池先建立好若干執行緒,並管理這些執行緒。當有新的任務到來時,將任務新增到一個已建立的空閒執行緒中執行。執行緒池所建立的執行緒優先順序都是一樣的,所以需要使用特定執行緒優先順序的任務不宜使用執行緒池。   (2)

windows執行同步

互斥量(鎖)適用範圍:可以跨程序同步,還可以用來保證程式只有一個互斥鎖例項執行(建立命名互斥量),也可以用來做執行緒間的同步如果用於程序間同步,一個執行緒建立互斥量物件後,另一個程序只需要獲取互斥量就可以,可以用OpenMutex(MUTEX_ALL_ACCESS,FALSE

Java執行原理四種執行的使用

 一、執行緒池簡介:     多執行緒技術主要解決處理器單元內多個執行緒執行的問題,它可以顯著減少處理器單元的閒置時間,增加處理器單元的吞吐能力。        假設一個伺服器完成一項任務所需