Java thread details
Java uses two methods of thread usage:
1. inherit from Thread
2. Implement Runnable
Some methods in the thread:
Wait ():
The method of the Object class. Calling the wait method for this Object causes this thread to discard the Object lock and enter the waiting lock pool for this Object. Only the notify method (or notifyAll) is issued for this Object) then this thread enters the object lock pool and prepares to get the object lock and enters the running state.
Sleep:
It is a Thread method, which causes the Thread to suspend the execution for a specified time and give the execution opportunity to other threads. However, the monitoring status remains unchanged and will be automatically restored later. Calling sleep does not release the object lock.
Yield:
This is called "concession". It gives the running opportunity to other threads with the same priority, that is, to allow the currently running threads to get out of CPU resources, but who doesn't know it, the thread status returns to the runable status. It may have just been transferred out, and it will continue to be executed immediately.
SetDaemon (true ):
This method sets the thread as a daemon. Daemon threads are rarely used, but are not useless. For example, JVM garbage collection and memory management threads are all daemon threads. What is the purpose? When all the remaining processes are daemon, the Java Virtual Machine will exit. That is to say, when all the non-daemon threads are executed, this daemon thread will not be executed yet, it will also end with the last non-daemon process!
Thread Synchronization:
1. Add synchronized to the method. When two concurrent threads access this method in the same object, only one thread can be executed in one time. The other thread must wait until the current thread finishes executing this code block before executing this code block ..
For example: public synchronized void test (){...}
2. synchronous code block: synchronized (Object ){...}
Note: Will synchronized lock code or object?
Synchronized locks the objects in brackets, not the code. For non-static synchronized methods, the lock is that the object itself is this.
See the following code:
package com.liang.ThreadDemo;public class SynchronizedTest implements Runnable{@Overridepublic void run() { synchronized(this){ for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " synchronized loop " + i); } } }public static void main(String[] args) {SynchronizedTest t1 = new SynchronizedTest();SynchronizedTest t2 = new SynchronizedTest();Thread ta = new Thread(t1,"A");Thread tb = new Thread(t2,"B");ta.start();tb.start();}}The result is as follows: why is it not locked? Because the lock is an object rather than code, but different objects cannot be locked!
A synchronized loop 0B synchronized loop 0A synchronized loop 1B synchronized loop 1A synchronized loop 2B synchronized loop 2A synchronized loop 3B synchronized loop 3A synchronized loop 4B synchronized loop 4
It's easy to lock the class directly:
package com.liang.ThreadDemo;public class SynchronizedTest implements Runnable{@Overridepublic void run() { synchronized(SynchronizedTest.class){ for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " synchronized loop " + i); } } }public static void main(String[] args) {SynchronizedTest t1 = new SynchronizedTest();SynchronizedTest t2 = new SynchronizedTest();Thread ta = new Thread(t1,"A");Thread tb = new Thread(t2,"B");ta.start();tb.start();}}
A synchronized loop 0A synchronized loop 1A synchronized loop 2A synchronized loop 3A synchronized loop 4B synchronized loop 0B synchronized loop 1B synchronized loop 2B synchronized loop 3B synchronized loop 4
Only the red part is changed, that is, how to lock the class. Of course, it is added to the method. If you change the method to static class, it is also how to lock the class.