Calling the wait()/notify() method of the Class object in the synchronized static method

Start with a sample demo using the wait()/notify() method in general

public class Test {

	public static void main(String[] args) {
		Object lock = new Object();// Lock object
		
		// target thread, which will be suspended
		Thread target = new Thread(() -> waited(lock));
		target.start();

		// action thread, use this thread to wake up the above target thread
		Thread action = new Thread(() -> wakeUp(lock));
		action.start();
	}

	private static void waited(Object lock) {
		synchronized (lock) {
			int times = 0;
			try {
				while (true) {// Loop print numbers until thread is suspended
					System.out.println(times);
					times++;
					if (times == 5) {// When the loop reaches the fifth time, the thread is suspended
						System.out.println("Target thread is suspended");
						lock.wait();
					}
					Thread.sleep(1000);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private static void wakeUp(Object lock) {
		try {
			Thread.sleep(10000);
			synchronized (lock) {
				lock.notify();
				System.out.println("Wake up target thread");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

In almost all tutorials, when using the wait()/notify() method, the programmer is required to first new an Object object Object as the call target, and lock the Object before calling the wait()/notify() method of the Object object Object.

Imagine that if the methods called by a thread are static methods (there is no new object in the whole business logic), and these static methods are all in the same class, then add the synchronized keyword directly to the static method (as we all know, adding the synchronized keyword directly to the static method is actually adding the class file where the static method is located Lock), and then directly use "class name. Class" to call class object wait()/notify(), which should be reasonable. Because there is no relevant test code on the Internet, I wrote a test myself.

The final conclusion is OK.

Post the code

public class Test {

	public static void main(String[] args) {
		// target thread, which will be suspended
		Thread target = new Thread(() -> waited());
		target.start();

		// action thread, use this thread to wake up the above target thread
		Thread action = new Thread(() -> wakeUp());
		action.start();
	}

	private synchronized static void waited() {
		int times = 0;
		try {
			while (true) {// Loop print numbers until thread is suspended
				System.out.println(times);
				times++;
				if (times == 5) {// When the loop reaches the fifth time, the thread is suspended
					System.out.println("Target thread is suspended");
					Test.class.wait();
				}
				Thread.sleep(1000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private synchronized static void wakeUp() {
		try {
			Thread.sleep(10000);
			Test.class.notify();
			System.out.println("Wake up target thread");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

The operation effect of the two writing methods is exactly the same

In fact, in the real business logic, it is almost impossible that there is no new object in the whole project, and it is difficult to ensure that all static methods are in the same class file. But I don't rule out that some wonderful companies will ask about this in their interview questions.

Keywords: Programming

Added by morphius on Fri, 20 Mar 2020 16:15:06 +0200