Synchronized,Synchronized(this),Synchronized类实例锁,Synchronized类锁,Synchronized变量锁一试就懂

2019-03-05 16:28:12 浏览数 (2)

代码语言:javascript复制
public class TestSynchronized1 {
    
    public static void main(String[] args) {
        Fun1 fun = new Fun1();
        Fun1 fun1 = new Fun1();
        new Thread(new Runnable() {
            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                fun.runSync(name);
                System.out.println("thread end:"   name);
            }
        }, "thread-1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                fun.runThis(name);
                System.out.println("thread end:"   name);
            }
        }, "thread-2").start();

        /*TestSynchronized1 sss = new TestSynchronized1();
        Fun1 fun = new Fun1();
        new Thread(sss.new Thread1(fun),"thread-1").start();
        new Thread(sss.new Thread2(fun),"thread-2").start();*/
    }

    class Thread1 implements Runnable {
        private Fun1 fun;

        public Thread1(Fun1 fun) {
            this.fun = fun;
        }

        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            fun.runThis(name);
            System.out.println("thread end:"   name);
        }
    }

    class Thread2 implements Runnable {
        private Fun1 fun;

        public Thread2(Fun1 fun) {
            this.fun = fun;
        }

        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            fun.runThis(name);
            System.out.println("thread end:"   name);
        }
    }

}

class Fun1 {
/*
//同实例 不阻塞。不同实例 不阻塞。不是同一个锁
fun.runSync(name);
fun.runThis(name);

//同实例 阻塞。同实例 阻塞。同一个锁
fun.runS(name);
fun1.runThis(name);
 */

    //都不阻塞
    public void run(String name) {
        try {
            for (int i = 0; i < 5; i  ) {
                System.out.println("thread in:"   name);
                Thread.sleep(200);
                System.out.println("thread out:"   name);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //同实例 阻塞
    //不同实例 不阻塞
    public synchronized void runSync(String name) {
        try {
            for (int i = 0; i < 5; i  ) {
                System.out.println("thread in:"   name);
                Thread.sleep(200);
                System.out.println("thread out:"   name);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //同实例 阻塞
    //不同实例 阻塞
    public static synchronized void runS(String name) {
        try {
            for (int i = 0; i < 5; i  ) {
                System.out.println("thread in:"   name);
                Thread.sleep(200);
                System.out.println("thread out:"   name);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //同实例 阻塞
    //不同实例 阻塞
    public void runThis(String name) {
        synchronized (Fun1.class) {
            try {
                for (int i = 0; i < 5; i  ) {
                    System.out.println("thread in:"   name);
                    Thread.sleep(200);
                    System.out.println("thread out:"   name);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

0 人点赞