< prev index next >

test/jdk/java/lang/Thread/BuilderTest.java

Print this page

  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /**
 25  * @test
 26  * @summary Unit test for Thread.Builder
 27  * @enablePreview
 28  * @run junit BuilderTest
 29  */
 30 
 31 import java.util.concurrent.*;
 32 import java.util.concurrent.atomic.AtomicBoolean;
 33 import java.util.concurrent.atomic.AtomicReference;
 34 import java.util.concurrent.locks.LockSupport;
 35 
 36 import org.junit.jupiter.api.Test;
 37 import static org.junit.jupiter.api.Assertions.*;
 38 import static org.junit.jupiter.api.Assumptions.*;
 39 
 40 class BuilderTest {
 41 
 42     /**
 43      * Test Thread.ofPlatform to create platform threads.
 44      */
 45     @Test
 46     void testPlatformThread() throws Exception {
 47         Thread parent = Thread.currentThread();

556         thread3.start();
557         thread3.join();
558         assertTrue(done.get());
559     }
560 
561     /**
562      * Test Thread.Builder creates threads that allow thread locals.
563      */
564     @Test
565     void testThreadLocals1() throws Exception {
566         Thread.Builder builder = Thread.ofPlatform();
567         testThreadLocals(builder);
568     }
569 
570     @Test
571     void testThreadLocals2() throws Exception {
572         Thread.Builder builder = Thread.ofVirtual();
573         testThreadLocals(builder);
574     }
575 
576     /**
577      * Test Thread.Builder creating threads that disallow or allow
578      * thread locals.
579      */
580     @Test
581     void testThreadLocals3() throws Exception {
582         Thread.Builder builder = Thread.ofPlatform();
583 
584         // disallow
585         builder.allowSetThreadLocals(false);
586         testNoThreadLocals(builder);
587 
588         // allow
589         builder.allowSetThreadLocals(true);
590         testThreadLocals(builder);
591     }
592 
593     @Test
594     void testThreadLocals4() throws Exception {
595         Thread.Builder builder = Thread.ofVirtual();
596 
597         // disallow
598         builder.allowSetThreadLocals(false);
599         testNoThreadLocals(builder);
600 
601         // allow
602         builder.allowSetThreadLocals(true);
603         testThreadLocals(builder);
604     }
605 
606     /**
607      * Tests that a builder creates threads that inherits the initial values of
608      * inheritable thread locals.
609      */
610     private void testInheritedThreadLocals(Thread.Builder builder) throws Exception {
611         Object value = new Object();
612         INHERITED_LOCAL.set(value);
613 
614         AtomicBoolean done = new AtomicBoolean();
615         Runnable task = () -> {
616             assertTrue(INHERITED_LOCAL.get() == value);
617             done.set(true);
618         };
619 
620         done.set(false);
621         Thread thread1 = builder.unstarted(task);
622         thread1.start();
623         thread1.join();
624         assertTrue(done.get());
625 

682 
683         // inherit
684         builder.inheritInheritableThreadLocals(true);
685         testInheritedThreadLocals(builder);
686     }
687 
688     @Test
689     void testInheritedThreadLocals2() throws Exception {
690         Thread.Builder builder = Thread.ofVirtual();
691         testInheritedThreadLocals(builder); // default
692 
693         // do no inherit
694         builder.inheritInheritableThreadLocals(false);
695         testNoInheritedThreadLocals(builder);
696 
697         // inherit
698         builder.inheritInheritableThreadLocals(true);
699         testInheritedThreadLocals(builder);
700     }
701 
702     @Test
703     void testInheritedThreadLocals3() throws Exception {
704         Thread.Builder builder = Thread.ofPlatform();
705 
706         // thread locals not allowed
707         builder.allowSetThreadLocals(false);
708         testNoInheritedThreadLocals(builder);
709         builder.inheritInheritableThreadLocals(false);
710         testNoInheritedThreadLocals(builder);
711         builder.inheritInheritableThreadLocals(true);
712         testNoInheritedThreadLocals(builder);
713 
714         // thread locals allowed
715         builder.allowSetThreadLocals(true);
716         builder.inheritInheritableThreadLocals(false);
717         testNoInheritedThreadLocals(builder);
718         builder.inheritInheritableThreadLocals(true);
719         testInheritedThreadLocals(builder);
720     }
721 
722     @Test
723     void testInheritedThreadLocals4() throws Exception {
724         Thread.Builder builder = Thread.ofVirtual();
725 
726         // thread locals not allowed
727         builder.allowSetThreadLocals(false);
728         testNoInheritedThreadLocals(builder);
729         builder.inheritInheritableThreadLocals(false);
730         testNoInheritedThreadLocals(builder);
731         builder.inheritInheritableThreadLocals(true);
732         testNoInheritedThreadLocals(builder);
733 
734         // thread locals allowed
735         builder.allowSetThreadLocals(true);
736         builder.inheritInheritableThreadLocals(false);
737         testNoInheritedThreadLocals(builder);
738         builder.inheritInheritableThreadLocals(true);
739         testInheritedThreadLocals(builder);
740     }
741 
742     /**
743      * Tests a builder creates threads that inherit the context class loader.
744      */
745     private void testInheritContextClassLoader(Thread.Builder builder) throws Exception {
746         ClassLoader savedCCL = Thread.currentThread().getContextClassLoader();
747         try {
748             ClassLoader loader = new ClassLoader() { };
749             Thread.currentThread().setContextClassLoader(loader);
750 
751             var ref = new AtomicReference<ClassLoader>();
752             Runnable task = () -> {
753                 ref.set(Thread.currentThread().getContextClassLoader());
754             };
755 
756             // unstarted
757             Thread thread1 = builder.unstarted(task);
758             assertTrue(thread1.getContextClassLoader() == loader);
759 
760             // started
761             ref.set(null);

833 
834         // inherit
835         builder.inheritInheritableThreadLocals(true);
836         testInheritContextClassLoader(builder);
837     }
838 
839     @Test
840     void testContextClassLoader2() throws Exception {
841         Thread.Builder builder = Thread.ofVirtual();
842         testInheritContextClassLoader(builder); // default
843 
844         // do no inherit
845         builder.inheritInheritableThreadLocals(false);
846         testNoInheritContextClassLoader(builder);
847 
848         // inherit
849         builder.inheritInheritableThreadLocals(true);
850         testInheritContextClassLoader(builder);
851     }
852 
853     @Test
854     void testContextClassLoader3() throws Exception {
855         Thread.Builder builder = Thread.ofPlatform();
856 
857         // thread locals not allowed
858         builder.allowSetThreadLocals(false);
859         testNoInheritContextClassLoader(builder);
860         builder.inheritInheritableThreadLocals(false);
861         testNoInheritContextClassLoader(builder);
862         builder.inheritInheritableThreadLocals(true);
863         testNoInheritContextClassLoader(builder);
864 
865         // thread locals allowed
866         builder.allowSetThreadLocals(true);
867         builder.inheritInheritableThreadLocals(false);
868         testNoInheritContextClassLoader(builder);
869         builder.inheritInheritableThreadLocals(true);
870         testInheritContextClassLoader(builder);
871     }
872 
873     @Test
874     void testContextClassLoader4() throws Exception {
875         Thread.Builder builder = Thread.ofVirtual();
876 
877         // thread locals not allowed
878         builder.allowSetThreadLocals(false);
879         testNoInheritContextClassLoader(builder);
880         builder.inheritInheritableThreadLocals(false);
881         testNoInheritContextClassLoader(builder);
882         builder.inheritInheritableThreadLocals(true);
883         testNoInheritContextClassLoader(builder);
884 
885         // thread locals allowed
886         builder.allowSetThreadLocals(true);
887         builder.inheritInheritableThreadLocals(false);
888         testNoInheritContextClassLoader(builder);
889         builder.inheritInheritableThreadLocals(true);
890         testInheritContextClassLoader(builder);
891     }
892 
893     /**
894      * Test NullPointerException.
895      */
896     @Test
897     void testNulls1() {
898         Thread.Builder.OfPlatform builder = Thread.ofPlatform();
899         assertThrows(NullPointerException.class, () -> builder.group(null));
900         assertThrows(NullPointerException.class, () -> builder.name(null));
901         assertThrows(NullPointerException.class, () -> builder.name(null, 0));
902         assertThrows(NullPointerException.class, () -> builder.uncaughtExceptionHandler(null));
903         assertThrows(NullPointerException.class, () -> builder.unstarted(null));
904         assertThrows(NullPointerException.class, () -> builder.start(null));
905     }
906 
907     @Test
908     void testNulls2() {
909         Thread.Builder builder = Thread.ofVirtual();
910         assertThrows(NullPointerException.class, () -> builder.name(null));
911         assertThrows(NullPointerException.class, () -> builder.name(null, 0));
912         assertThrows(NullPointerException.class, () -> builder.uncaughtExceptionHandler(null));

  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /**
 25  * @test
 26  * @summary Unit test for Thread.Builder

 27  * @run junit BuilderTest
 28  */
 29 
 30 import java.util.concurrent.*;
 31 import java.util.concurrent.atomic.AtomicBoolean;
 32 import java.util.concurrent.atomic.AtomicReference;
 33 import java.util.concurrent.locks.LockSupport;
 34 
 35 import org.junit.jupiter.api.Test;
 36 import static org.junit.jupiter.api.Assertions.*;
 37 import static org.junit.jupiter.api.Assumptions.*;
 38 
 39 class BuilderTest {
 40 
 41     /**
 42      * Test Thread.ofPlatform to create platform threads.
 43      */
 44     @Test
 45     void testPlatformThread() throws Exception {
 46         Thread parent = Thread.currentThread();

555         thread3.start();
556         thread3.join();
557         assertTrue(done.get());
558     }
559 
560     /**
561      * Test Thread.Builder creates threads that allow thread locals.
562      */
563     @Test
564     void testThreadLocals1() throws Exception {
565         Thread.Builder builder = Thread.ofPlatform();
566         testThreadLocals(builder);
567     }
568 
569     @Test
570     void testThreadLocals2() throws Exception {
571         Thread.Builder builder = Thread.ofVirtual();
572         testThreadLocals(builder);
573     }
574 






























575     /**
576      * Tests that a builder creates threads that inherits the initial values of
577      * inheritable thread locals.
578      */
579     private void testInheritedThreadLocals(Thread.Builder builder) throws Exception {
580         Object value = new Object();
581         INHERITED_LOCAL.set(value);
582 
583         AtomicBoolean done = new AtomicBoolean();
584         Runnable task = () -> {
585             assertTrue(INHERITED_LOCAL.get() == value);
586             done.set(true);
587         };
588 
589         done.set(false);
590         Thread thread1 = builder.unstarted(task);
591         thread1.start();
592         thread1.join();
593         assertTrue(done.get());
594 

651 
652         // inherit
653         builder.inheritInheritableThreadLocals(true);
654         testInheritedThreadLocals(builder);
655     }
656 
657     @Test
658     void testInheritedThreadLocals2() throws Exception {
659         Thread.Builder builder = Thread.ofVirtual();
660         testInheritedThreadLocals(builder); // default
661 
662         // do no inherit
663         builder.inheritInheritableThreadLocals(false);
664         testNoInheritedThreadLocals(builder);
665 
666         // inherit
667         builder.inheritInheritableThreadLocals(true);
668         testInheritedThreadLocals(builder);
669     }
670 








































671     /**
672      * Tests a builder creates threads that inherit the context class loader.
673      */
674     private void testInheritContextClassLoader(Thread.Builder builder) throws Exception {
675         ClassLoader savedCCL = Thread.currentThread().getContextClassLoader();
676         try {
677             ClassLoader loader = new ClassLoader() { };
678             Thread.currentThread().setContextClassLoader(loader);
679 
680             var ref = new AtomicReference<ClassLoader>();
681             Runnable task = () -> {
682                 ref.set(Thread.currentThread().getContextClassLoader());
683             };
684 
685             // unstarted
686             Thread thread1 = builder.unstarted(task);
687             assertTrue(thread1.getContextClassLoader() == loader);
688 
689             // started
690             ref.set(null);

762 
763         // inherit
764         builder.inheritInheritableThreadLocals(true);
765         testInheritContextClassLoader(builder);
766     }
767 
768     @Test
769     void testContextClassLoader2() throws Exception {
770         Thread.Builder builder = Thread.ofVirtual();
771         testInheritContextClassLoader(builder); // default
772 
773         // do no inherit
774         builder.inheritInheritableThreadLocals(false);
775         testNoInheritContextClassLoader(builder);
776 
777         // inherit
778         builder.inheritInheritableThreadLocals(true);
779         testInheritContextClassLoader(builder);
780     }
781 








































782     /**
783      * Test NullPointerException.
784      */
785     @Test
786     void testNulls1() {
787         Thread.Builder.OfPlatform builder = Thread.ofPlatform();
788         assertThrows(NullPointerException.class, () -> builder.group(null));
789         assertThrows(NullPointerException.class, () -> builder.name(null));
790         assertThrows(NullPointerException.class, () -> builder.name(null, 0));
791         assertThrows(NullPointerException.class, () -> builder.uncaughtExceptionHandler(null));
792         assertThrows(NullPointerException.class, () -> builder.unstarted(null));
793         assertThrows(NullPointerException.class, () -> builder.start(null));
794     }
795 
796     @Test
797     void testNulls2() {
798         Thread.Builder builder = Thread.ofVirtual();
799         assertThrows(NullPointerException.class, () -> builder.name(null));
800         assertThrows(NullPointerException.class, () -> builder.name(null, 0));
801         assertThrows(NullPointerException.class, () -> builder.uncaughtExceptionHandler(null));
< prev index next >