Skip to content

Commit 6465a19

Browse files
ntreldlang-bot
authored andcommitted
[spec/class] Update synchronized method docs
Explain how they work. Add example.
1 parent d50330a commit 6465a19

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

spec/class.dd

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,41 @@ $(H3 $(LNAME2 objc-member-functions, Objective-C linkage))
268268
)
269269

270270

271-
$(H2 $(LNAME2 synchronized-classes, Synchronized Classes))
271+
$(H2 $(LNAME2 synchronized-methods, Synchronized Method Calls))
272272

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`.
274306
A static member function is synchronized on the $(I classinfo)
275307
object for the class, which means that one monitor is used
276308
for all static member functions for that synchronized class.
@@ -297,24 +329,15 @@ $(H2 $(LNAME2 synchronized-classes, Synchronized Classes))
297329
}
298330
}
299331
---
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).)
310333

311334
$(P Member fields of a synchronized class cannot be public:
312335
)
313336

314337
---
315338
synchronized class Foo
316339
{
317-
int foo; // disallowed: public field
340+
int foo; // Error: public field
318341
}
319342

320343
synchronized class Bar
@@ -323,8 +346,8 @@ $(H2 $(LNAME2 synchronized-classes, Synchronized Classes))
323346
}
324347
---
325348

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+
328351

329352
$(H2 $(LNAME2 constructors, Constructors))
330353

0 commit comments

Comments
 (0)