引入锁机制以解决线程安全问题:
悲观锁:当存在多个线程操作共享数据时,需要保证同一时刻有且只有一个线程在操作共享数据,其他线程必须等到该线程处理完数据后再进行
乐观锁:CAS compare and set
在添加synchronized关键字后就可以保证在一个时刻上只有一个线程在调用某个方法或者代码块,不会出现并发的情形,达到排队执行的效果。
在Java中synchronized可保证在同一个时刻,只有一个线程可以执行某个方法或者某个代码块(主要是对方法或者代码块中存在共享数据的操作),同时还应该注意到synchronized另外一个重要的作用,synchronized可保证一个线程的变化(主要是共享数据的变化)被其他线程所看到(保证可见性,完全可以替代volatile功能),这点确实也是很重要的。
对象在内存中存储时可以分为对象头、实例数据和对齐字节三部分。对象头数据一般包含标识字mark word和类型指针klass pointer;实例数据就是具体对象的成员数据,一般按照4B为的单位进行数据存储;最后的对齐字节用于将对象存储的数据凑够8字节的整数倍。
synchronized用于实现同步处理,保证共享数据的安全性
数据有安全性问题的原因:1、共享数据 2、修改数据
synchronized相对于volatile是重量级的线程安全的方法,可以保证3大特性:原子性、可见性、有序性。
可以将并发操作转换为串型执行
- synchronized同步方法 在方法上添加同步关键字,当前的锁对象为当前对象---对象锁
- public class A2 {
- public static void main(String[] args) throws Exception {
- NumOper no = new NumOper(100);
- Thread[] ts = new Thread[4];
- for (int i = 0; i < 2; i++) {
- ts[i * 2] = new Thread(() -> {
- for (int k = 0; k < 50; k++)
- no.add();
- });
- ts[i * 2].start();
- ts[i * 2 + 1] = new Thread(() -> {
- for (int k = 0; k < 50; k++)
- no.sub();
- });
- ts[i * 2 + 1].start();
- }
- for (Thread tmp : ts)
- tmp.join();
- System.out.println("Main:" + no.getNum());
- }
- }
-
- /*
- * 以new出来的NumOper对象充当锁,当前对象内的所有synchronized方法在不同线程调用时互斥,
- * 但是可以直接访问非synchronized方法。注意synchronized允许持有锁的线程重入
- */
- class NumOper {
- private int num;
-
- public NumOper(int num) { //synchronized不能添加在构造器上
- this.num = num;
- }
-
- public synchronized void add() {
- System.out.println(Thread.currentThread() + "....add...begin:" + this.num);
- this.num++;
- sub();
- System.out.println(Thread.currentThread() + "....add...end" + this.num);
- }
-
- public synchronized void sub() {
- System.out.println(Thread.currentThread() + "....sub...begin" + this.num);
- this.num--;
- System.out.println(Thread.currentThread() + "....sub...end" + this.num);
- }
-
- public int getNum() {
- return this.num;
- }
- }
- synchronized同步静态方法 以当前类Class对象作为锁---类锁
针对一个类一般只会存储一个
- public class A3 {
- public static void main(String[] args) {
- for (int i = 0; i < 5; i++) {
- new Thread(() -> {
- for (int k = 0; k < 10; k++)
- new S3();
- }).start();
- }
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(S3.getCounter());
- }
- }
-
- class S3 {
- private static int counter = 0;
-
- public S3() {
- add();
- }
-
- private synchronized static void add() {
- System.out.println(Thread.currentThread() + "开始创建操作" + counter);
- counter++;
- eee();
- System.out.println(Thread.currentThread() + "完成创建操作" + counter);
- }
- public synchronized static void eee() {
-
- }
- public static int getCounter() {
- return counter;
- }
- }
- synchronized同步代码块 自定义对象充当锁
- public class A {
- public static void main(String[] args) {
- for(int i=0;i<3;i++) {
- Thread t1=new MyThread("第"+(i+1)+"个售票窗口");
- t1.start();
- }
- }
- }
-
- class MyThread extends Thread {
- private String name;
- private static int counter = 20;
- private final static String LOCK="lock1";
-
- public MyThread(String name) {
- this.name = name;
- }
-
- @Override
- public void run() {
- while (counter > 0) {
- try {
- sleep(100);// 模拟售票过程,加剧出错的可能性
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- synchronized (LOCK) {
- if (counter > 0) {
- System.out.println(this.name + "售出第" + counter + "号票");
- // synchronized (LOCK) {
- counter--;
- // }
- } else {
- System.out.println("票已经售尽!");
- }
- }
- }
- }
- }
1:使用类锁,所以不管new了多少个对象,都可以得到互斥的效果
-
-
- public class A4 {
- public static void main(String[] args) {
- new Thread(() -> {
- for (int i = 0; i < 100; i++) {
- S4 ss = new S4();
- ss.pp1();
- }
- }).start();
- new Thread(() -> {
- for (int i = 0; i < 100; i++) {
- S4 ss = new S4();
- ss.pp1();
- }
- }).start();
- }
- }
-
- class S4 {
- public synchronized static void pp1() {
- System.out.println(Thread.currentThread() + "...begin...");
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread() + "...end...");
- }
-
- public void pp2() {
- }
- }
-
2:使用的是对象锁,所以只能new一个对象,才可以得到互斥的效果。如果创建多个则不能达到互斥的目的
- public class A41 {
- public static void main(String[] args) {
- S41 ss = new S41();
- new Thread(() -> {
- // S41 ss=new S41();
- for (int i = 0; i < 100; i++) {
- // S41 ss = new S41();
- ss.pp2();
- }
- }).start();
- new Thread(() -> {
- // S41 ss=new S41();
- for (int i = 0; i < 100; i++) {
- // S41 ss = new S41();
- ss.pp2();
- }
- }).start();
- }
- }
-
- class S41 {
- public synchronized static void pp1() {
- System.out.println(Thread.currentThread() + "pp1...begin...");
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread() + "pp1...end...");
- }
-
- public synchronized void pp2() {
- System.out.println(Thread.currentThread() + "pp2...begin...");
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread() + "pp2...end...");
- }
- }
3:使用的是不同的锁,所以thread0和thread1不能达到互斥的目的
- public class A42 {
- public static void main(String[] args) {
- S42 ss = new S42();
- new Thread(() -> {
- for (int i = 0; i < 100; i++) {
- ss.pp2(); //非static -- 对象锁 ss
- }
- }).start();
- new Thread(() -> {
- for (int i = 0; i < 100; i++) {
- ss.pp1();//static --类锁 S42.class
- }
- }).start();
- }
- }
-
- class S42 {
- public synchronized static void pp1() {
- System.out.println(Thread.currentThread() + "pp1...begin...");
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread() + "pp1...end...");
- }
-
- public synchronized void pp2() {
- System.out.println(Thread.currentThread() + "pp2...begin...");
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread() + "pp2...end...");
- }
- }