synchronized使用情况

本文深入探讨Java中的synchronized关键字在不同场景下的行为,包括多个线程对同一对象或不同对象的访问方式,以及static修饰符如何改变同步效果。通过具体代码示例,展示了线程间的等待、交替执行和同步锁的概念。

未加static关键字

1)一对象两线程一方法【等待执行】

两个并发的线程访问同一个类Object中的synchronized同步代码块或者同步方法时,同一时间只能执行一个线程,另一个线程必须等待当前线程执行完才能执行。

import java.util.concurrent.TimeUnit;
public class Sync {
    public synchronized void test1(){
            for ( int i = 0; i < 5; i++) {
              System.out.println(Thread.currentThread().getName() + " " + i);
            }
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
    public static void main(String[] args) {
        Sync sync = new Sync();
        Thread thread1 = new Thread("线程A"){
            @Override
            public void run() {
                sync.test1();
            }
        };
        Thread thread2 = new Thread("线程B"){
            @Override
            public void run() {
                sync.test1();
            }
        };
        thread1.start();
        thread2.start();
    }
}

运行结果:
在这里插入图片描述

2)两对象两线程一方法【交替执行】

对象的创建是以类为模板,两个对象都有自己独立的方法,不同对象,分为两个线程,两个线程访问同一个方法,他们交替进行。

import java.util.concurrent.TimeUnit;

public class Sync {
    public synchronized void test1(){
        for ( int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Sync sync = new Sync();
        Sync sync1 = new Sync();
        Thread thread1 = new Thread("线程A"){
            @Override
            public void run() {
                sync.test1();
            }
        };
        Thread thread2 = new Thread("线程B"){
            @Override
            public void run() {
                sync1.test1();
            }
        };
        thread1.start();
        thread2.start();
    }
}

运行结果:
在这里插入图片描述

3)一对象两线程两方法【等待执行】

当一个线程访问了该类中的一个synchronized方法,另一个线程就不能再去访问其他的synchronized方法,只能执行完这一个再去继续执行。

import java.util.concurrent.TimeUnit;


public class Sync {
    public synchronized void test1(){
        for ( int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public synchronized void test2(){
        for ( int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Sync sync = new Sync();
        Sync sync1 = new Sync();
        Thread thread1 = new Thread("线程A"){
            @Override
            public void run() {
                sync.test1();
            }
        };
        Thread thread2 = new Thread("线程B"){
            @Override
            public void run() {
                sync.test2();
            }
        };
        thread1.start();
        thread2.start();
    }
}

运行结果:
在这里插入图片描述

4)一对象两线程两方法(有一非synchronized方法)【交替执行】

同一对象,当一个线程访问了一个类的synchronized方法,另一个对象访问其他的非synchronized方法,两个线程交替进行。

import java.util.concurrent.TimeUnit;


public class Sync {
    public synchronized void test1(){
        System.out.println(this);
        for ( int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public  void test2(){
        System.out.println(this);
        for ( int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Sync sync = new Sync();
        Sync sync1 = new Sync();
        Thread thread1 = new Thread("线程A"){
            @Override
            public void run() {
                sync.test1();
            }
        };
        Thread thread2 = new Thread("线程B"){
            @Override
            public void run() {
                sync.test2();
            }
        };
        thread1.start();
        thread2.start();
    }
}

运行结果:
在这里插入图片描述

加static关键字

5)一类一对象两线程两方法(一个方法static修饰,一个没有)【交替执行】

同一个类中,一个线程访问了加static的synchronized方法,另一个线程可以访问未加static修饰的synchronized方法,他们交替进行,因为static修饰后锁住的是类,所有对象共享,而没有加static锁住的是对象,只有每个对下那个私有。

import java.util.concurrent.TimeUnit;


public class Sync {
    public synchronized void test1(){
        System.out.println(this);
        for ( int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static synchronized void test2(){
        for ( int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Sync sync = new Sync();
        Sync sync1 = new Sync();
        Thread thread1 = new Thread("线程A"){
            @Override
            public void run() {
                sync.test1();
            }
        };
        Thread thread2 = new Thread("线程B"){
            @Override
            public void run() {
                //类调用类方法
                Sync.test2();
            }
        };
        thread1.start();
        thread2.start();
    }
}

运行结果:
在这里插入图片描述

6)一类两线程两方法【等待执行】

两个都被static修饰的synchronized方法,一个线程只能等待另一个线程执行完成后才能执行。

import java.util.concurrent.TimeUnit;


public class Sync {
    public static synchronized void test1(){
        for ( int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static synchronized void test2(){
        for ( int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Thread thread1 = new Thread("线程A"){
            @Override
            public void run() {
                Sync.test1();
            }
        };
        Thread thread2 = new Thread("线程B"){
            @Override
            public void run() {
                //类调用类方法
                Sync.test2();
            }
        };
        thread1.start();
        thread2.start();
    }
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值