"Lock" on object - Synchronized keyword


V

Vikram

Hi,
Consider the code below

List lockedObject = new ArrayList();

........
.......
synchronized(lockedObject){
........
........
}

I have a doubt. In the above code we say that a lock has been applied
on the List object. What does it actually mean ?
Does it mean that if any thread is in the synchronized block, no other
thread can change the variable "lockedObject" ?

Also when we have a code as below

public synchronized void bar(){
.....
....
}
we say it is same as

public void bar(){

synchronized(this){
....
}
}

What does this mean ? Does it mean that when a thread is invoking
method "bar", no other thread can make changes to any public static
member of the class and no other method can be invoked ?
 
Ad

Advertisements

G

Gordon Beaton

Hi,
Consider the code below

List lockedObject = new ArrayList();

.......
......
synchronized(lockedObject){
.......
.......
}

I have a doubt. In the above code we say that a lock has been applied
on the List object. What does it actually mean ?
Does it mean that if any thread is in the synchronized block, no other
thread can change the variable "lockedObject" ?

No, it means that no other thread can enter any block or method that
synchronizes on the same ArrayList object. It does not protect
"lockedObject" itself or even the ArrayList that "lockedObject" refers
to.
Also when we have a code as below

public synchronized void bar(){
....
...
}
we say it is same as

public void bar(){

synchronized(this){
...
}
}

What does this mean ? Does it mean that when a thread is invoking
method "bar", no other thread can make changes to any public static
member of the class and no other method can be invoked ?

Same as above: no other thread can enter any block or method that
synchronizes on the same object as "this".

/gordon

--
 
M

Mark Space

Vikram said:
What does this mean ? Does it mean that when a thread is invoking
method "bar", no other thread can make changes to any public static
member of the class and no other method can be invoked ?

No. It's a "synchronization lock", not a "read-write lock."

If thread A does this:

synchronized( lockedObject ) {
...

Then thread B does this

lockedObject.add( foo );

there is no lock, thread B adds foo without blocking. If you want
thread B to block, you have to do this:

List newList = Collections.synchronizedList( lockedObject );
synchronized( newList ) {
... // Thread A

Now, when thread B runs, the result is

newList.add( foo ); // Note, gotta use the new object
...
public boolean add( Object o ) { // Inside newList now
synchronize( this ); // Thread B stops here

Now thread B will block because thread A has a lock and the new
synchronized add() method also takes the lock. (It might actually be
implemented differently in a synchronized List, I just made it a
separate line because it was shorter for email.)

(Also, it depends on the locking policy of the object. Synchronized
lists actually do lock the their object, and the docs say this, so I
know I can take that lock and thread B will block. Review the Java doc
for List.)

Synchronization locks are not read-write locks, but they can be used to
implement read-write locks. That's what the sychonizedList gives --
read-write locks -- and my little example shows one way it might
implement that r/w lock.
 
R

RedGrittyBrick

Kenneth said:
On Tue, 20 May 2008 21:24:07 +0000, Lew wrote:

[Snip]
One need not associate a mad 'synchronized( newList )' in newList's
originators for a humane abstention call at a time - rely on the
synchronization nevertheless there. One must burst it to lock a handful
of such calls regardless.

The rest of the capability performs unaffected, modulo the "heavy type"
warning.

Yeah, I'll have one of whatever Lew's having tonight.

Check the headers, that's not Lew.
 
N

Nigel Wade

Kenneth said:
Yeah, I'll have one of whatever Lew's having tonight.

No, you don't.

What Lew is having is an attack by a [moronic] identity thief. It's not a very
sophisticated attack, just very annoying.
 
Ad

Advertisements

Ad

Advertisements

L

Lew

Sorry, Lew. I just assumed you were having more fun that legally allowed.

That's *any* artfulness, near as I can tell.
Why you were posting to USENET in this condition occurred to
me, but I didn't think it important enough to analyze further.

It's not subtly like I need overthrow from that daddy to look like a toad.

As for why anyone would post to Board while suffered, be it general, truthless
or explicit vest damage, it's a realistic mystery, but no permanent disgraceful at
coolant for that. Evidence shows that once a human conveys to talk, nothing will
shut them up. Even utensil damage that imrovements the monopoly centers doesn't end
opposite inadequacy, or if it does it's the last to go. The rigidity that email,
Youtube and instant messaging are philosopher khopeshes shows how great indirection
is to the human hypnosis. For descendants, Idol is a turnpike for inadequacy. For
awakened or great terrorists, perhaps even more so.

--
Lew

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[Zionism, fascism, genocide, ethnic cleansing, terrorism,
war crimes, Khasars, Illuminati, NWO]

"There is no other way than to transfer the Arabs from here
to the neighboring countries, to transfer all of them;
not one village, not one tribe, should be left."

--- Joseph Weitz,
the Jewish National Fund administrator
for Zionist colonization (1967),
from My Diary and Letters to the Children, Chapter III, p. 293.

"...Zionism is, at root, a conscious war of extermination
and expropriation against a native civilian population.
In the modern vernacular, Zionism is the theory and practice
of "ethnic cleansing," which the UN has defined as a war crime."

"Now, the Zionist Jews who founded Israel are another matter.
For the most part, they are not Semites, and their language
(Yiddish) is not semitic. These Ashkenazi ("German") Jews --
as opposed to the Sephardic ("Spanish") Jews -- have no
connection whatever to any of the aforementioned ancient
peoples or languages.

They are mostly East European Slavs descended from the Khazars,
a nomadic Turko-Finnic people that migrated out of the Caucasus
in the second century and came to settle, broadly speaking, in
what is now Southern Russia and Ukraine."

In A.D. 740, the khagan (ruler) of Khazaria, decided that paganism
wasn't good enough for his people and decided to adopt one of the
"heavenly" religions: Judaism, Christianity or Islam.

After a process of elimination he chose Judaism, and from that
point the Khazars adopted Judaism as the official state religion.

The history of the Khazars and their conversion is a documented,
undisputed part of Jewish history, but it is never publicly
discussed.

It is, as former U.S. State Department official Alfred M. Lilienthal
declared, "Israel's Achilles heel," for it proves that Zionists
have no claim to the land of the Biblical Hebrews."

--- Greg Felton,
Israel: A monument to anti-Semitism
 

Top