synchornized的使用

synchornized的使用场景

1、两个线程同时访问同一个对象的同步方法 安全
2、两个线程同时访问两个对象的同步方法 不安全 test1.func1() test2.func1()
3、两个线程同时访问(一个或两个)对象的静态同步方法 安全
4、两个线程分别同时访问(一个或两个)对象的同步方法和非同步方法 不安全
5、两个线程访问同一个对象中的同步方法,同步方法又调用另外一个非同步方法 不安全
6、两个线程同时访问同一个对象的不同的同步方法 安全
7、两个线程同时访问静态synchronized和非静态synchornized方法 不安全
8、同步方法抛出异常,JVM会自动释放锁

package thread;

import java.util.concurrent.TimeUnit;
public class TestDemo8 {
    
    
    public synchronized void fun1(){
    
    
        System.out.println(Thread.currentThread().getName()+":: 同步方法fun1开始");
        try {
    
    
            TimeUnit.MILLISECONDS.sleep(1000);
//        fun3();
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+":: 同步方法fun1结束");
    }

    public synchronized static void fun2() throws Exception {
    
    
        //类锁  class对象
        System.out.println(Thread.currentThread().getName()+":: 静态同步方法func2开始");
        try {
    
    
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+":: 静态同步方法func2结束");
    }

    public void fun3(){
    
    
        System.out.println(Thread.currentThread().getName()+":: 非同步方法func3");
        try {
    
    
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+":: 非同步方法func3");
    }

    public synchronized void fun4(){
    
    
        System.out.println(Thread.currentThread().getName()+":: 同步方法fun4开始");
        try {
    
    
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+":: 同步方法func4结束");
    }
    public static void main(String[] args) {
    
    
        TestDemo8 test1 = new TestDemo8();
        TestDemo8 test2 = new TestDemo8();
        new Thread("A"){
    
    
            @Override
            public void run() {
    
    
                test1.fun1();  //unlock
                test1.fun3();
            }
        }.start();

        new Thread("B"){
    
    
            @Override
            public void run() {
    
    
                test1.fun1(); //lock
                test1.fun3();
            }
        }.start();

//        Thread threadc = new Thread("C"){
    
    
//            @Override
//            public void run() {
    
    
//                test1.fun1();
//            }
//        };
//        threadc.start();
//        threadc.interrupt();
//
//        new Thread("D"){
    
    
//            @Override
//            public void run() {
    
    
//                test1.fun1();
//            }
//        }.start();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41571459/article/details/113939216