jdk1.5——Multi-threaded operation thread under the same lock communication writing method

 

0 code case:

 

package thread;

/**
 *
 * @author zm
 * Results of the:
The 0th execution of B
1st execution of A
1st execution B
2nd execution of A
2nd execution B
3rd execution of A
3rd execution B
4th execution of A
4th execution B
5th execution of A
5th execution B
6th execution of A
6th execution B
7th execution of A
7th execution B
8th execution of A
8th execution B
9th execution of A
9th execution B
 *
 * Notes:
 * 1 High cohesion encapsulates business methods on business classes and sets locks on business methods, so that as long as the calling thread uses the same business object, the mutual exclusion of business methods can be realized
 * 2 The premise of communication between multiple threads is still that the locks on the business method are the same. Use the keyword notify() wait() to achieve the waiting effect of multiple threads under the same lock
 */
public class CommunicateThread {

	// A executes B executes once and executes a total of 20 times
	public static void main(String[] args) {
		
		final Out out = new Out(); // the out object is the same latch
		
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace ();
				}
				for(int i=0; i<10; i++){
					out.printA (i);
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace ();
				}
				for(int i=0; i<10; i++){
					out.printB(i);
				}
			}
		}).start();
		
	}

}

class Out { // The business method only needs to have business, not anything with threads. Note that the same latch is required when the method needs mutual exclusion, and only the same latch can be used to wake up after the thread runs out of the current latch Other functions of the thread to use the latch
	
	public boolean flag = true;
	
	public synchronized void printA(int i){
		
		while(!flag){// When flag = false, the thread waits while executing this business method
			try {
				this.wait();// The latch waits
			} catch (InterruptedException e) {
				e.printStackTrace ();
			}
		}
		System.out.println("The "+i+"th time executes A"); // Otherwise, execute the business method directly
		flag = false; // change the flag bit
		this.notify(); // After running out of this latch, notify the thread that is also waiting to use this latch to use this latch. Pay attention to the usage of this
	}
	
	public synchronized void printB(int j){
		
		while(flag) {// When flag = true, the thread waits while executing this business method
			try {
				this.wait();// The latch waits
			} catch (InterruptedException e) {
				e.printStackTrace ();
			}
		}
		System.out.println("The "+j+" time execute B");
		flag = true; // change the flag bit
		this.notify(); //
	}
	
}



 

1 Brain map:

 



 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485820&siteId=291194637