java.util.Collections.synchronizedList() Method


Advertisements


Description

The synchronizedList(List<T>) method is used to return a synchronized (thread-safe) list backed by the specified list.

Declaration

Following is the declaration for java.util.Collections.synchronizedList() method.

public static <T> List<T> synchronizedList(List<T> list)

Parameters

  • list -- The list to be "wrapped" in a synchronized list.

Return Value

  • The method call returns a synchronized view of the specified list.

Exception

  • NA

Example

The following example shows the usage of java.util.Collections.synchronizedList()

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String[] args) {
   // create vector object 
   List<String> list = new ArrayList<String>();
      
   // populate the list
   list.add("1");
   list.add("2");
   list.add("3");
   list.add("4");
   list.add("5");
      
   // create a synchronized list
   List<String> synlist = Collections.synchronizedList(list);
     
   System.out.println("Sunchronized list is :"+synlist);
   }
}

Let us compile and run the above program, this will produce the following result.

Sunchronized list is :[1, 2, 3, 4, 5]

java_util_collections.htm

Advertisements