Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it really thread safe?! it behaves differently than ConcurrentHahMap. please run code below. #75

Open
abprojects055 opened this issue Nov 6, 2021 · 0 comments

Comments

@abprojects055
Copy link

abprojects055 commented Nov 6, 2021

import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class Main {

    public static void main(String[] args) {
        Data data = new Data();
        ExecutorService service = Executors.newFixedThreadPool(5);

        IntStream.range(0, 1000)
                .forEach(count -> service.submit(data::incrementBy));


    }

    public static class Data {

        private Map<Integer, Integer> map;

        public Data() {
//        this.map = new ConcurrentHashMap<>();
            this.map = ExpiringMap.builder()
                    .expiration(100, TimeUnit.MINUTES)
                    .expirationPolicy(ExpirationPolicy.CREATED).build();
            this.map.put(1, 0);
        }


        public void incrementBy() {
            this.map.computeIfPresent(1, (key, old) -> {
                System.out.println(old + 1);
                return old + 1;
            });
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant