Locking an Object

xiaoxiao2021-03-06  15

Public class cubbyhole {

PRIVATE INT CONTENTS;

Private boolean available = false;

Public synchronized int GET (INT whO) {

...

}

Public Synchronized Void Put (int who, int value) {

...

}

}

The Method Declarations for Both

Put and

Get Contain THE

SYNCHRONIZED Keyword.

Whenever control enters a synchronized method, the thread that called the method locks the object whose method has been called. Other threads can not call a synchronized method on the same object until the object is unlocked.

转载请注明原文地址:https://www.9cbs.com/read-46232.html

New Post(0)