1 // from ResourceScopeCloseMin.java 2 // https://bugs.openjdk.org/browse/JDK-8267532 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.concurrent.TimeUnit; 6 7 public class Example3 { 8 9 Runnable dummy = () -> {}; 10 11 static class ConfinedScope { 12 final Thread owner; 13 boolean closed; 14 final List<Runnable> resources = new ArrayList<>(); 15 //int [] weight = new int[1024]; 16 //final Runnable[] resources = new Runnable[1]; 17 18 private void checkState() { 19 if (closed) { 20 throw new AssertionError("Closed"); 21 } else if (owner != Thread.currentThread()) { 22 throw new AssertionError("Wrong thread"); 23 } 24 } 25 26 ConfinedScope() { 27 this.owner = Thread.currentThread(); 28 } 29 30 void addCloseAction(Runnable runnable) { 31 checkState(); 32 resources.add(runnable); 33 //resources[0] = runnable; 34 } 35 36 public void close() { 37 checkState(); 38 closed = true; 39 for (Runnable r : resources) { 40 r.run(); 41 } 42 } 43 } 44 45 public void confined_close() { 46 ConfinedScope scope = new ConfinedScope(); 47 try { // simulate TWR 48 scope.addCloseAction(dummy); 49 scope.close(); 50 } catch (RuntimeException ex) { 51 scope.close(); 52 throw ex; 53 } 54 } 55 56 public static void main(String[] args) { 57 var kase = new Example3(); 58 59 while (true) { 60 kase.confined_close(); 61 } 62 } 63 }