One: Process and thread
Overview : Almost any operating system supports running multiple tasks, usually a task is a program , and a program is a process . When a process runs, the internal may include multiple sequential execution flows, and each sequential execution flow is a thread .
process : a process is a program that is in the process of running and has certain independent functions . A process is a unit of system resource allocation and scheduling. When the program goes into memory run, it is the incoming process .
Three features of the process:
1: Independence: The process is a separate entity in the system, it can own resources independently, each process has its own independent address space , no process itself to run, User processes do not have direct access to the address space of other processes.
2: Dynamic: The difference between the process and the program is that the process is dynamic, the process has the concept of time, the process has its own life cycle and a variety of different states .
3: Concurrency: multiple processes can be executed concurrently on a single processor, independently of each other .
concurrency and parallelism are different concepts: parallelism means that at the same time, multiple commands are executed concurrently on multiple processors, and concurrency means that at the same time, only one command is executed on the processor, but multiple process commands are quickly rotated to execute. Enables simultaneous execution of multiple processes on a macro level
Note:
Thread:
threads are part of a process, and a process can have multiple threads , and a thread must have a parent process . a thread can have its own stack, its own program counter, and its own local variables, but cannot have system resources. It shares all the resources of the process with other threads of the parent process .
features of the thread :
Threads can accomplish tasks by sharing shared variables and parts of the parent process with other threads and collaborating with each other to complete the task.
Threads are run independently and do not know if any other threads exist in the process.
The execution of the thread is preemptive, that is, the currently executing thread may be suspended at any time in order to run another thread.
One thread can create or revoke another thread, and multiple threads in a process can execute concurrently.
second, the creation and use of threads
Java uses the thread class to represent threads, and all thread objects must be instances of thread or its subclasses, each of which performs a certain task, in effect executing a program flow (code that executes sequentially)
Scenario One: Inheriting the thread class to create a threading class
Step:① define a subclass of the thread class and override the Run method of the class, which represents the task that the thread needs to complete
② create an instance of the thread class, that is, the threading object is created
③ calls the thread's Start method to start the thread
Conclusion: Using an inherited child When a subclass of the thread class is created to create a threading class, multiple threads cannot share instance variables of the thread class (for example, I above)
Scenario Two: Implementing the Runnable interface
① defines the implementation class for the Runnable interface and overrides its run method, which is also the execution of the thread !
② create an instance of the Runnable implementation class and create a thread object as the target of the thread, which is the true thread object !
③ call the Start method to start the thread
Conclusion: using The Ruunable interface creates multiple threads that can share instance variables of the thread class, because in this way the Runnable object created by the program is only the target of the thread, and multiple threads can share a target, so multiple threads can share an instance variable
Implementing multithreading through runnable is actually wrapping the run as a thread's execution, but currently Java cannot wrap any method into a thread-executing body
Scenario Three: creating Threads with callable and future starting with Java5, Java provides the callable interface, and the callable interface provides a call () method that can be used as a thread executor and looks like runnable, but the call () method is more powerful-- The call () method can have a return value, the call () method can throw an exception
JAVA5 provides a future interface to represent the return value of the call () method of the callable interface, and provides a Futuretask implementation class for the future interface that implements the class future interface, The Runnable interface is also implemented-it can be used as the target of thread.
Implementation steps:
① creates an implementation class for the callable interface and implements the call method, which becomes the thread execution body, and the call method has a return value in the creation of the callable interface's implementation Class!
② uses the Futruetask class to wrap the callable object, which Futruetask encapsulates the return value of the call method for the class callable
③ uses the Futruetask object as the target of the thread to create and start a new thread !
④ Use The Get method of Futruetask gets the return value after execution ends
Conclusion: Take The advantage of Runnable and callable is that the thread class simply implements the Runnable or callable interface and can inherit other classes, and in this way, multiple threads can share a target object, so it is well suited for multiple identical threads to work on the same resource. Thus the CPU, the code and the data are separated, the shape parameter clear model, embodies the object-facing programming idea. The disadvantage is a slightly higher degree of programming complexity.
Processes and threads in Java--three ways to implement