E ExamMaster

Object-Oriented Programming through Java · Strings, Threads, JDBC and JavaFX

Multithreading

In Object-Oriented Programming through Java because two clerks must not sell the same seat — thread states, priority, synchronization, deadlock, wait and notify.

A thread is one run of the program that can proceed while another run proceeds. Two clerks booking seats are two threads. Each has a state and a priority. synchronized makes a block exclusive. Two locks taken in opposite order can deadlock. wait and notify let a thread pause until the hall has a seat.

  • Object-Oriented Programming through Java
  • Easy level
  • 7 concepts

1A thread is one concurrent run

A program starts with one thread: main. A second thread is another stack that can run at the same time. Meera and Arun booking seats are two threads sharing the hall object.

Sharing is the point and the danger. Two threads reading a seat as free can both sell it unless you coordinate.

Figure. Two threads, one hall. The shared object is where races happen.

How it works

  1. A thread is one concurrent runA thread is a concurrent run. Shared objects need a rule, or two clerks sell one seat.

2extend Thread or implement Runnable

One way: a class extends Thread and overrides run(). start() launches it. Calling run() yourself is just a method call on this thread.

The other way: implement Runnable and pass it to new Thread(runnable). Prefer Runnable when the class already extends something else.

Figure. new Thread(book, "meera").start() launches a second run. Calling run() yourself is a method call on this thread — still main, not a second clerk.

How it works

  1. extend Thread or implement Runnablestart() creates the second run. run() is the work. Do not call run() when you meant start().

start() launches; run() would not

Runnable book = () -> hall.take("A12");
Thread clerk = new Thread(book, "meera");
clerk.start();

3A thread moves through named states

After new Thread() the state is NEW. start() moves it to RUNNABLE. It becomes TERMINATED when run() returns.

BLOCKED is waiting to enter synchronized. WAITING is wait() with no timeout. TIMED_WAITING is sleep or a timed wait. The topic table names each state.

Four named JVM thread states in a row: NEW, RUNNABLE, BLOCKED, TERMINATED. A token sits on NEW, slides to RUNNABLE which fills sage, slides to BLOCKED which fills terracotta, then slides to TERMINATED.
start() leaves NEW for RUNNABLE. BLOCKED is waiting to enter synchronized. run() returning is TERMINATED.

How it works

  1. A thread moves through named statesNEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED. start() is what leaves NEW.

4Priority is a hint, not a promise

A thread has a priority from 1 to 10. Thread.NORM_PRIORITY is 5. setPriority asks the scheduler to prefer one clerk.

It is a hint. On many machines two clerks still interleave. Correctness comes from synchronization, not from hoping the student clerk runs first.

Figure. Priority is 1 to 10; NORM_PRIORITY is 5; MAX_PRIORITY is 10. setPriority is a hint to the scheduler. Exclusive access still comes from synchronized, not from hoping the student clerk runs first.

How it works

  1. Priority is a hint, not a promisePriority is 1..10 and advisory. It does not replace synchronized.

MAX_PRIORITY is 10; still not exclusive access

Thread clerk = new Thread(book);
clerk.setPriority(Thread.MAX_PRIORITY);
clerk.start();

5synchronized makes a block exclusive

synchronized (hall) { hall.take(seat); } lets only one thread at a time execute that block on that lock. The other waits (BLOCKED) until the lock is free.

The lock is the object you name. Two blocks that synchronize on different objects do not exclude each other. A synchronized method locks this.

Figure. synchronized (hall) lets only one thread execute the check-and-take block. The other is BLOCKED until that lock is free. Two blocks that lock different objects do not exclude each other.

How it works

  1. synchronized makes a block exclusiveOne lock object, one thread inside. Check and update must share that lock.

Check and take under the same lock

synchronized (hall) {
    if (hall.free(seat)) {
        hall.take(seat);
    }
}

6Deadlock is two locks taken in opposite order

Deadlock happens when thread A holds lock 1 and waits for lock 2, while thread B holds lock 2 and waits for lock 1. Neither can proceed.

Meera locks the hall then the till; Arun locks the till then the hall. The fix is a single lock order both clerks obey — or one lock for the whole booking.

Figure. Each clerk holds what the other wants. There is no arrow that frees a lock. Not a live animation — the stuck shape is the point.

How it works

  1. Deadlock is two locks taken in opposite orderDeadlock is a cycle of locks. Give every thread the same lock order.

7wait pauses inside the lock; notify wakes

wait() releases the synchronized lock and parks the thread (WAITING) until another thread calls notify or notifyAll on the same object.

A clerk waiting for a cancellation does hall.wait() inside synchronized (hall). The refund path calls hall.notifyAll() after freeing a seat. Always re-check the condition after wake — notify does not mean your seat is free.

Figure. hall.wait() inside synchronized (hall) releases the lock and parks the clerk (WAITING). A refund calls hall.notifyAll() after freeing a seat. Wake is not proof the seat is free — re-test in a while.

How it works

  1. wait pauses inside the lock; notify wakeswait releases the lock. notifyAll wakes waiters. Re-test the condition in a while.

wait in a while, not an if

synchronized (hall) {
    while (!hall.free(seat)) {
        hall.wait();
    }
    hall.take(seat);
}

Notes

  • In Object-Oriented Programming through Java because two clerks must not sell the same seat — thread states, priority, synchronization, deadlock, wait and notify.
  • A thread is New, Runnable, Blocked, Waiting, Timed Waiting, or Terminated. Priority is a hint, not a schedule you own.
  • synchronized (hall) { } is mutual exclusion. deadlock is two threads each holding what the other needs. wait/notify run inside that lock.

Exam traps & shortcuts

  • Thread.sleep does not release a synchronized lock. wait does.
  • A deadlock is not a crash with a stack you can catch. The clerks simply never finish.

Reference tables

These are the official Thread.State values. Runnable includes a thread that is ready but not currently on a core.

Thread states the JVM names
StateWhat the clerk is doing
NEWcreated, start() not called
RUNNABLErunning or ready to run
BLOCKEDwaiting to enter a synchronized block
WAITINGwait() until notify
TIMED_WAITINGsleep or wait with a timeout
TERMINATEDrun() has returned

Recap

A thread is a concurrent run. start() launches it. synchronized excludes. Opposite lock order deadlocks. wait/notify coordinate inside the lock.

start not run
run() on this thread is not a second clerk.
synchronized
One lock object. Check and take together.
deadlock
A cycle of locks. Same order, or one lock.
wait in while
Wake is not a proof the seat is free.

Practise Multithreading

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.