Thread-related
Java multi-threaded implementation
Inherited Thread, implement Runnable, implement Callable Interface (can throw an exception and returns a value, not used)
Why have inherited but also a way to Thread Runnable Interface
Way to achieve the same interface for multiple threads of program code to deal with the same resources, avoid the Java single inheritance restriction
Why start the JVM is multithreaded?
Because at least start the two threads: the main thread, the thread garbage collection
Meaning threads and processes
Process: is the basic unit of resource allocation of the operating system, running programs
Thread: is the basic unit of task scheduling and execution, the program uses the most basic unit of CPU
Meaning multi-threaded and multi-process
Multi-process : the operating system to run multiple tasks ie the program
Multithreading: the same program multiple streams in order to perform
Concurrency and parallelism of meaning
Parallel: simultaneously logically run for some time a plurality of programs
Concurrent: simultaneously physically, a plurality of programs run at the same time
The difference between threads and processes
difference | process | Thread |
There are multiple threads in a process | ||
RAM | No shared memory | Shared memory |
Resources | The process of sharing files between network resources | Not shared between threads |
Spending | Process needs to allocate memory, larger overhead | Thread stack and only need to assign a PC, smaller overhead |
independent | Can exist independently | Can be independent, you must rely on the process exist |
effect | The process is the smallest unit CPU resource allocation | The thread is the smallest unit of CPU scheduling |
communication | Inter-process communication is more complex because of its independence of data space is required by the operating system, inter-process communication mechanism based on the socket | Since the communication between threads multithreaded shared address and data spaces, it may communicate directly, without going through the operating system (kernel scheduling) |
Thread scheduling model
Time-sharing scheduling model: all the threads take turns using the right to use the CPU, the average allocation of time slices each thread CPU-intensive
Preemptive scheduling model: giving priority to high-priority threads CPU, get the more CPU time slice, Java uses this model
Inter-thread communication - producer-consumer model
Manufacturer: look at whether there is data, consumer spending has to wait for (wait), not on production, the production of consumer spending after the notification
Consumers: look at whether there is data on the consumer has not to wait for producers to produce, to inform producers of production data (notify)
Java thread scheduling
Thread to sleep: Thread.sleep (Long of millis), thread the transition to the blocking state, when the end of sleep, it is converted to a ready state
Thread waits: the Object.wait (), causes the current thread to wait until another thread this object notify () or notifyAll (call)
Thread wakes: Object.notify () method, wake up a single thread waiting on this object.
Thread concession: Thread.yield (), pause the currently executing thread object, to give the opportunity to perform the same or a higher priority thread
Threads are: the Join (), wait for the other thread to terminate
Thread State
Initial state: to create a thread
Ready state: thread calls start () method
Running state: the thread is CPU scheduling
Blocking state: give up the right to use the CPU, pause after completion back to a ready state (synchronous blocks, waiting for obstruction, other obstruction)
Death Status: thread execution finish due to abnormal or out of the run () method, thread the end of the life cycle