In a single-threaded runtime environment, actions execute one after another. The next action can happen only when the previous one is finished.
In Java, “thread” means two different things:
¦ An instance of class java.lang.Thread
¦ A thread of execution
An instance of Thread is just…an object. Like any other object in Java, it has variables and methods, and lives and dies on the heap. But a thread of execution is an individual process (a “lightweight” process) that has its own call stack. In Java,there is one thread per call stack—or, to think of it in reverse, one call stack per thread. The new call stack if we create will run concurrently.
You might find it confusing that we’re talking about code running concurrently—as if in parallel—given that there’s only one CPU on most of the machines running Java. What gives? 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.
The difference between the two types of threads (user and daemon) is that the JVM exits an application only when all user threads are complete—the JVM doesn’t care about letting daemon threads complete, so once all user threads are complete, the JVM will shut down, regardless of the state of any daemon threads.
In Java, “thread” means two different things:
¦ An instance of class java.lang.Thread
¦ A thread of execution
An instance of Thread is just…an object. Like any other object in Java, it has variables and methods, and lives and dies on the heap. But a thread of execution is an individual process (a “lightweight” process) that has its own call stack. In Java,there is one thread per call stack—or, to think of it in reverse, one call stack per thread. The new call stack if we create will run concurrently.
You might find it confusing that we’re talking about code running concurrently—as if in parallel—given that there’s only one CPU on most of the machines running Java. What gives? 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.
The difference between the two types of threads (user and daemon) is that the JVM exits an application only when all user threads are complete—the JVM doesn’t care about letting daemon threads complete, so once all user threads are complete, the JVM will shut down, regardless of the state of any daemon threads.
No comments:
Post a Comment