JAVA线程(好难把握的synchronized)

 1.       有关线程的一道题

package tengteng.org;

 

public class SynchronizedTest implements Runnable{

    int b = 100;

   

   

    public synchronized void m1(){

       b = 1000;

       try {

           Thread.sleep(5000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       System.out.println("m1中的b="+b);

    }

   

    public void m2(){

       System.out.println("m2中的b="+b);

    }

   

    public void run() {

       m1();

       try {

           Thread.sleep(5000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

    }

    public static void main(String[] args) {

       SynchronizedTest syt = new SynchronizedTest();

       Thread tr = new Thread(syt);

       tr.start();

       try {

           Thread.sleep(1000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       syt.m2();

    }

   

}

 

运行出来结果是什么呢?

m2中的b=1000

m1中的b=1000

很吃惊吧,不是已经被锁定了吗,怎么还可以访问。也许是一个BUG,不是!

记住:在synchronized语句块中的整体不能被访问,而m2可以被访问。(语句块中的修改能被访问到)

package tengteng.org;

 

public class SynchronizedTest implements Runnable{

    int b = 100;

   

   

    public synchronized void m1(){

       b = 1000;

       try {

           Thread.sleep(5000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       System.out.println("m1中的b="+b);

    }

   

    public void m2(){

        b = 2000;       

    }

   

    public void run() {

       m1();

       try {

           Thread.sleep(5000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

    }

    public static void main(String[] args) {

       SynchronizedTest syt = new SynchronizedTest();

       Thread tr = new Thread(syt);

       tr.start();

       try {

           Thread.sleep(1000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       syt.m2();

    }

   

}

 

运行结果:m1中的b=2000

说明:synchronized并不是一个完全锁,用同步的时候一定要认真考虑,仔细调试。

int b = 100;

   

    public synchronized void m1(){

       b = 1000;

       try {

           Thread.sleep(5000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       System.out.println("m1中的b="+b);

    }

   

    public synchronized void m2(){

       b = 2000;

      

    }

   

    public void run() {

       m1();

       try {

           Thread.sleep(5000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

    }

    public static void main(String[] args) {

       SynchronizedTest syt = new SynchronizedTest();

       Thread tr = new Thread(syt);

       tr.start();

       try {

           Thread.sleep(1000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       syt.m2();

       System.out.println("在方法的最后b:"+b);

    }

 

 

输出为:

m1中的b=1000

在方法的最后b:2000

在方法中用同步并不是想象的那么简单。

#############################################################

    int b = 100;

   

    public synchronized void m1(){

       b = 1000;

       try {

           Thread.sleep(5000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       System.out.println("m1中的b="+b);

       System.out.println("这里执行了m1");

    }

   

    public synchronized void m2(){

       try {

           Thread.sleep(2500);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       b = 2000;

       System.out.println("这里执行了m2");

      

    }

   

    public void run() {

       m1();

       try {

           Thread.sleep(5000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

    }

    public static void main(String[] args) {

       SynchronizedTest syt = new SynchronizedTest();

       Thread tr = new Thread(syt);

       tr.start();

       syt.m2();

       System.out.println("在方法的最后b:"+b);

    }

输出:

这里执行了m2

在方法的最后b:2000

m1中的b=1000

这里执行了m1

int b = 100;

   

    public synchronized void m1(){

       b = 1000;

       try {

           Thread.sleep(5000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       System.out.println("m1中的b="+b);

       System.out.println("这里执行了m1");

    }

   

    public synchronized void m2(){

       try {

           Thread.sleep(2500);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       b = 2000;

       System.out.println("这里执行了m2");

      

    }

   

    public void run() {

       m1();

       try {

           Thread.sleep(5000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

    }

    public static void main(String[] args) {

       SynchronizedTest syt = new SynchronizedTest();

       Thread tr = new Thread(syt);

       tr.start();

       try {

           Thread.sleep(1000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       syt.m2();

       System.out.println("在方法的最后b:"+b);

    }

输出:

m1中的b=1000

这里执行了m1

这里执行了m2

在方法的最后b:2000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值