CONCURRENCY (Java)

🧠 CONCURRENCY (Java) – MINI STUDY GUIDE


πŸ”Ή 1. What is Concurrency?

Definition (simple lang):
Concurrency means multiple tasks running at the same time.

πŸ‘‰ Think of it like this:

  • Nagla-laptop ka πŸ’» habang naka-play music 🎧

  • Or nagdo-download habang nagba-browse

πŸ’‘ This part is important kasi ito β€˜yung foundation ng topic.

Key Ideas:

  • A multithreaded program has multiple parts running at once

  • Helps improve efficiency (di sayang oras habang naghihintay)

πŸ“Œ From your handout (page 1):
Concurrency allows tasks waiting for resources to give way to other tasks.


πŸ”Ή 2. Thread vs Process

Concept

Meaning

Easy Example

Thread

Smallest unit of execution

One task inside an app

Process

Group of threads

Whole app (e.g., Chrome)

πŸ”₯ Real-Life Analogy:

  • Process = Restaurant 🍽

  • Threads = Waiters serving tables πŸ‘¨β€πŸ³

πŸ‘‰ Multiple waiters (threads) = faster service

πŸ“Œ Important:

  • Threads in the same process share memory

  • Meaning: mabilis communication between them


πŸ”Ή 3. How Threads Work (Scheduler + Context Switch)

🧠 Thread Scheduler

  • OS decides which thread runs next

πŸ”„ Context Switch

  • Saving current thread β†’ switching to another β†’ then returning later

πŸ‘‰ Parang:

  • You pause Netflix β†’ do homework β†’ balik ulit sa Netflix

πŸ“Œ From page 1:
Context switching stores and restores thread state.


πŸ”Ή 4. Main Thread in Java

  • Every Java program starts with main thread

public static void main(String[] args)

πŸ‘‰ This is the starting point of everything

πŸ’‘ You can create more threads to run tasks simultaneously


πŸ”Ή 5. Ways to Create Threads (SUPER IMPORTANT πŸ”₯)

βœ… Method 1: Extend Thread Class

class MyThread extends Thread {
    public void run() {
        System.out.println("Running thread");
    }
}

πŸ‘‰ Then:

MyThread t = new MyThread();
t.start();

βœ… Method 2: Implement Runnable Interface

class MyThread implements Runnable {
    public void run() {
        System.out.println("Running thread");
    }
}

πŸ‘‰ Then:

Thread t = new Thread(new MyThread());
t.start();

βš–οΈ Comparison

Extend Thread

Implement Runnable

Simpler

More flexible

Can’t extend another class

Can extend other classes

Less reusable

More reusable

πŸ’‘ In short:

Use Runnable if you want flexibility πŸ’―


πŸ”Ή 6. start() vs run() (COMMON CONFUSION ⚠)

Method

What it does

start()

Starts new thread

run()

Code executed by thread

πŸ‘‰ Important:

  • start() β†’ creates new thread

  • run() β†’ just runs like normal method if called directly

πŸ’‘ In short, ganito lang β€˜yan:

πŸ‘‰ start() = real multithreading

πŸ‘‰ run() = normal function lang


πŸ”Ή 7. Thread Methods (Core Tools)

From page 2:

Method

Purpose

start()

Start thread

run()

Main logic

setName()

Name thread

getName()

Get thread name

setPriority()

Set priority

getPriority()

Get priority

isAlive()

Check if running

join()

Wait for thread

sleep()

Pause thread


πŸ”Ή 8. Thread Priority


  • Range: 1 (low) β†’ 10 (high)


  • Default: 5

πŸ“Œ From page 2:

  • MIN_PRIORITY = 1

  • MAX_PRIORITY = 10

  • NORM_PRIORITY = 5

⚠ Important:


  • Higher priority β‰  guaranteed faster execution


  • It just has higher chance to run


πŸ”Ή 9. Useful Thread Methods Explained

πŸ’€ sleep()


  • Pauses thread

Thread.sleep(3000); // 3 seconds

πŸ‘‰ Example:


  • Waiting before next action (like loading screen)


⏳ join()


  • Wait for another thread to finish

πŸ‘‰ Example:


  • Wait for download β†’ then open file


🧬 isAlive()


  • Checks if thread is still running

t.isAlive(); // true or false

πŸ”₯ Example Flow (from pages 3–4)


  • Thread starts


  • Sleeps (pause)


  • Ends


  • Then check if alive β†’ false


πŸ”Ή 10. Thread.currentThread()

πŸ‘‰ Gets the currently running thread

Thread.currentThread().getName();

πŸ’‘ Useful for debugging or tracking execution


🧠 QUICK SUMMARY (Memory Mode)

πŸ”₯ Core Idea:

Concurrency = doing multiple things at the same time


🧩 Easy Memory Tricks

1. TPC Rule

  • Thread = small task

  • Process = container

  • Concurrency = multitasking


2. Start vs Run

πŸ‘‰ β€œStart creates, Run executes”


3. Thread Creation

πŸ‘‰ β€œExtend OR Implement”


  • Extend Thread


  • Implement Runnable


4. Thread Methods Shortcut

πŸ‘‰ SRSP GGIJS


  • Start


  • Run


  • Sleep


  • Priority


  • Get/Set Name


  • Get/Set Priority


  • isAlive


  • Join

(Okay medyo sabog mnemonic but it works 😭)


🧠 FINAL RECAP (Super Simplified)

πŸ‘‰ Concurrency = sabay-sabay na tasks

πŸ‘‰ Threads = workers

πŸ‘‰ Process = whole system

πŸ‘‰ start() = new thread

πŸ‘‰ run() = actual work

πŸ‘‰ sleep/join/isAlive = control tools