473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 14529
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Harry J. Smith | last post by:
How do you tell if two variables past as arguments to a subroutine are actually the same variable? In some languages you can compare their addresses. Visual Basic doesn't have pointers so what is...
6
by: Hal Vaughan | last post by:
Being self taught, this is one thing I've always had trouble with -- I finally get it straight in one situation and I find I'm not sure about another. I have a class that keeps calling an...
4
by: Rich Sienkiewicz | last post by:
Some classes, like Queue and SortedList, have a Synchronized method which gives a thread safe wrapper object for these classes. But the lock() statement does the same thing. Is there any rules as to...
2
by: dw | last post by:
Hi, everyone. I'm having difficulty understanding the difference of the keywords "friend" and "protected" vs. some of the other ways you can declare variables in ASP.NET ("dim", "public", and...
15
by: Bit byte | last post by:
I am writing a small parser object. I need to store keywords etc in lsts. Because this data is to be shared by all instances of my parser class, I have declared the variable as class variables...
6
by: ronrsr | last post by:
but I keep getting syntax errors on this one - adict = {'zid': result, 'keywords': result{0], 'quotations':result,'citations':result{3]}; zhtml.print_update_form(adict); result = 22L
2
by: alnoir | last post by:
I've looked around online and have even had a friend help me, however, for some reason I can't compare two strings. I'm doing this at the end of the code (within the two foreach loops), above...
1
by: =?Utf-8?B?QXZp?= | last post by:
Hi I have an interesting issue, I hope someone can shead light on: I have a 2 server "web farm" with a web app that produces image files. To ensure synchronization of content on both machines,...
1
by: bearophileHUGS | last post by:
Once in a while I feel free to write about less defined things, saying mostly wrong things. This post is mostly chat, if you aren't interested please ignore it. Python is fit enough for newbie...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.