< prev index next >

test/jdk/java/lang/Thread/virtual/YieldQueuing.java

Print this page

 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 Test Thread.yield submits the virtual thread task to the expected queue
 27  * @requires vm.continuations
 28  * @run junit/othervm -Djdk.virtualThreadScheduler.maxPoolSize=1 YieldQueuing
 29  */
 30 

 31 import java.util.List;
 32 import java.util.concurrent.CopyOnWriteArrayList;
 33 import java.util.concurrent.atomic.AtomicBoolean;
 34 import java.util.concurrent.locks.LockSupport;
 35 
 36 import org.junit.jupiter.api.Test;

 37 import static org.junit.jupiter.api.Assertions.*;
 38 
 39 class YieldQueuing {
 40 






 41     /**
 42      * Test Thread.yield submits the task for the current virtual thread to a scheduler
 43      * submission queue when there are no tasks in the local queue.
 44      */
 45     @Test
 46     void testYieldWithEmptyLocalQueue() throws Exception {
 47         var list = new CopyOnWriteArrayList<String>();
 48 
 49         var threadsStarted = new AtomicBoolean();
 50 
 51         var threadA = Thread.ofVirtual().unstarted(() -> {
 52             // pin thread until task for B is in submission queue
 53             while (!threadsStarted.get()) {
 54                 Thread.onSpinWait();
 55             }
 56 
 57             list.add("A");
 58             Thread.yield();      // push task for A to submission queue, B should run
 59             list.add("A");
 60         });

 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 Test Thread.yield submits the virtual thread task to the expected queue
 27  * @requires vm.continuations
 28  * @run junit/othervm -Djdk.virtualThreadScheduler.maxPoolSize=1 YieldQueuing
 29  */
 30 
 31 import java.lang.invoke.MethodHandles;
 32 import java.util.List;
 33 import java.util.concurrent.CopyOnWriteArrayList;
 34 import java.util.concurrent.atomic.AtomicBoolean;
 35 import java.util.concurrent.locks.LockSupport;
 36 
 37 import org.junit.jupiter.api.Test;
 38 import org.junit.jupiter.api.BeforeAll;
 39 import static org.junit.jupiter.api.Assertions.*;
 40 
 41 class YieldQueuing {
 42 
 43     @BeforeAll
 44     static void setup() throws Exception {
 45         // waiting for LockSupport to be initialized can change the scheduling
 46         MethodHandles.lookup().ensureInitialized(LockSupport.class);
 47     }
 48 
 49     /**
 50      * Test Thread.yield submits the task for the current virtual thread to a scheduler
 51      * submission queue when there are no tasks in the local queue.
 52      */
 53     @Test
 54     void testYieldWithEmptyLocalQueue() throws Exception {
 55         var list = new CopyOnWriteArrayList<String>();
 56 
 57         var threadsStarted = new AtomicBoolean();
 58 
 59         var threadA = Thread.ofVirtual().unstarted(() -> {
 60             // pin thread until task for B is in submission queue
 61             while (!threadsStarted.get()) {
 62                 Thread.onSpinWait();
 63             }
 64 
 65             list.add("A");
 66             Thread.yield();      // push task for A to submission queue, B should run
 67             list.add("A");
 68         });
< prev index next >