class ContextExecutorService {

    protected void attachToContext(final UUID contextUuid, final Future<?> future) {
        final List<Future<?>> futures = getFuturesOfContext(contextUuid);
        futures.add(future);
        if(log.isDebugEnabled())log.debug("Task is attached to context " + contextUuid);
        executor.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    future.get();
                } catch (Exception e) {
                    if(log.isDebugEnabled()) log.debug("Task execution is interrupted. Context " + contextUuid);
                    // do nothing. This future should be removed from a list in finally block. Exception doesn't matter
                } finally {
                    futures.remove(future);
                    if(log.isDebugEnabled())log.debug("Task is detached from a context " + contextUuid);
                }
                if(futures.isEmpty()){
                    synchronized (tasks){
                        if(futures.isEmpty()){
                            tasks.remove(contextUuid);
                        }
                    }
                }
            }
        });
    }

}