Created
November 13, 2018 04:30
-
-
Save diaolizhi/5de3096cb792694460faf214b38f8f41 to your computer and use it in GitHub Desktop.
Java synchronized 作用于普通方法、静态方法、方法块
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter3; | |
/** | |
* @program: studythread2 | |
* @description: synchronized Demo | |
* @author: diaolizhi | |
* @create: 2018-11-13 10:29 | |
**/ | |
public class SynchronizedDemo { | |
/** | |
* @Description: 作用在普通方法上面的 synchronized | |
* @Param: [] | |
* @return: void | |
* @Author: diaolizhi | |
* @Date: 2018/11/13 | |
*/ | |
public synchronized void out() throws InterruptedException { | |
System.out.println(Thread.currentThread().getName()); | |
Thread.sleep(5000); | |
System.out.println(Thread.currentThread().getName() + "结束。"); | |
} | |
/** | |
* @Description: 使用 synchronized 修饰静态方法 | |
* @Param: [] | |
* @return: void | |
* @Author: diaolizhi | |
* @Date: 2018/11/13 | |
*/ | |
public static synchronized void staticOut() throws InterruptedException { | |
System.out.println(Thread.currentThread().getName()); | |
Thread.sleep(5000); | |
System.out.println(Thread.currentThread().getName() + "结束。"); | |
} | |
private Object object = new Object(); | |
/** | |
* @Description: 使用 synchronized 修饰代码块,锁住一个对象 | |
* @Param: [] | |
* @return: void | |
* @Author: diaolizhi | |
* @Date: 2018/11/13 | |
*/ | |
public void myOut() throws InterruptedException { | |
System.out.println(Thread.currentThread().getName() + "进入 myOut 方法"); | |
synchronized (object) { | |
System.out.println(Thread.currentThread().getName() + "进入同步代码块"); | |
Thread.sleep(5000); | |
System.out.println(Thread.currentThread().getName() + "退出同步代码块"); | |
} | |
System.out.println(Thread.currentThread().getName() + "退出 myOut 方法"); | |
} | |
public static void main(String[] args) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter3_test; | |
import chapter3.SynchronizedDemo; | |
import org.junit.jupiter.api.DisplayName; | |
import org.junit.jupiter.api.Test; | |
/** | |
* @program: studythread2 | |
* @description: | |
* @author: diaolizhi | |
* @create: 2018-11-13 10:34 | |
**/ | |
public class SynchronizedTest { | |
/** | |
* @Description: 用 synchronized 修饰一个普通方法。创建两个线程分别调用 SynchronizedDemo 的 out 方法,发现它们互不干扰。 | |
* @Param: [] | |
* @return: void | |
* @Author: diaolizhi | |
* @Date: 2018/11/13 | |
*/ | |
@Test | |
void testLockCommonMethodAndTwoObj() throws InterruptedException { | |
SynchronizedDemo synchronizedDemo = new SynchronizedDemo(); | |
SynchronizedDemo synchronizedDemo1 = new SynchronizedDemo(); | |
new Thread(() -> { | |
try { | |
synchronizedDemo.out(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
}).start(); | |
new Thread(() -> { | |
try { | |
synchronizedDemo1.out(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
}).start(); | |
Thread.sleep(20000); | |
} | |
/** | |
* @Description: 锁住普通方法,创建两个线程调用同一个对象的 out 方法,发现只有当一个线程释放锁的时候,另一个线程才能访问。 | |
* @Param: [] | |
* @return: void | |
* @Author: diaolizhi | |
* @Date: 2018/11/13 | |
*/ | |
@Test | |
void testLockCommonMethodAndOneObj() throws InterruptedException { | |
SynchronizedDemo synchronizedDemo = new SynchronizedDemo(); | |
Runnable runnable = new Runnable() { | |
@Override | |
public void run() { | |
try { | |
synchronizedDemo.out(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
new Thread(runnable).start(); | |
new Thread(runnable).start(); | |
Thread.sleep(20000); | |
} | |
/** | |
* @Description: 对于普通方法,两个线程互不影响,但是对于静态方法,两个不同的对象在某一时刻,只有一个能访问。 | |
* @Param: [] | |
* @return: void | |
* @Author: diaolizhi | |
* @Date: 2018/11/13 | |
*/ | |
@Test | |
@DisplayName("两个线程调用两个对象的静态方法") | |
void lockStaticMethodAndTwoObj() throws InterruptedException { | |
SynchronizedDemo synchronizedDemo = new SynchronizedDemo(); | |
SynchronizedDemo synchronizedDemo1 = new SynchronizedDemo(); | |
new Thread(() -> { | |
try { | |
synchronizedDemo.staticOut(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
}).start(); | |
new Thread(() -> { | |
try { | |
synchronizedDemo1.staticOut(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
}).start(); | |
Thread.sleep(20000); | |
} | |
/** | |
* @Description: 在普通方法中使用 synchronized 锁住一个对象,发现在多线程中只有一个线程可以进入该同步代码块。 | |
* (因为必须锁住同一个对象,所以测试方法中只创建了一个对象。) | |
* @Param: [] | |
* @return: void | |
* @Author: diaolizhi | |
* @Date: 2018/11/13 | |
*/ | |
@Test | |
@DisplayName("两个线程访问同一个对象的同步代码块") | |
void testLockBlockAndOneObj() throws InterruptedException { | |
SynchronizedDemo synchronizedDemo = new SynchronizedDemo(); | |
Runnable runnable = () -> { | |
try { | |
synchronizedDemo.myOut(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
}; | |
new Thread(runnable).start(); | |
new Thread(runnable).start(); | |
Thread.sleep(20000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment