Maison  >  Questions et réponses  >  le corps du texte

Concernant l'inadéquation des types de paramètres causée par l'interface de conception générique Java

1. Conception d'une interface pour envelopper d'autres pojos afin de calculer s'ils sont expirés

public interface CatchWrapper<T>{
    public long getCatchedTime();
    
    public T getValue();
    
    public boolean valid();
}

Une certaine mise en œuvre :

public class DeviceCatchWrapper implements CatchWrapper<Device> {
    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;
    }

}

Il existe également une classe de gestion, principalement pour supprimer les caches expirés

public class DeviceCatchWrapperManager<T> {
    private static final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    private final ConcurrentMap<String, CatchWrapper<T>> catchStore;

    private final long initialDelay;
    private final long delay;
    private TimeUnit unit;

    private volatile boolean stop = false;

    public DeviceCatchWrapperManager(ConcurrentMap<String,CatchWrapper<T>> 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<String, CatchWrapper<T>> entry : catchStore.entrySet()) {
                    if (stop)
                        break;
                    String key = entry.getKey();
                    CatchWrapper<T> 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();
    }
}

Mais le vrai constructeur signale une erreur lors du passage des paramètres

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

下面的报错,参数不对
private final DeviceCatchWrapperManager<Device> catchManager = new DeviceCatchWrapperManager<Device>(catchMap, 2, 2, TimeUnit.HOURS);

Comment résoudre cette erreur ou comment concevoir l'interface ou l'améliorer ?

我想大声告诉你我想大声告诉你2713 Il y a quelques jours513

répondre à tous(1)je répondrai

  • 天蓬老师

    天蓬老师2017-05-17 10:02:20

    ConcurrentMap<String, DeviceCatchWrapper> catchMap = new ConcurrentHashMap<>(); Il y a un problème avec cette phrase
    Changer pour ConcurrentMap<String, CatchWrapper<Device>> Carte& lt; String, DeviceCatchWrapper >();Essayez-le

    répondre
    0
  • Annulerrépondre