Synchronized Keywords in Variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank

    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.syn chronizedX(...) 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.syn chronizedList(n ew LinkedList());

    Thanks for any help,
    Frank
  • Nathan Zumwalt

    #2
    Re: Synchronized Keywords in Variables

    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

    trollybaz@hotma il.com (Frank) wrote in message news:<c45f3aca. 0310081050.4241 c33f@posting.go ogle.com>...[color=blue]
    > Hi,
    >
    > In the javadocs regarding many of the java.util classes, it states
    > that the classes are not synchronized, and suggest using the
    > Collections.syn chronizedX(...) 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.syn chronizedList(n ew LinkedList());
    >
    > Thanks for any help,
    > Frank[/color]

    Comment

    • Chris

      #3
      Re: Synchronized Keywords in Variables

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1

      Nathan Zumwalt wrote:
      [color=blue]
      > 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[/color]

      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/wxczzJRavJYRAkq mAJ4sW/83G265OeeGJ7Cpk RK3PDzSsgCg9lJI
      hepCkjbs3FKJ4/YSPCGn504=
      =dJhI
      -----END PGP SIGNATURE-----

      Comment

      Working...