Thread
A code snippet that executes concurrently during the execution of a program.
Memory can be shared between threads.
Thread Safety
Synchronous processing is added to ensure that only one thread executes synchronous code at the same time.
The way to ensure thread safety is to lock the mechanism any object in Java can act as a lock object
synchronized (lock) {....} Code in a code block ensures that only one thread can execute at the same time
The synchronization method uses the current object as the synchronization object (this)
Public synchronized int GetTicket () {...} synchronized keyword can also be added to the method to ensure that only one thread at a time can access the method
Static methods can be processed synchronously, using class as the synchronization object.
Create thread mode
Inherit the thread class
1. The subclass overrides the Run method in the parent class and stores the code running by the thread in run.
2. The thread is also created while creating the subclass object.
3. Open the thread by calling the Start method.
Thread.CurrentThread ()//Get current thread of execution
Yield ()//Let the thread discard the CPU preemption rights
Sleep (int milliseconds)/number of milliseconds that the thread sleeps
Prerequisites for synchronization
1. Synchronization requires two or more than two threads.
2. Multiple threads are using the same lock.
The above two conditions are not met and cannot be called synchronous.
The drawbacks of synchronization
When the thread is quite a long time, because each thread will judge the lock on the synchronization, it is very resource-intensive, which will reduce the running efficiency of the program.
Inter-thread communication
Think of 1:wait (), notify (), Notifyall (), which is used to manipulate threads why is it defined in the object class?
1. These methods exist in the synchronization code.
2. When using these methods, you must identify the synchronization lock that belongs to
3. A lock can be any object, so the method called by any object must be defined in the object class.
What is the difference between thinking about 2:wait () and sleep ()?
Wait (): Release CPU execution, release lock
Sleep (): Frees CPU execution and does not release locks
Daemon Thread case
There are two players and a waiter players in the process of playing the waiter needs every second time when the player exits after the waiter also quits
Here the waiter's thread is the daemon thread (the thread that serves the other thread) and when the player finishes, set its thread as the daemon thread, if all the threads are daemon threads
The program will automatically exit.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/B6/wKiom1cpwhmzjz8vAAA-0u8eKLw638.png "title=" Clipboard.png "alt=" Wkiom1cpwhmzjz8vaaa-0u8eklw638.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/B4/wKioL1cpwxPg_kGCAAA7ZM9-V18006.png "title=" Clipboard.png "alt=" Wkiol1cpwxpg_kgcaaa7zm9-v18006.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/B4/wKioL1cpw27QT3isAAA1Rp51ktg011.png "title=" Clipboard.png "alt=" Wkiol1cpw27qt3isaaa1rp51ktg011.png "/>
Multithreading & Thread Synchronization