Java同步代码块和同步方法处理Thread类和Runnable的线程安全问题
源出处:http://www.gulixueyuan.com/my/course/310
例子:创建三个窗口卖票,总票数为100张
- 问题:卖票过程中,出现了重票、错票 -->出现了线程的安全问题
- 问题出现的原因:当某个线程操作车票的过程中,操作尚未完成时,其他线程参与进来,也操作车票。
- 如何解决:当一个线程a在操作ticket的时候,其他线程不能参与进来。直到线程a操作完ticket时,其他线程才可以开始操作ticket。这种情况即使线程a出现了阻塞,也不能被改变。
- 在Java中,我们可以通过同步机制,来解决线程的安全问题。
方式一:同步代码块
synchronized(同步监视器){
//需要被同步的代码
}
使用同步代码块解决继承Thread类的方式的线程安全问题:
/**
* 例子:创建三个窗口卖票,总票数为100张,使用继承Thread类的方式
*
* 说明:在继承Thread类创建多线程的方式中,慎用this充当同步监视器,考虑使用当前类充当同步监视器
*/
class Window2 extends Thread{
private static int ticket = 100; //100张票
private static Object object = new Object();
@Override
public void run() {
while (true){
//synchronized (object){
synchronized (Window2.class){
//错误的方式:synchronized (this){ this代表三个对象
if (ticket>0){
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":卖票,票号为:"+ticket);
ticket--;
}else{
break;
}
}
}
}
}
public class WindowTest2 {
public static void main(String[] args) {
Window2 window1 = new Window2();
Window2 window2 = new Window2();
Window2 window3 = new Window2();
window1.setName("窗口1");
window2.setName("窗口2");
window3.setName("窗口3");
window1.start();
window2.start();
window3.start();
}
}
使用同步代码块解决实现Runnable接口方式的线程安全问题:
//创建一个实现了Runnable接口的类
class Window1 implements Runnable{
private int ticket = 100; //100张票
//Object object = new Object();
Dog dog = new Dog();
//实现类去实现Runnable中的抽象方法:run()
@Override
public void run() {
while (true){
synchronized(this){ //此时的this:唯一的window1的对象 //方式二:synchronized(dog) {
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "号窗口:卖票,票号为:" + ticket);
ticket--;
} else {
break;
}
}
}
}
}
public class WindowTest1 {
public static void main(String[] args) {
//创建实现类的对象
Window1 window1 = new Window1();
//将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象
Thread thread1 = new Thread(window1);
Thread thread2 = new Thread(window1);
Thread thread3 = new Thread(window1);
thread1.setName("窗口1");
thread2.setName("窗口2");
thread3.setName("窗口3");
//通过Thread类的对象调用start():①启动线程 ②调用当前线程的run()
thread1.start();
thread2.start();
thread3.start();
}
}
class Dog{
}
方式二:同步方法
关于同步方法的总结:
- 同步方法仍然涉及到同步监视器,只是不需要我们显式的声明。
- 非静态的同步方法,同步监视器是:this
静态的同步方法,同步监视器是:当前类本身
使用同步方法处理继承Thread类的方式中的线程安全问题
class Window4 extends Thread {
private static int ticket = 100; //100张票
private static Object object = new Object();
@Override
public void run() {
while (true) {
show();
}
}
private static synchronized void show(){
//private synchronized void show(){ //同步监视器:window1、window2、window3
if (ticket>0){
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":卖票,票号为:"+ticket);
ticket--;
}
}
}
public class WindowTest4 {
public static void main(String[] args) {
Window4 window1 = new Window4();
Window4 window2 = new Window4();
Window4 window3 = new Window4();
window1.setName("窗口1");
window2.setName("窗口2");
window3.setName("窗口3");
window1.start();
window2.start();
window3.start();
}
}
使用同步方法解决实现Runnable接口的线程安全问题
class Window3 implements Runnable{
private int ticket = 100; //100张票
//实现类去实现Runnable中的抽象方法:run()
@Override
public void run() {
while (true){
show();
}
}
private synchronized void show(){ //同步监视器:this
//synchronized (this){
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "号窗口:卖票,票号为:" + ticket);
ticket--;
}
}
}
public class WindowTest3 {
public static void main(String[] args) {
//创建实现类的对象
Window3 window = new Window3();
//将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象
Thread thread1 = new Thread(window);
Thread thread2 = new Thread(window);
Thread thread3 = new Thread(window);
thread1.setName("窗口1");
thread2.setName("窗口2");
thread3.setName("窗口3");
//通过Thread类的对象调用start():①启动线程 ②调用当前线程的run()
thread1.start();
thread2.start();
thread3.start();
}
}