Java 多线程设计模式 —— Single Threaded Execution
source link: http://www.zhaojun.im/java-multithreaded-design-pattern-01/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Java 多线程设计模式 —— Single Threaded Execution
Single Threaded Execution 有时也会被称之为 临界区
。表示 临界区
内的代码同一时间内只允许一个线程执行,这个模式是并发编程的基础,对应 Java 中也就是同步代码块 synchronized
,或其他显式锁 lock
。
- 多线程环境
- 多个线程同时访问的共享资源
- 这些线程会改变共享资源。
现有票 100 张,分三个窗口售卖,卖完为止。
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Ticket {
private int counter = 100;
public void sell() {
if (counter > 0) {
System.out.println(Thread.currentThread().getName() + "号窗口卖出:" + this.counter-- + "号票");
} else {
System.out.println("票已卖完");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class StationThread extends Thread {
private final Ticket ticket;
public StationThread(String name, Ticket ticket) {
super(name);
this.ticket = ticket;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
ticket.sell();
}
}
}
1
2
3
4
5
6
7
8
9
public class Main {
public static void main(String[] args) {
Ticket ticket = new Ticket();
new StationThread("S1", ticket).start();
new StationThread("S2", ticket).start();
new StationThread("S3", ticket).start();
}
}
运行结果如下:
很明显的可以看到,同一张票,被多个窗口同时售卖了,就是因为未对共享资源 Ticket 做保护,导致了这种情况。
解决办法也很简单,给 Ticket 的 sell 方法加上 synchronized
即可。这样就防止了多个窗口同时访问同一张票。
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Ticket {
private int counter = 100;
public synchronized void sell() {
if (counter > 0) {
System.out.println(Thread.currentThread().getName() + "号窗口卖出:" + this.counter-- + "号票");
} else {
System.out.println("票已卖完");
}
}
}
单线程的情况,不需要使用此模式,就好像一个人在家上厕所,不需要锁门一样。
只有当 多线程 同时对 同一个共享资源,在线程中 进行了修改,才需要使用此模式。
且需要注意,对于 共享资源,如果用到了此模式,要保证所有使用他的地方都进行了保护。不然等于你把门锁住了,但窗子没关。
- 本文作者: 赵俊
- 本文链接: http://www.zhaojun.im/java-multithreaded-design-pattern-01/
- 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK