Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2021
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. public class concurrent {
  2. volatile static int x;
  3. public static void test(ExecutorService pool) throws Exception{
  4. x = 1;
  5. Future<Integer> f1 = pool.submit(() -> x = x + 1);
  6. Future<Integer> f2 = pool.submit(() -> x = x * 5);
  7. f2.get();
  8. f1.get();
  9. }
  10. public static void main(String[] args) throws Exception {
  11. Set<Integer> set = new HashSet<>();
  12. ExecutorService pool = Executors.newFixedThreadPool(3);
  13. for(int i = 0 ;i < 100_000_000; i++){
  14. test(pool);
  15. set.add(x);
  16. }
  17. for(Integer e:set){
  18. System.out.println(e);
  19. }
  20. pool.shutdown();
  21. }
  22.  
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement