5. Thread

Vegeedog
JavaNotes
Published in
Jul 17, 2021

--

The main function stands for the main thread.

To create other threads,

The run method defines the action of this thread.

For instance, we want this thread to print something every second within 5 seconds.

  • Note that the Thread.sleep method would show an error hint, which can be simply solved by adding the try-catch. (shortcut: Alt + Shift +Enter)

Whole code:

The output:

(The output from main thread & work thread appear alternatively.)

--

--