Java Threads — தமிழ்

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 48 / 50 · தமிழ்

Java Threads

Separate a task from when it executes.

Step-by-step explanationsRunnable JavaPractice + answer

முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.

இந்தப் பாடத்தில்…

  • Explain the key idea behind Threads.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

அடிப்படையைப் புரிந்துகொள்ளுங்கள்

A thread can execute independently of the calling thread. start schedules a new thread; calling run directly is just a normal method call on the current thread. Use join when the caller must wait for completion. Real shared-state work also needs correct synchronization.

02 / Step by step

விரிவான விளக்கங்கள்

Java Threads: Concurrency அறிமுகம்

Thread என்பது program-ஓட ஒரு execution path. Multithreading என்பது ஒரே நேரத்துல multiple tasks execute பண்றது.

Thread create பண்ண இரண்டு ways இருக்கு:

  • Thread class-ஐ extend பண்றது
  • Runnable interface implement பண்றது

Thread Create — Extend Thread Class

Thread class extend பண்ணி run() method override பண்றோம்:

Thread-ஐ start பண்ண start() method call பண்ணணும் — run()-ஐ directly call பண்ணா multithreading நடக்காது.

Three Ways to Create a Thread

Java reference fragmentJAVA
// 1. Extend Thread
class MyThread extends Thread {
    public void run() { System.out.println("Thread running"); }
}
new MyThread().start();

// 2. Implement Runnable
class MyTask implements Runnable {
    public void run() { System.out.println("Task running"); }
}
new Thread(new MyTask()).start();

// 3. Lambda (Java 8+)
new Thread(() -> System.out.println("Lambda thread")).start();

Thread Lifecycle

StateMeaning
NewThread object created, not started yet
RunnableReady to run, waiting for CPU
RunningCurrently executing
Blocked/WaitingWaiting for a lock, I/O, or sleep to finish
TerminatedExecution completed or exception thrown

Always call start(), not run(). Calling run() directly executes on the current thread — no new thread is created.

Production-ல் Raw Thread avoid செய்யவும்: new Thread() directly create செய்வதை production code-ல் avoid செய்யவும். ExecutorService பயன்படுத்தவும்: ExecutorService pool = Executors.newFixedThreadPool(10); — இது thread reuse, error handling, மற்றும் graceful shutdown-ஐ handle செய்யும். Spring Boot-ல் @Async annotation இன்னும் எளிதாக்குகிறது.

03 / Read and predict

முழுமையான உதாரணம்

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        Thread worker = new Thread(() -> System.out.println("Worker finished"));
        worker.start();
        worker.join();
        System.out.println("Main continues");
    }
}
Predict first. Then reveal the expected output / வெளியீடு
Worker finished
Main continues

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

நீங்களே முயற்சி செய்யுங்கள்

Practice challenge

Remove join and run several times. What output order is guaranteed?

Reveal the answer and reasoning / விடை

Without join, either line can appear first. One particular run does not establish a guaranteed order.

தவிர்க்க வேண்டிய தவறு: Thread.sleep is not a reliable replacement for coordination such as join, locks, or higher-level concurrency utilities.

பாடத்தின் சுருக்கம்

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.