java并发相关

Java Synchronzied

2018-09-25  本文已影响13人  韭菜待收割

Synchronzied关键字的作用是线程同步,它可以用来修饰对象中的方法,将对象加锁。

1、Synchronzied的几个分类

1)非静态方法的同步

//每个对象各持有一把锁

//懒,就写一起了
public class MainTest {
    //测试方法
    public static void main(String[] args) throws Exception {
        MainTest mainTest = new MainTest();

        //mainTest.synchronizedTest1();
        mainTest.synchronizedTest2();
    }

    public void synchronizedTest2() {
        //2、2个对象  每个对象各持有一把锁
        Test test = new Test();

        Test test2 = new Test();
        MyRunale myRunale2 = new MyRunale(test2, 2);
        Thread thread2 = new Thread(myRunale2);
        thread2.start();

        test.test1();
        //会打印2条记录
    }

    public void synchronizedTest1() {
        //1、同一个对象  只持有一把锁
        //只持有一把锁
        Test test = new Test();
        MyRunale myRunale2 = new MyRunale(test, 2);
        Thread thread2 = new Thread(myRunale2);
        thread2.start();
        test.test1();
        //只会打印1条记录
    }

    public class MyRunale implements Runnable {
        private Test test;
        private int type;

        public MyRunale(Test test, int type) {
            this.test = test;
            this.type = type;
        }

        @Override
        public void run() {
            if (type == 1) {
                test.test1();
            } else {
                test.test2();
            }

        }
    }

    public class Test {
        public synchronized void test1() {
            System.out.println("我是test1");
            try {
                Thread.sleep(10000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public synchronized void test2() {
            System.out.println("我是test2");
            try {
                Thread.sleep(10000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

2)静态方法的同步

//所有静态方法的锁也只有一个
//静态方法的锁和 非静态方法的锁是不一样的

//懒,就写一起了
public class MainTest {
    //测试方法
    public static void main(String[] args) throws Exception {
        MainTest mainTest = new MainTest();

        //mainTest.synchronizedTest1();
        mainTest.synchronizedTest2();
    }

    public void synchronizedTest2() {
        //静态方法的锁和 非静态方法的锁是不一样的
        System.out.println("-------------synchronizedTest2开始-------------");
        Test test = new Test();

        MyRunale myRunale2 = new MyRunale(test, 1);
        Thread thread2 = new Thread(myRunale2);
        thread2.start();

        test.test0();
        System.out.println("-------------synchronizedTest2结束-------------");
        //会打印2条记录
    }

    public void synchronizedTest1() {
        //所有静态方法的锁也只有一个
        System.out.println("-------------synchronizedTest1开始-------------");
        Test test = new Test();

        Test test2 = new Test();
        MyRunale myRunale2 = new MyRunale(test2, 0);
        Thread thread2 = new Thread(myRunale2);
        thread2.start();

        test.test0();
        System.out.println("-------------synchronizedTest1结束-------------");
        //会打印1条记录
    }

    public class MyRunale implements Runnable {
        private Test test;
        private int type;

        public MyRunale(Test test, int type) {
            this.test = test;
            this.type = type;
        }

        @Override
        public void run() {
            if (type == 1) {
                test.test1();
            } else if (type == 2) {
                test.test2();
            } else if (type == 0) {
                test.test00();
            }
        }
    }
}

public class Test {
    public static synchronized void test00() {
        System.out.println("我是test00");
        try {
            Thread.sleep(10000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static synchronized void test0() {
        System.out.println("我是test0");
        try {
            Thread.sleep(10000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void test1() {
        System.out.println("我是test1");
        try {
            Thread.sleep(10000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void test2() {
        System.out.println("我是test2");
        try {
            Thread.sleep(10000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

3)类的同步

//同步块中的锁只有一个,即使是多个对象也只有一个
public void method() { 
  synchronized(SomeClass.class) { 
   
  } 
} 
//懒,就写一起了
public class MainTest {
    //测试方法
    public static void main(String[] args) throws Exception {
        MainTest mainTest = new MainTest();

        mainTest.synchronizedTest1();
    }

    public void synchronizedTest1() {
        Test test2 = new Test();
        MyRunale myRunale2 = new MyRunale(test2, 2);
        Thread thread2 = new Thread(myRunale2);
        thread2.start();

        Test test = new Test();
        test.test1();
//        -----test1开始-----
//        -----test1执行中-----
//        -----test2开始-----
    }

    public class MyRunale implements Runnable {
        private Test test;
        private int type;

        public MyRunale(Test test, int type) {
            this.test = test;
            this.type = type;
        }

        @Override
        public void run() {
            if (type == 1) {
                test.test1();
            } else {
                test.test2();
            }

        }
    }
}

public class Test {

    public void test1() {
        System.out.println("-----test1开始-----");
        synchronized(Test.class){
            try {
                System.out.println("-----test1执行中-----");
                Thread.sleep(10000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("-----test1结束-----");
    }

    public void test2() {
        System.out.println("-----test2开始-----");
        synchronized(Test.class){
            try {
                System.out.println("-----test2执行中-----");
                Thread.sleep(10000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("-----test2结束-----");
    }
} 

4)公共对象的同步

//没有static则每个test对象对于lock对象都有不同的锁
private static Object lock=new Object();
public void method() { 
  synchronized(lock) { 
   
  } 
} 

//懒,就写一起了
public class MainTest {
    //测试方法
    public static void main(String[] args) throws Exception {
        MainTest mainTest = new MainTest();

        mainTest.synchronizedTest1();
    }

    public void synchronizedTest1() {
        Test test2 = new Test();
        MyRunale myRunale2 = new MyRunale(test2, 2);
        Thread thread2 = new Thread(myRunale2);
        thread2.start();

        Test test = new Test();
        test.test1();
//        -----test1开始-----
//        -----test1执行中-----
//        -----test2开始-----
    }

    public class MyRunale implements Runnable {
        private Test test;
        private int type;

        public MyRunale(Test test, int type) {
            this.test = test;
            this.type = type;
        }

        @Override
        public void run() {
            if (type == 1) {
                test.test1();
            } else {
                test.test2();
            }

        }
    }
}

public class Test {
    //没有static则每个test对象对于lock对象都有不同的锁
    private static Object lock = new Object();

    public void test1() {
        System.out.println("-----test1开始-----");
        synchronized (lock) {
            try {
                System.out.println("-----test1执行中-----");
                Thread.sleep(10000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("-----test1结束-----");
    }

    public void test2() {
        System.out.println("-----test2开始-----");
        synchronized (lock) {
            try {
                System.out.println("-----test2执行中-----");
                Thread.sleep(10000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("-----test2结束-----");
    }
}
上一篇下一篇

猜你喜欢

热点阅读