Concurrency and parallelism
##Concurrency: refers to within a certain period of time, Multiple tasks are performed alternately. When multiple threads are operating, the CPU running time is divided into several time periods, and then the time periods are allocated to each thread for execution. While one thread's code is running, other threads are suspended.
Parallel: refers to the ability to handle multiple tasks at the same time. When multiple threads are operating, the CPU's ability to handle requests from these threads at the same time.
So in a concurrent environment, the closure of the program is broken, and the following characteristics appear: 1. There is a mutual restriction relationship between concurrent programs. Direct constraints are reflected in one program requiring the calculation results of another program; indirect constraints are reflected in multiple programs competing for shared resources, such as processors, buffers, etc. 2. The execution process of concurrent programs is intermittent. The program needs to remember on-site instructions and execution points. 3. When the number of concurrency is set appropriately and the CPU has sufficient processing power, concurrency will improve the running efficiency of the program. In a concurrent environment, when an object can be accessed by multiple threads, it will cause the object to be modified by any accessed thread, resulting in data inconsistency. Therefore, the concept of thread safety is proposed.Introduction to the basic concepts of threads
Threads and processes
Processes: Every process has Independent code and data space (process context), switching between processes will have a large overhead, and a process contains 1--n threads. (Process is the smallest unit of resource allocation). Simply put, processes are programs that are isolated from each other and run independently to some extent. Threads: Threads of the same type share code and data space. Each thread has an independent running stack and program counter (PC), and thread switching overhead is small. (Thread is the smallest unit of CPU scheduling) Threads are divided into five stages like processes: creation, ready, running, blocking, and termination. #1. Creation: A new thread object is created, but the start() method has not been called yet. For example, Thread thread = new Thread();2. Ready: After the thread object is created, other threads (such as the main thread) call the start() method of the object. The thread in this state is located in the runnable thread pool, waiting to be selected by thread scheduling to obtain the right to use the CPU. 3. Run: The thread in the running state (runnable) obtains the cpu time slice (timeslice) and executes the program code. 4. Blocked: The thread is blocked. The difference between "blocked state" and "waiting state" is: "blocked state" is waiting to acquire an exclusive lock. This event will occur in another event. Occurs when a thread gives up the lock; the "waiting state" is waiting for a period of time, or a wake-up action occurs. The thread will enter this state while the program is waiting to enter the synchronized area (synchronized). (1). Waiting for blocking: The running thread executes the o.wait() method, and the JVM will put the thread into the waiting queue. (2). Synchronous blocking: When a running thread acquires the synchronization lock of an object, if the synchronization lock is occupied by another thread, the JVM will put the thread into the lock pool. )middle. (3). Other blocking: When the running thread executes the Thread.sleep(long ms) or t.join() method, or issues an I/O request, the JVM will set the thread to is in blocking state. When the sleep() state times out, join() waits for the thread to terminate or times out, or the I/O processing is completed, the thread returns to the runnable state. 5. Waiting: The thread entering this state needs to wait for other threads to make some specific actions (notification or interruption). 6. Blocking: When a thread attempts to acquire an internal object lock (non-java.util.concurrent library lock), and the lock is held by other threads, the thread enters the blocking state. 7. Waiting: When a thread waits for another thread to notify the scheduler of a condition, the thread enters the waiting state. For example, call: Object.wait(), Thread.join(), and wait for Lock or Condition. 8. Timeout waiting (TIMED_WAITING): This state is different from WAITING. It can return by itself after the specified time. 9. TERMINATED: Indicates that the thread has completed execution.The above is the detailed content of What is concurrency in java?. For more information, please follow other related articles on the PHP Chinese website!