java 泛型接口参数类型_关于java 泛型设计接口 导致的参数类型不匹配问题

1.设计了一个接口用于包装其它 pojo,以计算是否过期

public interface CatchWrapper{

public long getCatchedTime();

public T getValue();

public boolean valid();

}

某一个实现:

public class DeviceCatchWrapper implements CatchWrapper {

private final long catchedTime;

private final Device device;

private static final long CATCH_TIME = 20*1000;

public DeviceCatchWrapper(Device device) {

this.device = device;

catchedTime = System.currentTimeMillis();

}

@Override

public long getCatchedTime() {

return catchedTime;

}

@Override

public Device getValue() {

return device;

}

@Override

public boolean valid() {

return System.currentTimeMillis() - catchedTime < CATCH_TIME;

}

}

另有一个管理类,主要是删除过期的缓存

public class DeviceCatchWrapperManager {

private static final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

private final ConcurrentMap> catchStore;

private final long initialDelay;

private final long delay;

private TimeUnit unit;

private volatile boolean stop = false;

public DeviceCatchWrapperManager(ConcurrentMap> catchStore, long initialDelay,

long delay, TimeUnit unit) {

this.catchStore = catchStore;

this.initialDelay = initialDelay;

this.delay = delay;

this.unit = unit;

}

/**

* 周期性检查过期的缓存,然后删除

*/

public void startLoop() {

service.scheduleWithFixedDelay(new Runnable() {

@Override

public void run() {

for (Entry> entry : catchStore.entrySet()) {

if (stop)

break;

String key = entry.getKey();

CatchWrapper cw = entry.getValue();

if (!cw.valid()){

System.out.println("Device catch manager --------------->remove:"+key);

catchStore.remove(key, cw);

}

}

}

}, initialDelay, delay, unit);

}

/**

* 停在对缓存进行过期检查

*/

public void stop() {

stop = true;

service.shutdownNow();

}

}

但是真正构造函数 传参数报错

private final ConcurrentMap catchMap = new ConcurrentHashMap<>();

下面的报错,参数不对

private final DeviceCatchWrapperManager catchManager = new DeviceCatchWrapperManager(catchMap, 2, 2, TimeUnit.HOURS);

改怎么解决这个错误 或者 该怎么设计接口或者改进呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值