线程锁

如果程序块锁用this,会不会和方法锁互斥?  -- 不会。

package com.shtel.iptv3a.webservice;

public class Main2 {
    public static void main(String[] args) {
        final Student s = new Student();
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                s.a();
            }
        });
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                s.b();
            }
        });
        t1.start();t2.start();
    }
    
    
    public static class Student{
        
        public void a() {
            System.out.println("a");
            synchronized (this) {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("a end");
        }
        
        public synchronized void b() {
            System.out.println("b");
        }
    }
}

 

posted on 2018-12-14 22:26  angelshelter  阅读(110)  评论(0编辑  收藏  举报

导航