задача Описание
При использовании виртуальных потоков, вызывая Thread.slep () перед Locksupport.parknanos () вызывает последующие паркинги, чтобы вернуть немедленно без Blocing. В приведенном ниже воспроизведении: < /p>
[*] a виртуальная нить < /strong> и a поток платформы < /strong> оба:
- Два последовательных Parknanos (9_000_000_000) < /code> вызывает каждую секунду) < /li>
un -natems. Второй < /li>
< /ul>
< /li>
< /ol>
Ожидаемое поведение: < /strong>
Обе потоки должны сообщать ~ 2000 мс на итерацию (каждый парк () < /code> после ~ 1, общее количество 2S для двух парков). Поведение:
Платформа. Поток: правильно блокируется (~ 2000 мс/итерация)
-
Виртуальная нить: только блокирует ~ 1000 мс/итерация - parknos () ~ 1000 мс/итатерация - parknos () . Sleep ()
public class Main {
public static void main(String[] args) {
Thread thread1 = Thread.startVirtualThread(() -> {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
//boolean a = 9_000_000_000L==9 * 1000 * 1000000L;
long startNanos_25_15 = System.nanoTime();
LockSupport.parkNanos(9 * 1000 * 1000000L);
LockSupport.parkNanos(9 * 1000 * 1000000L);
long endNanos_25_17 = System.nanoTime();
System.out.println("Virtual thread: " + ((endNanos_25_17 - startNanos_25_15) / 1000000.0));
}
});
Thread thread2 = new Thread(() -> {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
long startNanos_25_15 = System.nanoTime();
LockSupport.parkNanos(9 * 1000 * 1000000L);
LockSupport.parkNanos(9 * 1000 * 1000000L);
long endNanos_25_17 = System.nanoTime();
System.out.println("Platform thread: " + ( (endNanos_25_17 - startNanos_25_15) / 1000000.0));
}
}
);
thread2.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
while (true){
LockSupport.unpark(thread1);
LockSupport.unpark(thread2);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
< /code>
Выходной фрагмент < /h2>
Virtual thread: 985.0724
Virtual thread: 988.1296
Platform thread: 1988.9397
Virtual thread: 988.9953
Platform thread: 1989.7579
Virtual thread: 990.4098
Virtual thread: 988.9639
Virtual thread: 989.0095
Platform thread: 1991.151
Virtual thread: 989.9759
Platform thread: 1990.103
Virtual thread: 988.1343
Virtual thread: 990.1048
Platform thread: 1991.1685
Virtual thread: 989.0044
Virtual thread: 989.0884
Platform thread: 1991.3585
Virtual thread: 989.889
Virtual thread: 988.7816
Virtual thread: 990.0407
Подробнее здесь: https://stackoverflow.com/questions/796 ... arknanos-t