程序设计 选择题 较难 ★ 精品

synchronized 同步机制 — 程序设计选择题

Java 程序设计(大学通用) 多线程基础 AI 三重审校通过

题目
某票务系统有 total = 100 张共享票,两个线程 t1、t2 均执行如下售票方法 sell():方法体内用 synchronized (this) 包裹「判断 total > 0 后执行 total--」的操作,t1 与 t2 调用的是同一个 Ticket 对象的同一方法。另外类中还有一个方法 check(),其内部的「读 total」操作用 synchronized (另一个普通 Object 对象 lock2) 包裹。以下说法正确的是:A. sell() 内的同步代码块失效,total-- 仍可能出现竞态条件 B. sell() 之间能正确互斥,但 check() 与 sell() 之间不互斥 C. t1 与 t2 会因锁对象不同而发生死锁 D. check() 与 sell() 之间也互斥,因为都在访问 total