- An instance of class java.lang.Thread
- A thread of execution
main()method, which starts the thread process, runs in one thread, called the main thread. If you looked at the main call stack you would see that
main()is the first method on the stack, the method at the bottom. But as soon as you create a new thread, a new stack materializes and methods called from that thread run in a call stack that is separate from the
main()call stack. That second new call stack is said to run concurrently with the main thread.
What mechanism allows the code to run concurrently?
The JVM, which gets its turn at the CPU by whatever scheduling mechanism the underlying OS uses, operates like a mini-OS and schedules its own threads, regardless of the underlying operating system. In some JVMs, the Java threads are actually mapped to native OS threads, but we will not discuss that here. It is not required to understand how threads behave in different JVM environments.
In fact, the most important concept to understand from this module is this:
When it comes to threads, very little is guaranteed. So be very cautious about interpreting the behavior you see on one machine as "the way threads work."