Thread related
Java multithreading implementation
Inherit Thread, implement Runnable interface, implement Callable interface (can throw exceptions and return values, not commonly used)
Why do we have a Runnable interface when we have Thread inheritance
The interface is implemented in a way that allows multiple threads of the same program code to handle the same resource, avoiding the restriction of single inheritance in Java
Why is JVM startup multithreaded?
At least two threads are started: the main thread and the garbage collection thread
Threads and processes
Process: The basic unit of operating system resource allocation, a running program
Thread: Is the basic unit of task scheduling and execution. Programs use the most basic unit of CPU
The meaning of multithreading and multiprocess
Multiprocess: The ability of an operating system to run multiple tasks, or programs, simultaneously
Multithreading: Multiple sequential streams executing in the same program
Concurrency and parallelism
Parallelism: Logically simultaneous, running more than one program at a time
Concurrency: Physically simultaneous, with more than one program running at the same time at one point in time
The difference between threads and processes
The difference between | process | thread |
There are multiple threads in a process | ||
memory | Unshared memory | The Shared memory |
resources | File network resources are shared between processes | Threads are not shared |
overhead | Processes need to allocate memory, which is expensive | The thread only needs to allocate the stack and a PC, which is less overhead |
independent | Can stand on its own | Can be independent, must depend on the process to exist |
role | A process is the smallest unit of CPU resource allocation | Threads are the smallest unit of CPU scheduling |
communication | Inter-process communication is more complicated because of its data space independence, which requires inter-process communication mechanism based on socket through the operating system | However, the communication between threads can be directly communicated without the operating system (kernel scheduling) because multithreads share address space and data space. |
Thread scheduling model
Time-sharing scheduling model: All threads take turns to use the CPU and allocate the CPU time slice equally for each thread
Preemptive scheduling model: Give priority to threads with higher priority to get more slices of CPU. Java uses this model
Interthread communication — producer-consumer pattern
Producer: see if there is data first, wait for consumers to consume if there is data, produce if there is no data, notify consumers to consume after production
Consumer: first see if there is any data, then consume. If there is no data, wait for the producer to produce, and notify the producer to produce data.
Java thread scheduling
Thread sleep: Thread.sleep(long millis) causes the Thread to switch to the blocked state and to switch to the ready state when sleep is over
Thread wait: object.wait (), causing the current thread to wait until another thread calls notify() or notifyAll() of the Object
Thread wake up: the object.notify () method wakes up a single thread waiting on this Object.
Thread yield: Thread.yield() suspends the currently executing Thread object to yield execution to a Thread of the same or higher priority
Threads join: join() and wait for other threads to terminate
Thread state
Initial state: Create a thread
Ready: The thread calls the start() method
Running status: The thread is scheduled by the CPU
Block state: relinquish CPU usage, return to ready state after pause (synchronous block, wait block, other block)
Dead: the thread ends its life cycle when it finishes executing or exits the run() method due to an exception