多线程(二) 同步
一、前情回顾
在多线程(一)中提到,多线程的使用是否一定是安全的呢?下面举一个例子来分析
该例子是一个火车票售票系统,由三个窗口售票,用两种创建多线程的方式来运行
①继承的方式
package thread2;
//模拟火车站售票窗口,开启三个窗口售票,总票数为100张
class Window extends Thread {
static int ticket = 100;
public void run() {
while (true) {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket--);
} else {
break;
}
}
}
}
public class TestWindow {
public static void main(String[] args) {
Window w1 = new Window();
Window w2 = new Window();
Window w3 = new Window();
w1.setName("窗口1");
w2.setName("窗口2");
w3.setName("窗口3");
w1.start();
w2.start();
w3.start();
}
}
很明显,该程序存在着线程的安全问题,运行可知。
②实现的方式
package thread2;
//使用实现Runnable接口的方式,售票
class Window1 implements Runnable {
int ticket = 100;
public void run() {
while (true) {
if (ticket > 0) {
// try {
// Thread.currentThread().sleep(10);
// } catch (InterruptedException e) {
// e.printStackTrace();
// } //放大错误
System.out.println(Thread.currentThread().getName() + "售票,票号为:"
+ ticket--);
} else {
break;
}
}
}
}
public class TestWindow1 {
public static void main(String[] args) {
Window1 w = new Window1();
Thread t1 = new Thread(w);
Thread t2 = new Thread(w);
Thread t3 = new Thread(w);
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
类似于①,此程序依旧也存在线程安全问题:打印出错票,重票等
我们应该如何解决这些安全问题呢,下面介绍线程的同步机制
二、同步
1.定义:同步是指程序中用于控制不同线程间操作发生相对顺序的机制
2.同步方式
同步的方式一般分为两种:①同步代码块 ②同步方法
①同步代码块
synchronized(同步监视器){
//需要被同步的代码块(即为操作共享数据的代码)
}
* 1.共享数据:多个线程共同操作的同一个数据(变量)
* 2.同步监视器:由一个类的对象来充当。哪个线程获取此监视器,谁就执行大括号里被同步的代码。俗称:锁
* 要求:所有的线程必须共用同一把锁!
class Window2 implements Runnable {
int ticket = 100;// 共享数据
public void run() {
while (true) {
synchronized (this) {//this表示当前对象,本题中即为w
if (ticket > 0) {
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()
+ "售票,票号为:" + ticket--);
}
}
}
}
}
public class TestWindow2 {
public static void main(String[] args) {
Window2 w = new Window2();
Thread t1 = new Thread(w);
Thread t2 = new Thread(w);
Thread t3 = new Thread(w);
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
注:在实现的方式中,考虑同步的话,可以使用this来充当锁。但是在继承的方式中,慎用this!
②同步方法
将操作共享数据的方法声明为synchronized。即此方法为同步方法,能够保证当其中一个线程执行
此方法时,其它线程在外等待直至此线程执行完此方法。
同步方法的锁:this
class Window4 implements Runnable {
int ticket = 100;// 共享数据
public void run() {
while (true) {
show();
}
}
public synchronized void show() {
if (ticket > 0) {
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket--);
}
}
}
public class TestWindow4 {
public static void main(String[] args) {
Window4 w = new Window4();
Thread t1 = new Thread(w);
Thread t2 = new Thread(w);
Thread t3 = new Thread(w);
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
线程同步的弊端:由于同一个时间只能有一个线程访问共享数据,效率变低了。
(部分代码来源于尚硅谷宋红康老师,在此感谢宋老师)