Three ways to give a thread its work
A thread is an independent path of execution inside one program, with its own call stack but sharing the same heap. You describe the work as a Runnable and run it on a new Thread with start(); the JVM maps each such platform thread to an operating system thread.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Start four threads that each count the even numbers in a quarter of 1..4000 into their own array slot. Join them and print each count and the total.
Create a thread named printer whose task stores its own name and whether it is a daemon into a String[]. Start it, join it and print the stored values.
Write a thread whose task throws ArithmeticException (divide by zero). Install an uncaught exception handler that prints the exception class name, and print done after joining.
Explain it without notes
What is the difference between a process and a thread, and what do threads in the same JVM share?
Why does calling run() instead of start() not create concurrency?
Why can a lambda passed to a thread only use effectively final local variables?
What decides when the JVM exits, and what are daemon threads for?
Expected output
hello from subclass
runnable total = 55
lambda: lambda ran