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