共有100张票,4个窗口同时卖票
1、是多线程环境
2、有共享数据
3、有多条语句操作数据
满足三个条件会出现安全问题
解决办法:
同步代码块
synchronized(任意对象){
多条语句操作共享数据的代码
}
public class ylh1 implements Runnable {
private static int tickets = 100;
Object o=new Object();
@Override
public void run() {
while (true) {
synchronized (o) {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在售票第" + tickets + "张");
tickets--;
}
}
}
}
}
public class demo {
public static void main(String[] args) {
ylh1 y=new ylh1();
Thread t1=new Thread(y,"窗口1");
Thread t2=new Thread(y,"窗口2");
Thread t3=new Thread(y,"窗口3");
t1.start();
t2.start();
t3.start();
}
}
不会出现重复卖票了
同步方法:
就是把synchronized关键字加到方法上
package com;
public class ylh1 implements Runnable {
private static int tickets = 100;
Object o=new Object();
int a=0;
@Override
public void run() {
while (true) {
if(a%2==0){
synchronized (o) {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在售票第" + tickets + "张");
tickets--;
}
}
}
else {
sell();
}
a++;
}
}
private static synchronized void sell () {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在售票第" + tickets + "张");
tickets--;
}
}
}
出现问题,因为同步代码块锁的的o对象
非静态同步方法的锁对象是this
所以将o改为this即可解决
静态同步方法的锁对象是类的字节码文件
如下:
package com;
public class ylh1 implements Runnable {
private static int tickets = 100;
Object o=new Object();
int a=0;
@Override
public void run() {
while (true) {
if(a%2==0){
synchronized (ylh1.class) {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在售票第" + tickets + "张");
tickets--;
}
}
}
else {
sell();
}
a++;
}
}
private static synchronized void sell () {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在售票第" + tickets + "张");
tickets--;
}
}
}
public class demo {
public static void main(String[] args) {
ylh1 y=new ylh1();
Thread t1=new Thread(y,"窗口1");
Thread t2=new Thread(y,"窗口2");
Thread t3=new Thread(y,"窗口3");
t1.start();
t2.start();
t3.start();
}
}