Posts

Showing posts from June, 2024

Before Java 5, multithreading in Java

 Before Java 5, multithreaded applications were managed using low-level concurrency mechanisms provided by the java.lang and java.util packages. Developers had to do a lot of work manually to create, manage, and synchronize threads. Here's a breakdown of how multithreading was handled pre-Java 5: 🧵 1. Creating Threads Before Java 5, developers used: java.lang.Thread by extending it: class MyThread extends Thread { public void run () { // task } } new MyThread ().start(); Or Runnable by implementing it: class MyRunnable implements Runnable { public void run () { // task } } new Thread ( new MyRunnable ()).start(); There were no thread pools or ExecutorService . So each thread was started manually, which caused performance and resource management issues. 🛡️ 2. Synchronization To manage concurrent access to shared resources: synchronized keyword was heavily used: synchronized void increment () { ...