一道题,关于线程

ttt 2002-02-06 09:04:02
Which of the following best describes the use of the synhronized keyword?

1) Allows two process to run in paralell but to communicate with each other
2) Ensures only one thread at a time may access a method or object
3) Ensures that two or more processes will start and end at the same time
4) Ensures that two or more Threads will start and end at the same time

我觉得就2有点对,不过也不完全,应该不包括method吧,因为锁本来就是基于object的。
...全文
88 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttt 2002-02-06
  • 打赏
  • 举报
回复
看看这段:
The synchronized keyword can be used to mark a statement or block of code so that only one thread may execute an instance of the code at a time. Entry to the code is protected by a monitor lock around it. This process is implemented by a system of locks. You may also see the words monitor, or mutex (mutually exclusive lock) used. A lock is assigned to the object and ensures only one thread at a time can access the code. Thus when a thread starts to execute a synchronized block it grabs the lock on it. Any other thread will not be able to execute the code until the first thread has finished and released the lock. Note that the lock is based on the object and not on the method
Patrick_DK 2002-02-06
  • 打赏
  • 举报
回复
当然是2喽,这应该没有什么疑问吧
pengji 2002-02-06
  • 打赏
  • 举报
回复
2是对的!
judgement_sword 2002-02-06
  • 打赏
  • 举报
回复
谁说不包括method的,就是2,一点都没错。

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧