package com.zhongzheng.modules.order.domain;

import com.zhongzheng.common.core.redis.RedisCache;
import com.zhongzheng.common.core.redis.RedisLockEntity;
import org.springframework.beans.factory.annotation.Autowired;

public class Printer implements Runnable {


     RedisCache redisCache;

    int id;
    public static int num = 1;

    public Printer(int id,RedisCache redisCache) {
        this.id = id;
        this.redisCache = redisCache;
    }

    @Override
    public void run() {
        while (num <= 20) {
            RedisLockEntity redisLockEntity = new RedisLockEntity();
            redisLockEntity.setLockKey(RedisLockEntity.KEY_LOCK_GRADE);
            redisLockEntity.setRequestId(id+"");
            System.out.print("线程");
            if(redisCache.lock(redisLockEntity)){
                System.out.print("报数id" + id + ":"+num+"\n");
                num++;

                try {
                    Thread.sleep(400);

                } catch (InterruptedException e) {
                    redisCache.unlockLua(redisLockEntity);
                }

                redisCache.unlockLua(redisLockEntity);
            }

        }
    }
}
