472,122 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

Synchronized Keywords in Variables

Hi,

In the javadocs regarding many of the java.util classes, it states
that the classes are not synchronized, and suggest using the
Collections.synchronizedX(...) methods for getting synchronized
objects. However, why couldn't one simply declare:

private synchronized LinkedList l;

and have the variable be automatically synchronized, instead of having

private List l = Collections.synchronizedList(new LinkedList());

Thanks for any help,
Frank
Jul 17 '05 #1
2 14446
You can't use the synchronized keyword in a variable declaration, only
in a method delaration. You could make your LinkedList variable
private, create a public synchronized method that "gets" the variable
and only use that method when accessing the LinkedList... it would
amount to the same thing.

-Nathan

tr*******@hotmail.com (Frank) wrote in message news:<c4**************************@posting.google. com>...
Hi,

In the javadocs regarding many of the java.util classes, it states
that the classes are not synchronized, and suggest using the
Collections.synchronizedX(...) methods for getting synchronized
objects. However, why couldn't one simply declare:

private synchronized LinkedList l;

and have the variable be automatically synchronized, instead of having

private List l = Collections.synchronizedList(new LinkedList());

Thanks for any help,
Frank

Jul 17 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nathan Zumwalt wrote:
You can't use the synchronized keyword in a variable declaration,
only
in a method delaration. You could make your LinkedList variable
private, create a public synchronized method that "gets" the
variable and only use that method when accessing the LinkedList...
it would amount to the same thing.

-Nathan


Hi,
Sorry, but it actually wouldn't be the same thing. I think it's really
important to realize this. The only thing this would do, is make sure
that only one thread could GET the object at a time, it would not
determine what could be done with it once gotten. For example:

- --THREAD1--
getList().add(x);

- --THREAD2--
getList().add(y);

You could have something like this happen:

- --THREAD1-- --THREAD2--
getList() WAITING
NOT SCHEDULED getList()
add() add()

This satisfies the requirement that only one thread is executing
getList() at a time, but it DOESN'T have any impact on what either
thread does with the List once it's returned.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/kB2/wxczzJRavJYRAkqmAJ4sW/83G265OeeGJ7CpkRK3PDzSsgCg9lJI
hepCkjbs3FKJ4/YSPCGn504=
=dJhI
-----END PGP SIGNATURE-----
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Hal Vaughan | last post: by
4 posts views Thread by Rich Sienkiewicz | last post: by
2 posts views Thread by dw | last post: by
1 post views Thread by bearophileHUGS | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.