@@ -268,9 +268,41 @@ $(H3 $(LNAME2 objc-member-functions, Objective-C linkage))
268
268
)
269
269
270
270
271
- $(H2 $(LNAME2 synchronized-classes , Synchronized Classes ))
271
+ $(H2 $(LNAME2 synchronized-methods , Synchronized Method Calls ))
272
272
273
- $(P All member functions of synchronized classes are synchronized.
273
+ $(P Member functions of a (non-`synchronized`) class can be individually
274
+ marked as `synchronized`.
275
+ The class instance's monitor object will be locked when the method is
276
+ called and unlocked when the call terminates.
277
+ )
278
+
279
+ $(P A synchronized method can only be called on a
280
+ $(DDSUBLINK spec/const3, shared, `shared`) class instance.)
281
+
282
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
283
+ ---
284
+ class C
285
+ {
286
+ void foo();
287
+ synchronized int bar();
288
+ }
289
+
290
+ void test(C c)
291
+ {
292
+ c.foo; // OK
293
+ //c.bar; // Error, `c` is not `shared`
294
+
295
+ shared C sc = new shared C;
296
+ //sc.foo; // Error, `foo` not callable using a `shared` object
297
+ sc.bar; // OK
298
+ }
299
+ ---
300
+ )
301
+ $(P See also $(GLINK2 statement, SynchronizedStatement).)
302
+
303
+ $(H3 $(LNAME2 synchronized-classes, Synchronized Classes))
304
+
305
+ $(P Each member function of a `synchronized` class is implicitly `synchronized`.
274
306
A static member function is synchronized on the $(I classinfo)
275
307
object for the class, which means that one monitor is used
276
308
for all static member functions for that synchronized class.
@@ -297,24 +329,15 @@ $(H2 $(LNAME2 synchronized-classes, Synchronized Classes))
297
329
}
298
330
}
299
331
---
300
-
301
- $(P Member functions of non-synchronized classes can be individually marked as synchronized.
302
- )
303
-
304
- ---
305
- class Foo
306
- {
307
- synchronized void foo() { } // foo is synchronized
308
- }
309
- ---
332
+ $(NOTE `bar` uses a $(GLINK2 statement, SynchronizedStatement).)
310
333
311
334
$(P Member fields of a synchronized class cannot be public:
312
335
)
313
336
314
337
---
315
338
synchronized class Foo
316
339
{
317
- int foo; // disallowed : public field
340
+ int foo; // Error : public field
318
341
}
319
342
320
343
synchronized class Bar
@@ -323,8 +346,8 @@ $(H2 $(LNAME2 synchronized-classes, Synchronized Classes))
323
346
}
324
347
---
325
348
326
- $(P The $(D synchronized) attribute can only be applied to classes,
327
- structs cannot be marked to be synchronized.)
349
+ $(NOTE struct types cannot be marked `synchronized`.)
350
+
328
351
329
352
$(H2 $(LNAME2 constructors, Constructors))
330
353
0 commit comments