首页 > 编程语言 > 详细

多线程(同步方法)学习笔记

时间:2018-08-08 19:51:22      阅读:190      评论:0      收藏:0      [点我收藏+]
使用synchronized关键字修饰一个方法, 该方法中所有的代码都是同步的 class Printer { public static void print1() { synchronized(Printer.class){ //锁对象可以是任意对象,但是被锁的代码需要保证是同一把锁,不能用匿名对象 System.out.print("黑"); System.out.print("马"); System.out.print("程"); System.out.print("序"); System.out.print("员"); System.out.print("\r\n"); } } /* * 非静态同步函数的锁是:this * 静态的同步函数的锁是:字节码对象 */ public static synchronized void print2() { System.out.print("传"); System.out.print("智"); System.out.print("播"); System.out.print("客"); System.out.print("\r\n"); } }

多线程(同步方法)学习笔记

原文:http://blog.51cto.com/357712148/2156473

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!