Results 1 to 2 of 2
  1. #1
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Synchronized ArrayList

    Hi All,

    Can anyone kindly tell me how to create a synchronized arraylist? Below mentioned is a line of code that i bumped into some site while searching about how to synchronize an array list'

    List<String> syncList = Collections.synchronizedList(new ArrayList<String>());.

    But when i tried using that code i faced compile time error >>> incompatible types.

    I tried the below code on my own and kindly tell me will it work?

    import java.util.*;

    class AList

    {

    public static void main(String[] agrs)

    {

    ArrayList <String> al = new ArrayList<String>();


    al.add("john");

    al.add("max");



    for(String s : al)

    {

    Collections.synchronizedList(s);

    System.out.println(s);


    }


    }

    }

    Now is the above code valid

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,345
    Blog Entries
    7
    Rep Power
    20

    Default Re: Synchronized ArrayList

    The return value of the Collections.synchronizedList( ... ) method is a synchronized list, but you're not using it at all.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. synchronized(this){}
    By zserzs in forum Threads and Synchronization
    Replies: 6
    Last Post: 03-21-2012, 02:08 AM
  2. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  3. Replies: 1
    Last Post: 09-08-2010, 04:08 PM
  4. Synchronized(this)?
    By kiza in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-07-2009, 01:20 PM
  5. synchronized
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-28-2007, 10:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •