Files
2026-06-08 13:33:56 +03:30

1.2 KiB

start() vs run()

Q1 the output: Calling run() Running in: main Calling start() Running in: Thread-2

answer to why: run() is executed by main thread although start() is executed by thread object that been created.

Q2 the difference between these two, is that when a thread (which has been created as an object) calls run(), couldn't execute it. So the method will be executed by the thread that created t1. But by calling start(), you wouldn't see this behavior and method will be executed by thread that been created as t2.

Deamon Threads

Q1 the output: Main thread ends. Daemon thread running...

answer to why: Main thread executes faster than deamon thread and exits the program so it doesn't let deamon thread execute properly.

Q2 the thread will execute properly and we'll see "Daemon thread running..." prints for 20 times after printing "Main thread ends.".

Q3 for example one of use cases is Logging and Auditing Services. Daemon threads can be used to log background activities continuously.

A shorter way to create threads

Q1 the output: Thread is running using a ...!

Q2 Java Lambda

Q3 it is the easiest way to create a thread. All three ways do the same execution. But with different usages.