当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法

当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法
对象的synchronized方法不能进入了,但它的其他非synchronized方法还是可以访问的。

原因很简单:锁对象只有一个  一次只能被一个线程拥有 而访问非同步方法不需要锁对象

 1 public class TT implements Runnable {
 2      int b = 100;
 3      public synchronized void m1(){
 4             b = 10000;
 5             try {
 6                 Thread. sleep(5000);
 7                 System. out.println( "m1----b="+ b);
 8            } catch (InterruptedException e) {
 9                 e.printStackTrace();
10            }
11      }
12      public void m2(){
13            System. out.println( "m2====="+ b);
14      }
15      @Override
16      public void run() {
17            m1();
18      }
19      public static void main(String[] args) {
20            TT tt = new TT();
21            Thread t = new Thread(tt);
22            t.start();
23             try {
24                 Thread. sleep(1000);
25            } catch (InterruptedException e) {
26                 e.printStackTrace();
27            }
28            tt.m2();
29      }
30 }

 

posted @ 2015-08-28 15:35  yweihainan  阅读(1860)  评论(0编辑  收藏  举报