`
aahyhaa
  • 浏览: 8485 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

什么时候用到synchronize关键字?

    博客分类:
  • Java
阅读更多

When should you synchronize? Apply Brian’s Rule of Synchronization

If you are writing a variable that might next be read by another thread, or reading a 

variable that might have last been written by another thread, you must use 

synchronization, and further, both the reader and the writer must synchronize using the 

same monitor lock. 

If you have more than one method in your class that deals with the critical data, you must 

synchronize all relevant methods. If you synchronize only one of the methods, then the 

others are free to ignore the object lock and can be called with impunity. This is an important 

point: Every method that accesses a critical shared resource must be synchronized or it 

won’t work right. 

分享到:
评论

相关推荐

    synchronize.zip

    在多线程环境下,为避免数据竞争和保证数据一致性,开发者会使用`synchronized`关键字来控制对共享资源的访问。这里的`SyncResultTask.java`可能是处理多个任务执行后的同步结果,确保数据的一致性和完整性。 2. **...

    超级有影响力霸气的Java面试题大全文档

    26、什么时候用assert。 assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制。在实现中,assertion就是在程序中的一条语句,它对一个boolean表达式进行检查,一个正确程序必须保证这...

    JSP填空题和判断题.doc

    19. JSP声明函数时,如果在前面加上synchronize关键字,功能是当前一个用户在执行该方法时,其他用户必须等待,直到该用户完成操作。 20. Page指令的属性Language的默认值是java。 21. 在Error.jsp页面中,要使用...

    java 面试题 总结

    23、什么时候用assert。 assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制。在实现中,assertion就是在程序中的一条语句,它对一个boolean表达式进行检查,一个正确程序必须保证这...

    NSLock和@synchronized

    在Objective-C中,我们通常会用到两种主要的互斥锁机制:NSLock和@synchronized关键字。这两个工具都用于在多线程环境下控制对共享资源的访问,防止数据竞争和死锁等问题的发生。接下来,我们将深入探讨这两种锁的...

    QQ空间多线程登陆-易语言

    在编写多线程登录程序时,开发者需要理解并掌握如“创建线程”(Thread.Create)、“线程同步”(Thread.Synchronize)等关键字的使用方法。此外,还需要熟悉易语言的错误处理机制,确保程序在遇到异常情况时能够...

Global site tag (gtag.js) - Google Analytics