synchronized同步对象锁

package com.system.util;

import com.common.Constants;
import com.util.Cache;


/**
 * 创建同步对象锁
 * 
 * @author jqlin
 *
 */
public class SyncObject {
    /**唯一对象*/
    private Object obj;

    public SyncObject(Object obj) {
        this.obj = obj;
    }
 
    /**
     * 获取同步对象锁
     * 
     * @param cache 缓存对象
     * @param preCacheKey 唯一标识缓存的ID
     * @param obj 唯一对象
     * @return
     */
    public static synchronized SyncObject getSyncObject(Cache cache, String preCacheKey, Object obj) {
        SyncObject sysObject = cache.get(preCacheKey + obj);
        if (sysObject == null) {
            sysObject = new SyncObject(obj);
            cache.put(preCacheKey + obj, sysObject);
        }
        
        return sysObject;
    }

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }
    
    public static void main(String[] args) {
        Cache cache = null;
        String userId = null;
        
        SyncObject syncOrder = SyncObject.getSyncObject(cache, Constants.DQ_REDEMP_CACHE, userId);
        synchronized (syncOrder) {
            //todo
        }
    }
}

 

posted on 2016-06-16 10:50  Ruthless  阅读(2366)  评论(0编辑  收藏  举报