Skip to content

Chapter 22 of 37

Threads and Runnable

Start, join, interrupt, and coordinate Thread and Runnable tasks.

38 minutes 10 quick checksBy Subha Prasad
Lesson 22 of 37Course navigation

Lesson content

Read, practise, then check your understanding

A Thread represents an execution; Runnable represents a no-result task. Call start, not run, to create concurrent execution.

Lifecycle and cancellation

Thread worker = new Thread(() -> {
    while (!Thread.currentThread().isInterrupted()) {
        // bounded unit of work
    }
});
worker.start();
worker.interrupt();
worker.join();

Interruption is cooperative. Blocking methods may throw InterruptedException; either propagate it or restore status with Thread.currentThread().interrupt(). Avoid sharing mutable state and never use deprecated forceful controls such as stop. For application work prefer executors, structured concurrency where supported, or virtual threads for numerous blocking tasks rather than manually creating unbounded platform threads.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes Thread?
Which Java term matches this description: An object representing a platform thread of execution.
Which statement best describes Runnable?
Which Java term matches this description: A no-result task whose run method can execute on a thread.
Which statement best describes start?
Which Java term matches this description: The Thread method that schedules a new execution and later invokes run.
Which statement best describes join?
Which Java term matches this description: A method that waits for a thread to terminate.
Which statement best describes interrupt?
Which Java term matches this description: A cooperative cancellation signal represented by interrupt status.

0 of 10 checks passed

Your progress is saved on this device.

Threads and Runnable | Java Lesson | Subha Prasad