Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

76
LINES

< > BotCompany Repo | #1012172 // Bench concurrency-safely iterating TreeMap faster with interference [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (1734L/11K).

1  
!7
2  
3  
static int n = 1000000;
4  
sbool done;
5  
6  
// iterate safely (& quickly) in the face of concurrent modifications
7  
// is now called concurrentlyIterateKeys in the library
8  
static <A, B> IterableIterator<A> iterateKeysQuickly(final NavigableMap<A, B> map) {
9  
  ret iteratorFromFunction(new F0<A>() {
10  
    Iterator<A> it = keys(map).iterator();
11  
    A key;
12  
    
13  
    A get() {
14  
      synchronized(map) {
15  
        try {
16  
          if (!it.hasNext()) null;
17  
          ret key = it.next();
18  
        } catch (ConcurrentModificationException e) {
19  
          print("Re-iterating");
20  
          it = map.tailMap(key, false).keySet().iterator();
21  
          if (!it.hasNext()) null;
22  
          ret key = it.next(); // Can't throw another exception
23  
        }
24  
      }
25  
    }
26  
  });
27  
}
28  
29  
p {
30  
  final NavigableMap<S, Bool> map = synchroNavigableMap(litcimap());
31  
  final new L<S> ids;
32  
  repeat n {
33  
    map.put(addAndReturn(ids, randomID()), Bool.TRUE);
34  
  }
35  
  
36  
  long sum = 0;
37  
  repeat 5 {
38  
    int i = 0;
39  
    sum = 0;
40  
    time "Iterating" {
41  
      for (S key : keys(map)) {
42  
        if (!key.startsWith("_")) {
43  
          ++i;
44  
          sum += key.hashCode();
45  
        }
46  
      }
47  
    }
48  
    assertEquals(i, n);
49  
  }
50  
51  
  thread "Interfere" {
52  
    repeat with ms sleep 10 {
53  
      if (done) ret;
54  
      synchronized(map) {
55  
        map.put("_" + randomID(), Bool.TRUE);
56  
      }
57  
    }
58  
  }
59  
  
60  
  repeat 5 {
61  
    int i = 0;
62  
    long sum2 = 0;
63  
    time "Iterating quickly" {
64  
      for (S key : iterateKeysQuickly(map)) {
65  
        if (!key.startsWith("_")) {
66  
          ++i;
67  
          sum2 += key.hashCode();
68  
        }
69  
      }
70  
    }
71  
    assertEquals(i, n);
72  
    assertEquals(sum, sum2);
73  
  }
74  
  
75  
  done = true;
76  
}

Author comment

Began life as a copy of #1012169

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1012172
Snippet name: Bench concurrency-safely iterating TreeMap faster with interference [OK]
Eternal ID of this version: #1012172/7
Text MD5: 5b386502410a4e4dc306af43383e7097
Transpilation MD5: 744845ef2b81cc992f6ea18302c24cbd
Author: stefan
Category: javax / collections
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-11-23 11:21:40
Source code size: 1721 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 522 / 1158
Version history: 6 change(s)
Referenced in: [show references]