< prev index next >

test/jdk/javax/security/auth/Subject/CallAsWithScopedValue.java

Print this page

 48         Subject subject = new Subject();
 49         subject.getPrincipals().add(new UserPrincipal("Duke"));
 50 
 51         // Always observable in the same thread
 52         Subject.callAs(subject, () -> check(0, Subject.current(), "Duke"));
 53 
 54         // Observable in a new platform thread in ACC mode, but not in the SV mode
 55         Subject.callAs(subject, () -> {
 56             Thread.ofPlatform().start(() -> check(1, Subject.current(), usv ? null : "Duke")).join();
 57             return null;
 58         });
 59 
 60         // Never observable in a new virtual thread
 61         Subject.callAs(subject, () -> {
 62             Thread.ofVirtual().start(() -> check(2, Subject.current(), null)).join();
 63             return null;
 64         });
 65 
 66         // Observable in structured concurrency in SV mode, but not in ACC mode
 67         Subject.callAs(subject, () -> {
 68             try (var scope = new StructuredTaskScope<>()) {

 69                 scope.fork(() -> check(3, Subject.current(), usv ? "Duke" : null));
 70                 scope.join();
 71             }
 72             return null;
 73         });
 74 
 75         // Suggested way to pass the current subject into arbitrary
 76         // threads. Grab one using current() and explicitly pass it
 77         // into the new thread.
 78         Subject.callAs(subject, () -> {
 79             Subject current = Subject.current();
 80             Thread.ofPlatform().start(() -> {
 81                 Subject.callAs(current, () -> check(4, Subject.current(), "Duke"));
 82             }).join();
 83             return null;
 84         });
 85 
 86         if (results.size() != 5 || results.containsValue(false)) {
 87             System.out.println(results);
 88             throw new RuntimeException("Failed");

 48         Subject subject = new Subject();
 49         subject.getPrincipals().add(new UserPrincipal("Duke"));
 50 
 51         // Always observable in the same thread
 52         Subject.callAs(subject, () -> check(0, Subject.current(), "Duke"));
 53 
 54         // Observable in a new platform thread in ACC mode, but not in the SV mode
 55         Subject.callAs(subject, () -> {
 56             Thread.ofPlatform().start(() -> check(1, Subject.current(), usv ? null : "Duke")).join();
 57             return null;
 58         });
 59 
 60         // Never observable in a new virtual thread
 61         Subject.callAs(subject, () -> {
 62             Thread.ofVirtual().start(() -> check(2, Subject.current(), null)).join();
 63             return null;
 64         });
 65 
 66         // Observable in structured concurrency in SV mode, but not in ACC mode
 67         Subject.callAs(subject, () -> {
 68             var joiner = StructuredTaskScope.Joiner.awaitAll();
 69             try (var scope = StructuredTaskScope.open(joiner)) {
 70                 scope.fork(() -> check(3, Subject.current(), usv ? "Duke" : null));
 71                 scope.join();
 72             }
 73             return null;
 74         });
 75 
 76         // Suggested way to pass the current subject into arbitrary
 77         // threads. Grab one using current() and explicitly pass it
 78         // into the new thread.
 79         Subject.callAs(subject, () -> {
 80             Subject current = Subject.current();
 81             Thread.ofPlatform().start(() -> {
 82                 Subject.callAs(current, () -> check(4, Subject.current(), "Duke"));
 83             }).join();
 84             return null;
 85         });
 86 
 87         if (results.size() != 5 || results.containsValue(false)) {
 88             System.out.println(results);
 89             throw new RuntimeException("Failed");
< prev index next >