Java_基础—多线程(同步代码块和同步方法)

梦里梦外; 2022-06-09 09:44 387阅读 0赞

一、什么情况下需要同步?

  • 当多线程并发, 有多段代码同时执行时, 我们希望某一段代码执行的过程中CPU不要切换到其他线程工作. 这时就需要同步.
  • 如果两段代码是同步的, 那么同一时间只能执行一段, 在一段代码没执行结束之前, 不会执行另外一段代码.

二、同步代码块

  • 使用synchronized关键字加上一个锁对象来定义一段代码, 这就叫同步代码块
  • 多个同步代码块如果使用相同的锁对象, 那么他们就是同步的

    package com.soar.syn;

    public class Demo1_Synchronized {

    1. /*
    2. * 同步代码块
    3. */
    4. public static void main(String[] args) {
    5. final Printer p = new Printer(); //匿名内部类使用局部变量必须使用final修饰
    6. new Thread(){
    7. public void run(){
    8. while(true){
    9. p.print1();
    10. }
    11. }
    12. }.start();
    13. new Thread(){
    14. public void run(){
    15. while(true){
    16. p.print2();
    17. }
    18. }
    19. }.start();
    20. }

    }

    class Printer {

    1. Demo d = new Demo();
    2. public void print1(){
    3. synchronized(d){ //同步代码块,锁机制,锁对象可以是任意的
    4. System.out.println("S");
    5. System.out.println("o");
    6. System.out.println("a");
    7. System.out.println("r");
    8. System.out.println();
    9. }
    10. }
    11. public void print2(){
    12. //synchronized(new Demo()){ //锁对象不能用匿名对象,因为匿名对象不是同一个对象
    13. synchronized(d){
    14. System.out.println("S");
    15. System.out.println("i");
    16. System.out.println("r");
    17. System.out.println();
    18. }
    19. }

    }

    class Demo{

    }

三、同步方法的使用

使用synchronized关键字修饰一个方法, 该方法中所有的代码都是同步的,锁对象可以是任意对象,但是被锁的代码需要保证是同一把锁,不能用匿名对象

  • 非静态的同步方法的锁是this
  • 静态的同步方法的琐是字节码对象(类名.class)

    package com.soar.syn;

    public class Demo2_Synchronized {

    1. /*
    2. * 同步方法中的代码块
    3. */
    4. public static void main(String[] args) {
    5. final Printer p = new Printer(); //匿名内部类使用局部变量必须使用final修饰
    6. new Thread(){
    7. public void run(){
    8. while(true){
    9. p.print1();
    10. }
    11. }
    12. }.start();
    13. new Thread(){
    14. public void run(){
    15. while(true){
    16. p.print2();
    17. }
    18. }
    19. }.start();
    20. }

    }
    /*

    1. //非静态的同步方法的锁对象是什么?
    2. //非静态的同步方法的锁对象是this

    class Printer {

    1. public synchronized void print1(){ //同步方法只需要在方法上加synchronized关键字即可
    2. System.out.println("S");
    3. System.out.println("o");
    4. System.out.println("a");
    5. System.out.println("r");
    6. System.out.println();
    7. }
    8. public void print2(){
    9. synchronized(this){
    10. System.out.println("S");
    11. System.out.println("i");
    12. System.out.println("r");
    13. System.out.println();
    14. }
    15. }

    }*/

    //静态的同步方法的锁对象是什么呢?
    //是该类的字节码对象
    class Printer {

    1. public static synchronized void print1(){ //同步方法只需要在方法上加synchronized关键字即可
    2. System.out.println("S");
    3. System.out.println("o");
    4. System.out.println("a");
    5. System.out.println("r");
    6. System.out.println();
    7. }
    8. public static void print2(){
    9. synchronized(Printer.class){
    10. System.out.println("S");
    11. System.out.println("i");
    12. System.out.println("r");
    13. System.out.println();
    14. }
    15. }

    }

发表评论

表情:
评论列表 (有 0 条评论,387人围观)

还没有评论,来说两句吧...

相关阅读

    相关 线(12)同步方法同步

    在并发编程中,同步方法和同步块是两种常用的同步机制,它们用以确保多线程环境下对共享资源的访问是安全的,防止数据的不一致性和竞态条件的产生。本文将详细探讨这两种同步机制的工...