• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

CH-03 : The Phantom Thread – Jai & Veeru vs. The Virtual Thread Mystery

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Jai and Veeru were back in action. This time, they were working on a high-performance chat application that needed to handle thousands of users simultaneously.

“Veeru, this app must handle millions of messages per second!” Jai declared, typing away furiously.

Veeru, munching on a samosa, shrugged. “So what? We’ll just spawn thousands of threads like we always do!”

Jai’s fingers froze. He turned slowly. “Did you just say… thousands of threads?”

Veeru grinned. Yes! More threads, more power, right?”

Jai sighed and pointed at his laptop screen. “No, Veeru. Traditional threads in Java use OS threads, which are heavy. Too many of them will make our app slow, increasing memory usage and causing thread starvation.”

Veeru looked confused. “So… what do we do? Reduce the threads?”

Jai smirked. “Nope. We use Virtual Threads!”

Enter Java 21’s Virtual Threads


Jai quickly replaced this old, heavy thread creation method:


ExecutorService executor = Executors.newFixedThreadPool(100); // Heavy OS threads
executor.submit(() -> {
System.out.println("Processing request...");
});

With this lightweight Virtual Thread approach:


try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
executor.submit(() -> System.out.println("Processing request..."));
}

Veeru’s jaw dropped. “Wait… this creates thousands of threads without slowing down the system?”

Jai nodded. Yes! Virtual Threads are cheap, don’t block OS threads, and allow high scalability with minimal memory usage.”

Veeru clapped his hands. “Jai, you’re a genius! This means we can handle millions of users without breaking our app!”

Structured Concurrency — Fixing Messy Thread Management


“But wait, Jai,” Veeru frowned. “Managing threads is always a headache. What if some threads finish early and others take forever?”

Jai grinned. “Good question! Java 21 solves this with Structured Concurrency. Instead of tracking and managing each thread manually, we can group tasks together!”

Instead of this traditional, messy thread management:


ExecutorService executor = Executors.newFixedThreadPool(10);
Future<String> future1 = executor.submit(() -> fetchData("user1"));
Future<String> future2 = executor.submit(() -> fetchData("user2"));
System.out.println(future1.get() + future2.get());

Jai showed Veeru this cleaner, structured concurrency approach:


try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
var user1 = scope.fork(() -> fetchData("user1"));
var user2 = scope.fork(() -> fetchData("user2"));
scope.join();
System.out.println(user1.get() + user2.get());
}

Veeru was impressed. “So now, if one thread fails, we can shut everything down safely, avoiding half-executed tasks?”

Jai nodded. “Exactly! No more thread leaks or orphaned tasks.”

The Future of High-Performance Java Apps


With Java 21’s Virtual Threads & Structured Concurrency, Jai and Veeru’s chat app became:

  • ✅ Ultra-fast — Thousands of lightweight threads without crashing the system.
  • ✅ Memory-efficient — No wasted resources on heavyweight OS threads.
  • ✅ Clean & Manageable — Threads were grouped logically, preventing errors.

Veeru stretched his arms. “Jai, this Java 21 update is better than a Bollywood plot twist!”

Jai laughed. Yes, my friend! Now, let’s launch our app — this time, without a thread crisis!”

? To be continued… ?

?Key Takeaways from This Chapter


✔ Virtual Threads — Lightweight, scalable, and perfect for high-performance apps.
✔ Structured Concurrency — Simplifies thread management, prevents failures.
✔ Goodbye Heavy OS Threads — More efficiency, fewer headaches!


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу