RUVIDEO
Поделитесь видео 🙏

Java 19: Virtual Threads

Java 19 preview:
Virtual Threads



Operating systems can’t increase the efficiency of platform threads, but the JDK will make better use of them by severing the one-to-one relationship between its threads and OS threads.



A virtual thread is an instance of java dot lang dot thread that requires an OS thread to do CPU work -- but doesn’t hold the OS thread while waiting for other resources.



Don’t pool virtual threads!

With thread pool
(bad)
Suppose you have several hundreds of queries to launch. What you can do is create an executor service with 10 threads, Submit your hundred of queries to this executor service, and it will nicely send them to your database, 10 at a time.

With semaphore
(good)
Instead, what you can do is just use a classical Semaphore, that will do exactly the same.
And in fact, if you check the code, you can see that it is simpler, and far more expressive.
With a Semaphore, your code is shouting "I don't want to launch more than 10 queries on this DB at the same time".



Avoid frequent and long-lived pinning !

With synchronization
(bad)
Now the thing is, for the moment, if a virtual thread is executing some code inside a synchronized block, it cannot be detached from its platform thread.
So during the time it is running this synchronized block of code, it blocks a platform thread.
If this time is long, that is, if it's doing some long I/O operation, you may need to do something.

With ReentrantLock
(good)
You can prevent this situation from happenning by simply replacing the call to synchronized() with a reentrant lock.
This problem with synchronized blocks may be solved in the future, so in fact it may be solved by the time virtual threads become a final feature of the JDK.



CREDITS
https://blogs.oracle.com/javamagazine/post/java-loom-virtual-threads-platform-threads

https://youtu.be/lKSSBvRDmTg

MUSIC
http://ccmixter.org/files/Robbero/65280

Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Java 19: Virtual Threads», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.

Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.

Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!

Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.