What is a thread? -- Linux general technology-Linux programming and kernel information. The following is a detailed description. What is thread? As shown in Figure A, A thread is A sequence of given commands (your code), A stack (variables defined in the given method ), and some shared data (Class-level variables ). The thread can also access static data from the global class.
Stack and some possible shared data
Each thread has its own stack and program counter (PC ). You can imagine a program counter (PC) as a command used to track the thread's execution, while a stack is used to track the context of the thread. The context is when the thread executes somewhere, the value of the current local variable. Although you can write a subroutine that transfers data between threads, under normal circumstances, one thread cannot access the stack variables of another thread.
A thread must be in one of the following four possible states:
Initial State: the state in which a thread is located after the new method is called and before the start method is called. In the initial state, you can call the start and stop methods.
Runnable: Once the thread calls the start method, the thread changes to the Runnable state. Note that if the thread is in the Runnable state, it may not be running because of priority and scheduling problems. Blocking/NonRunnable: The thread is in the blocking/NonRunnable state, which is caused by two possibilities: either suspended or blocked for some reason, for example, wait for the completion of the IO request. Exit: the thread goes to the exit state. There are two possibilities: either the execution of the run method is completed or the stop method is called.
The last concept is the thread priority. A thread can set a priority. A high-priority thread can be completed before a low-priority thread. An application can set the priority of a thread by using the method setPriority (int) in the thread.
We have already talked about the basic knowledge of threads. Now we can look at the two mechanisms Java provides for developing thread-based applications: thread classes and Runnable interfaces.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service