Java SE API
Thread.run()
Run the platform-thread task when called directly; this does not create a new thread.
Formal Syntax & Structure
java syntaxjava
void run()Working Example
Example Snippetjava
import java.time.Duration;import java.util.concurrent.*;public class Main { // Only finite tasks and timed coordination are used in these examples. static void finish(Thread thread) throws InterruptedException { try { if (!thread.join(Duration.ofSeconds(3))) { thread.interrupt(); if (!thread.join(Duration.ofSeconds(3))) throw new AssertionError("worker did not stop"); throw new AssertionError("worker timed out"); } } catch (InterruptedException e) { thread.interrupt(); throw e; } } public static void main(String[] args) throws Exception { Runnable work = () -> System.out.println(Thread.currentThread().getName()); Thread worker = new Thread(work, "catalog-worker"); worker.run(); System.out.println(worker.getState()); worker.start(); finish(worker); System.out.println(worker.getState()); }}