1.线程安全
线程安全问题的产生:多线程操作同一个共享资源,就会产生对该资源的抢占,会造成多线程数据不一致的安全问题。
2.同步锁
同步锁:多线程并发访问共享数据时,保证同一时刻只能有一个线程能访问资源。
Synchronized:又可以叫做内置锁
给内置锁一个解释:synchronized是java内置于JDK的,底层实现是native,加锁、解锁都是自动完成,不需要用户显示的控制,非常方便。
用途:可以使用在方法或代码块上,保证同一时刻,只有一个线程处于方法和代码块中,保证线程对变量访问的可见性和排他性。
3.同步锁的应用
如果不加锁的结果
package SynchronizedTest;
public class SynchronizedTest {
public void show() {
System.out.print(Thread.currentThread().getName()+":");
System.out.print("W");
System.out.print("e");
System.out.print("l");
System.out.print("c");
System.out.print("o");
System.out.print("m");
System.out.print("e");
System.out.print(" to ");
System.out.print("Shang");
System.out.println("Hai");
}
}
package SynchronizedTest;
public class test1 {
public static void main(String[] args) {
SynchronizedTest synchronizedTest = new SynchronizedTest();
Thread thread1 = new Thread(() -> {
for (;;) {
synchronizedTest.show();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread thread2 = new Thread(() -> {
for (;;) {
synchronizedTest.show();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread1.start();
thread2.start();
}
}
加锁之后的结果是
package SynchronizedTest;
public class SynchronizedTest {
private Object object = new Object();
public void show() {
synchronized (object) {
System.out.print(Thread.currentThread().getName()+":");
System.out.print("W");
System.out.print("e");
System.out.print("l");
System.out.print("c");
System.out.print("o");
System.out.print("m");
System.out.print("e");
System.out.print(" to ");
System.out.print("Shang");
System.out.println("Hai");
}
}
}
多线程竞争共享资源的例子:
案例1:四个窗口共同售卖100张票
分析:每个窗口独立完成同样的售票行为,因此四个窗口为四个线程, 有一个共同点的资源:票的数量。
package SaleTickets;
public class window extends Thread{ private static int num = 100;
public window(String name) {
super(name);
}
@Override
public void run() {
while(true) {
synchronized (window.class) {
if (num > 0) {
System.out.println(Thread.currentThread().getName() + "正在售卖第" + num + "号票");
num --;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
System.out.println(Thread.currentThread().getName() + "系统的票已经售完!!");
break;
}
}
}
}
}
package SaleTickets;
public class test {
public static void main(String[] args) {
window window1 = new window("窗口1");
window window2 = new window("窗口2");
window window3 = new window("窗口3");
window window4 = new window("窗口4");
window1.start();
window2.start();
window3.start();
window4.start();
}
}
synchronized位于while(true)里面,四个线程会在synchronized处等待,谁能拿到锁是不确定的,但如果位于while(true)外面 ,一个线程进去会执行完,直到break结束循环。
案例2:模拟库存超卖的问题
package ShopSale;
public class shop{
private static int stock =2;
public synchronized void sale() {
// synchronized(this) {
if (stock > 0) {
System.out.println(Thread.currentThread().getName() +"成功购买一件商品");
stock -- ;
System.out.println(",还剩余"+stock+"件商品");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
System.out.println("很抱歉,"+Thread.currentThread().getName()+"没有抢到商品,库存不足!!!");
}
// }
}
}
package ShopSale;
public class test {
public static void main(String[] args) {
shop sp = new shop();
Thread thread1 = new Thread(() -> {
sp.sale();
}) ;
Thread thread2 = new Thread(() -> {
sp.sale();
}) ;
Thread thread3 = new Thread(() -> {
sp.sale();
}) ;
thread1.start();
thread2.start();
thread3.start();
}
}
总结synchronized的两种锁:首先无论哪一种锁,有一个前提条件是,一定要使得锁具有唯一性。
两种锁:类锁和对象锁。类锁就如案例1那样,以类.class为锁,它是唯一的,因为在程序运行时,会把类先加载到方法区,加载字节码,每个类的字节码文件是唯一的。第二种就是类锁,类锁就像案例2那样,用类作为锁的对象,案例2用this,因为只new了一次shop,是唯一的,但this不能用在案例1,不唯一。
另外,synchronized加载普通方法是对象锁,加载static方法上是类锁。