java thread synchronized combing

 

        There are two kinds of synchronized, one is the locking method, and the other is the locking code block. The difference is that the locking code block is more flexible and consumes less performance.

        Here are two examples, the first one:

public class TestSynchronizedMethod1 implements Runnable{

	/**
	 * When two concurrent threads access the synchronized(this) synchronized code block in the same object object, only one thread can be executed at a time.
	 * Another thread must wait for the current thread to finish executing the code block before executing the code block.
	 * @author cdzhujun
	 */
	
	public 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) {
		TestSynchronizedMethod1 t1 = new TestSynchronizedMethod1();
		Thread ta = new Thread(t1,"A");
		Thread tb = new Thread(t1,"B");
		ta.start();
		tb.start();
	}

}

The execution result is:

A synchronized loop 0
A synchronized loop 1
A synchronized loop 2
A synchronized loop 3
A synchronized loop 4
B synchronized loop 0
B synchronized loop 1
B synchronized loop 2
B synchronized loop 3
B synchronized loop 4

 The above program demonstrates the basic role of locks.

 

Second example:

public class TestSynchronizedMethod2 implements Runnable {

	/**
	 * However, when a thread accesses a synchronized(this) synchronized block of object,
	 * Another thread can still access non-synchronized(this) synchronized code blocks in this object.
	 * @author cdzhujun
	 */

	int b = 100;

	public synchronized void m1() throws Exception {
		b = 1000;
		Thread.sleep(5000);
		System.out.println("b = " + b);
	}

	//The m2 method can be called by the main thread, and the variable b can still be accessed as a resource. The synchronized method only locks the m1 method, and does not lock the variable resource b.
	public void m2() throws Exception {
		Thread.sleep(2500);
		b = 2000;
	}

//	public void m2() {
//		System.out.println(b);
//	}
	
	public void run() {
		try {
			m1();
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}

	public static void main(String[] args) throws Exception {
		TestSynchronizedMethod2 tsm = new TestSynchronizedMethod2();
		Thread t = new Thread(tsm);
		t.start();

		Thread.sleep(1000);
		tsm.m2 ();
	}
}

 The program execution result is: after 5 seconds, the output: b = 2000

 

Explanation of the second example: the main thread of main calls the method tsm.m2(), which can be accessed because m2() is a non-synchronized method. So the execution sequence of the program is that tsm starts a new thread, and during the 5 seconds of waiting, the main thread main calls the method tsm.m2(), sets b to 2000, and then outputs m1 after 5 seconds. At this time, b Already 2000. (b=1000 is indeed set in m1, but after 2500 milliseconds, the m2 method resets the value).

      Note here: synchronized locks all locked method blocks of the current object and this object.

      How to understand? that is:

      1. When a thread accesses a synchronized (this) synchronized code block of an object, another thread can still access a non-synchronized (this) synchronized code block in the object.

      2. The synchronized methods of different object instances do not interfere with each other. That is, other threads can still access synchronized methods in another object instance of the same class at the same time.

      3. When other threads access the non-synchronized code block of the object object, the member variables of the object, as a resource, can still be accessed (variable b in the second example).

      Through the above examples, I believe that everyone should have a correct understanding of the synchronized keyword.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326725729&siteId=291194637