Skip to content

Commit bdf60d9

Browse files
Synchronized
1 parent 0a48130 commit bdf60d9

File tree

8 files changed

+268
-0
lines changed

8 files changed

+268
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ Java并发编程
3535
饥饿
3636

3737

38+
## Synchronized
39+
作用用法
40+
性质
41+
原理
42+
缺陷
43+
44+
3845
## 原子类
3946
基本类型原子类
4047
AtomicInteger

Synchronized/Synchronized.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* .class代码块
3+
*
4+
* @Author: zzStar
5+
* @Date: 10-21-2020 19:14
6+
*/
7+
public class SynchronizedClassClass implements Runnable {
8+
static SynchronizedClassClass instance1 = new SynchronizedClassClass();
9+
static SynchronizedClassClass instance2 = new SynchronizedClassClass();
10+
11+
12+
@Override
13+
public void run() {
14+
try {
15+
method();
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}
19+
}
20+
21+
private void method() throws InterruptedException {
22+
// synchronized (this) {
23+
synchronized (SynchronizedClassClass.class) {
24+
System.out.println("SynchronizedClassClass,I am " + Thread.currentThread().getName());
25+
Thread.sleep(3000);
26+
System.out.println(Thread.currentThread().getName() + "运行结束");
27+
}
28+
29+
}
30+
31+
32+
public static void main(String[] args) {
33+
Thread thread1 = new Thread(instance1);
34+
Thread thread2 = new Thread(instance2);
35+
thread1.start();
36+
thread2.start();
37+
38+
// 死循环
39+
while (thread1.isAlive() || thread2.isAlive()) {
40+
41+
}
42+
System.out.println("finished");
43+
44+
}
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 类锁的第一种形式 -> static形式
3+
*
4+
* @Author: zzStar
5+
* @Date: 10-21-2020 19:07
6+
*/
7+
public class SynchronizedClassStatic implements Runnable {
8+
9+
static SynchronizedClassStatic instance1 = new SynchronizedClassStatic();
10+
static SynchronizedClassStatic instance2 = new SynchronizedClassStatic();
11+
12+
@Override
13+
public void run() {
14+
method();
15+
}
16+
17+
// 加上static,全局同步方法
18+
public static synchronized void method() {
19+
System.out.println("SynchronizedClassStatic,I am " + Thread.currentThread().getName());
20+
try {
21+
Thread.sleep(3000);
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
}
25+
System.out.println(Thread.currentThread().getName() + "运行结束");
26+
}
27+
28+
29+
public static void main(String[] args) {
30+
Thread thread1 = new Thread(instance1);
31+
Thread thread2 = new Thread(instance2);
32+
thread1.start();
33+
thread2.start();
34+
35+
// 死循环
36+
while (thread1.isAlive() || thread2.isAlive()) {
37+
38+
}
39+
System.out.println("finished");
40+
41+
}
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 对象锁之代码块形式
3+
*
4+
* @Author: zzStar
5+
* @Date: 10-21-2020 18:43
6+
*/
7+
public class SynchronizedObjectCodeBlock implements Runnable {
8+
9+
static SynchronizedObjectCodeBlock instance = new SynchronizedObjectCodeBlock();
10+
// 自定义锁
11+
Object lock1 = new Object();
12+
Object lock2 = new Object();
13+
14+
@Override
15+
public void run() {
16+
// synchronized (this) {
17+
synchronized (lock1) {
18+
System.out.println("SynchronizedObjectCodeBlock,I am " + Thread.currentThread().getName());
19+
try {
20+
Thread.sleep(3000);
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
System.out.println(Thread.currentThread().getName() + "运行结束");
25+
}
26+
27+
// 可以并行执行
28+
synchronized (lock2) {
29+
System.out.println("SynchronizedObjectCodeBlock,I am " + Thread.currentThread().getName());
30+
try {
31+
Thread.sleep(3000);
32+
} catch (InterruptedException e) {
33+
e.printStackTrace();
34+
}
35+
System.out.println(Thread.currentThread().getName() + "运行结束");
36+
}
37+
}
38+
39+
public static void main(String[] args) {
40+
Thread thread1 = new Thread(instance);
41+
Thread thread2 = new Thread(instance);
42+
thread1.start();
43+
thread2.start();
44+
45+
// 死循环
46+
while (thread1.isAlive() || thread2.isAlive()) {
47+
48+
}
49+
System.out.println("finished");
50+
}
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 方法锁
3+
*
4+
* @Author: zzStar
5+
* @Date: 10-21-2020 19:00
6+
*/
7+
public class SynchronizedObjectMethod implements Runnable {
8+
static SynchronizedObjectMethod instance = new SynchronizedObjectMethod();
9+
10+
@Override
11+
public void run() {
12+
method();
13+
}
14+
15+
public synchronized void method() {
16+
System.out.println("SynchronizedObjectMethod,I am " + Thread.currentThread().getName());
17+
try {
18+
Thread.sleep(3000);
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
System.out.println(Thread.currentThread().getName() + "运行结束");
23+
}
24+
25+
public static void main(String[] args) {
26+
Thread thread1 = new Thread(instance);
27+
Thread thread2 = new Thread(instance);
28+
thread1.start();
29+
thread2.start();
30+
31+
// 死循环
32+
while (thread1.isAlive() || thread2.isAlive()) {
33+
34+
}
35+
System.out.println("finished");
36+
}
37+
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Synchronized使用总结
3+
*
4+
* @Author: zzStar
5+
* @Date: 10-21-2020 18:31
6+
*/
7+
public class SynchronizedSummary {
8+
9+
/**
10+
* JVM会自动通过使用monitor来加锁和解锁,保证了同时只有一个线程可以执行指定代码,从而保证了线程安全,具有可重入和不可中断的性质
11+
*/
12+
13+
/**
14+
* 作用 ->
15+
* 能够保证在 同一时刻❗ 最多 只有一个❗ 线程执行该段代码,以达到保证并发安全的效果
16+
*/
17+
18+
19+
/**
20+
* 对象锁
21+
* -> 包括方法锁(默认锁对象为this当前实例对象)和 同步代码块锁(自己指定锁对象)
22+
* 类锁
23+
* -> 指synchronized修饰静态的方法或指定锁为Class对象
24+
*/
25+
26+
/**
27+
* 几种情况
28+
* -> 一把锁只能同时被一个线程获取,没有拿到锁的线程必须等待
29+
* -> 每个实例都对应有自己的一把锁,不同实例之间互不影响,例外:锁对象是.class以及synchronized修饰的是static时,所有的对象共用同一把类锁
30+
* -> 无论是方法正常执行完毕或者方法抛出异常,都会释放锁
31+
* -> 另外,在synchronized中调用了没有被synchronized修饰的方法,不是线程安全的!
32+
*/
33+
34+
/**
35+
* 缺陷 ->
36+
* 效率低:锁的释放情况少,视图获得锁时不能设定超时,不能中断一个正在试图获得锁的线程
37+
* 不够灵活:加锁和释放的时机单一
38+
* 无法知道是否成功获得锁
39+
*/
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.concurrent.locks.Lock;
2+
import java.util.concurrent.locks.ReentrantLock;
3+
4+
/**
5+
* 加锁和释放锁的原理
6+
*
7+
* @Author: zzStar
8+
* @Date: 10-21-2020 19:45
9+
*/
10+
public class SynchronizedToLock {
11+
12+
Lock lock = new ReentrantLock();
13+
14+
public static void main(String[] args) {
15+
SynchronizedToLock synchronizedToLock = new SynchronizedToLock();
16+
synchronizedToLock.method1();
17+
synchronizedToLock.method2();
18+
}
19+
20+
public synchronized void method1() {
21+
System.out.println("synchronized形式的锁");
22+
}
23+
24+
// 等价方法
25+
public void method2() {
26+
lock.lock();
27+
try {
28+
System.out.println("我是lock形式的锁");
29+
} finally {
30+
lock.unlock();
31+
}
32+
}
33+
34+
}

0 commit comments

Comments
 (0)