#259132: Ready for multi-threaded access to observables ConcurrentComputed259132
authorJaroslav Tulach <jtulach@netbeans.org>
Wed, 11 May 2016 06:22:49 +0200
branchConcurrentComputed259132
changeset 10929329156bb5d4
parent 1091 9c7413573b98
child 1093 7d3990311255
#259132: Ready for multi-threaded access to observables
json/src/main/java/org/netbeans/html/json/spi/Observers.java
     1.1 --- a/json/src/main/java/org/netbeans/html/json/spi/Observers.java	Wed May 11 06:21:52 2016 +0200
     1.2 +++ b/json/src/main/java/org/netbeans/html/json/spi/Observers.java	Wed May 11 06:22:49 2016 +0200
     1.3 @@ -73,7 +73,9 @@
     1.4          synchronized (GLOBAL) {
     1.5              for (Watcher w : GLOBAL) {
     1.6                  if (w.proto == p) {
     1.7 -                    throw new IllegalStateException("Re-entrant attempt to access " + p);
     1.8 +                    if (w.owner == Thread.currentThread()) {
     1.9 +                        throw new IllegalStateException("Re-entrant attempt to access " + p);
    1.10 +                    }
    1.11                  }
    1.12              }
    1.13          }        
    1.14 @@ -91,13 +93,21 @@
    1.15      
    1.16      static void finishComputing(Proto p) {
    1.17          synchronized (GLOBAL) {
    1.18 -            Watcher w = GLOBAL.pop();
    1.19 -            if (w.proto != p) {
    1.20 -                throw new IllegalStateException("Inconsistency: " + w.proto + " != " + p);
    1.21 +            boolean found = false;
    1.22 +            Iterator<Watcher> it = GLOBAL.iterator();
    1.23 +            while (it.hasNext()) {
    1.24 +                Watcher w = it.next();
    1.25 +                if (w.proto == p && w.owner == Thread.currentThread()) {
    1.26 +                    if (w.prop != null) {
    1.27 +                        Observers mine = p.observers(true);
    1.28 +                        mine.add(w);
    1.29 +                    }
    1.30 +                    found = true;
    1.31 +                    it.remove();
    1.32 +                }
    1.33              }
    1.34 -            if (w.prop != null) {
    1.35 -                Observers mine = p.observers(true);
    1.36 -                mine.add(w);
    1.37 +            if (!found) {
    1.38 +                throw new IllegalStateException("Cannot find " + p + " in " + GLOBAL);
    1.39              }
    1.40          }
    1.41      }
    1.42 @@ -205,10 +215,12 @@
    1.43      }
    1.44      
    1.45      private static final class Watcher {
    1.46 +        final Thread owner;
    1.47          final Proto proto;
    1.48          final String prop;
    1.49  
    1.50          Watcher(Proto proto, String prop) {
    1.51 +            this.owner = Thread.currentThread();
    1.52              this.proto = proto;
    1.53              this.prop = prop;
    1.54          }