10. The thread has eight locks

  • If there are multiple synchronized methods in an object, any thread that calls one of the synchronized methods at any given time must wait. In other words, only one thread can access the synchronized methods at any given time
  • The lock is on the current object this, and after this is locked, no other thread can access other synchronized methods on the current object
  • Add a normal method and find that it has nothing to do with synchronization lock
  • When you switch to two objects, instead of the same lock, the situation changes immediately.
  • Once you switch to static synchronization, things change again
  • All non-statically synchronized methods use the same lock — the instance itself. That is, if a non-statically synchronized method of an instance acquires a lock, the other non-statically synchronized methods of that instance must wait for the method that acquires the lock to release the lock. However, non-statically synchronized methods of other instances can acquire their own locks without waiting for the instance to release the lock because they use a different lock than the instance’s non-statically synchronized methods.
  • All statically synchronized methods use the same lock — the class object itself. These two locks are two different objects, so there are no race conditions between statically synchronized methods and non-statically synchronized methods. But once a statically synchronized method acquires the lock, all other statically synchronized methods must wait for that method to release the lock before acquiring it, whether between statically synchronized methods of the same instance object or between statically synchronized methods of different instance objects, as long as they are of the same class!
class Number{
	
	public static synchronized void getOne(a){//Number.class
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
		}
		
		System.out.println("one");
	}
	
	public synchronized void getTwo(a){//this
		System.out.println("two");
	}
	
	public void getThree(a){
		System.out.println("three"); }}Copy the code

test

	public static void main(String[] args) {
		Number number = new Number();
		Number number2 = new Number();
		
		new Thread(new Runnable() {
			@Override
			public void run(a) {
				number.getOne();
			} 
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run(a) {
// number.getTwo();
				number2.getTwo();
			}
		}).start();
		
		/*new Thread(new Runnable() { @Override public void run() { number.getThree(); } }).start(); * /
		
	}
Copy the code
  1. Two normal synchronization methods, two threads, standard print, print? //one two
  2. Add thread.sleep () to getOne(), print? //one two
  3. New common method getThree(), print? //three one two
  4. Two normal synchronization methods, two Number objects, print? //two one
  5. Change getOne() to static synchronization, print? //two one
  6. Change two methods are statically synchronized, a Number object? //one two
  7. One statically synchronized method, one nonstatically synchronized method, two Number objects, okay? //two one
  8. Two statically synchronized methods, two Number objects? //one two

The key to thread eight locks:

① The default lock for non-static methods is this, and the lock for static methods is the corresponding Class instance

② Only one thread can hold a lock at a time, regardless of how many methods.