2 zzg8696256 zzg8696256 于 2014.07.28 14:56 提问

如何写线程类的测试类?

package com.tywire.tadu.cache.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

import com.tywire.tadu.cache.key.FavoriteCache;
import com.tywire.tadu.cache.queue.opt.FavoriteOpt;
import com.tywire.tadu.cache.refresh.FavoriteRefresh;
import com.tywire.tadu.facade.FavoriteFacade;
import com.tywire.tadu.pojo.Favorite;
import com.tywire.tadu.redis.JedisHelper;
import com.tywire.tadu.utils.init.AppContext;
import com.tywire.tadu.utils.service.LogHelper;
import com.tywire.tadu.utils.thread.ThreadPoolManager;

/**

  • 收藏清缓存
    */
    public class FavoriteQ {

    private static FavoriteFacade favoriteFacade = (FavoriteFacade) AppContext.getInstance().getAppContext()
    .getBean("favoriteFacade");
    private static FavoriteRefresh favoriteRefresh = (FavoriteRefresh) AppContext.getInstance().getAppContext()
    .getBean("favoriteRefresh");

    private FavoriteQ() {
    }

    public static BlockingQueue q = new LinkedBlockingDeque();

    static {

    new Thread(new Queuelis()).start();
    

    }

    private static class Queuelis implements Runnable {

    /**
     * 添加收藏清缓存
     * @param opt FavoriteOpt类信息
     */
    private void add(FavoriteOpt opt) {
        // 1、增加单条进入缓存
        String cachekey = FavoriteCache.loadUserFavoritesKey(opt.getFavorite().getUserId());
        boolean cacheExist = JedisHelper.exists(cachekey);
        if (cacheExist) {
            JedisHelper.zadd(FavoriteCache.loadUserFavoritesKey(opt.getFavorite().getUserId()), opt.getFavorite()
                    .getTimeScore(), opt.getFavorite());
        }
        //else {// 如果缓存不存在,不做处理
        //  loadUserFavorites(favorite.getUserId());
        //}
    
        // 2、修改个数
        favoriteRefresh.refreshUserFavoriteSize(opt.getFavorite().getUserId());
    }
    
    /**
     * 更新收藏信息清缓存
     * @param opt 收藏信息
     */
    private void update(FavoriteOpt opt) {
        // 1、覆盖单条进入缓存
        String cachekey = FavoriteCache.loadUserFavoritesKey(opt.getFavorite().getUserId());
        boolean cacheExist = JedisHelper.exists(cachekey);
        if (cacheExist) {
            JedisHelper.zadd(FavoriteCache.loadUserFavoritesKey(opt.getFavorite().getUserId()), opt.getFavorite()
                    .getTimeScore(), opt.getFavorite());
        }
        //else {// 如果缓存不存在,不做处理
        //  loadUserFavorites(favorite.getUserId());
        //}
        // 修改单条,缓存改为
        // FavoriteRefresh.refreshGetFavoriteByUserAndBook(favorite.getUserId(), favorite.getBookId());
        favoriteRefresh.refreshFavoriteById(opt.getFavorite().getId());
    }
    
    /**
     * 加载用户收藏信息<br>系统调用
     * @param opt
     */
    private void load(FavoriteOpt opt) {
        favoriteRefresh.refreshUserFavorites(opt.getFavorite().getUserId());
    }
    
    /**
     * 删除收藏夹中的缓存信息
     * @param opt
     */
    private void deleteFavoritesByFolderId(FavoriteOpt opt) {
        /* 1、处理收藏夹中,收藏的缓存 */
        // 修改个数
        favoriteRefresh.refreshUserFavoriteSize(opt.getFavorite().getUserId());
        // 重新加载用户收藏数
        favoriteFacade.loadUserFavorites(opt.getFavorite().getUserId());
    }
    
    /**
     * 批量删除收藏信息 
     * @param opt 收藏信息
     */
    private void deleteFavoriteByIds(FavoriteOpt opt) {
        if (opt.getList() == null) {
            return;
        }
        // 如果列表缓存存在删除缓存
        String cachekey = FavoriteCache.loadUserFavoritesKey(opt.getFavorite().getUserId());
        boolean cacheExist = JedisHelper.exists(cachekey);
        if (cacheExist) {
            for (Favorite fav : opt.getList()) {
                JedisHelper.zrem(FavoriteCache.loadUserFavoritesKey(opt.getFavorite().getUserId()), fav);
                JedisHelper.del(FavoriteCache.getFavoriteByIdKey(fav.getId()));
            }
        }
        // 加载个数
        favoriteRefresh.refreshUserFavoriteSize(opt.getFavorite().getUserId());
    }
    
    private void deleteFavoriteByUserId(FavoriteOpt opt) {
        // 删除缓存
        JedisHelper.del(FavoriteCache.loadUserFavoritesKey(opt.getFavorite().getUserId()));
        // 修改个数
        JedisHelper.del(FavoriteCache.countUserFavoritesKey(opt.getFavorite().getUserId()));
        // 单条的不作出处理,等待其自动过期
    }
    
    /**
     * 执行所有缓存操作
     */
    @Override
    public void run() {
        for (;;) {
    
            try {
                final FavoriteOpt opt = q.take();
                ThreadPoolManager.es.execute(new Runnable() { // 执行所有缓存操作
                            @Override
                            public void run() {
                                if (opt.getOpt().equalsIgnoreCase("add")) { // 增加
                                    add(opt);
                                } else if (opt.getOpt().equalsIgnoreCase("update")) { // 修改
                                    update(opt);
                                } else if (opt.getOpt().equalsIgnoreCase("deleteFavoritesByFolderId")) { // 删除
                                    deleteFavoritesByFolderId(opt);
                                } else if (opt.getOpt().equalsIgnoreCase("deleteFavoriteByIds")) { // 删除
                                    deleteFavoriteByIds(opt);
                                } else if (opt.getOpt().equalsIgnoreCase("deleteFavoriteByUserId")) { // 删除
                                    deleteFavoriteByUserId(opt);
                                } else if (opt.getOpt().equalsIgnoreCase("load")) { // 加载
                                    load(opt);
                                }
    
                            }
                        });
            } catch (InterruptedException e) {
                LogHelper.exceptionLog(e);
            }
    
        }
    }
    

    }

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!