Java thread explanation and the difference between thread and process

@H_ 419_ 1@java Thread explanation and the difference between thread and process

@H_ 419_ 1@1. Process and thread

Each process has an exclusive memory space, and an application can start multiple processes at the same time. For example, IE browser, opening an IE browser is equivalent to starting a process.

Thread refers to an execution process in a process. A process can contain multiple threads.

Each process needs the operating system to allocate independent memory space, and multiple threads in the same process share this space, that is, share resources such as memory.

Every time you call Java Exe, the operating system will start a Java virtual machine process. When starting the Java virtual machine process, the Java virtual machine will create a main thread, which will execute from the main method of the program entry.

Every time the Java virtual machine starts a thread, it will assign a thread method stack to the thread to store relevant information (such as local variables). The thread runs on this stack. @ h)u 419)u 1 @ so the local variables in Java objects are thread safe, but the instance variables and class variables are not thread safe because they are not stored in the stack.

A process has three states: ready, execution, and blocking.

@H_ 419_ 1@2. Thread creation method

Runnable mode: (this mode is flexible and recommended)

The running result may be:

@H_ 419_ 1@Thead mode

Run result: thread's name is thread-0

@H_ 419_ 1 @ note: each time the program runs, there is a main thread in addition to the custom thread.

Comprehensive:

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>