Skip to content

Instantly share code, notes, and snippets.

@bharath2020
Created February 4, 2015 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bharath2020/9f156d8f6e6dfe719552 to your computer and use it in GitHub Desktop.
Save bharath2020/9f156d8f6e6dfe719552 to your computer and use it in GitHub Desktop.
Synchronized array in JAVA
import java.util.ArrayList;
/**
* Created by bbooshan on 2/3/15.
*/
public class FTSynchronizedArray<T> {
private ArrayList<T> itemStore;
public FTSynchronizedArray()
{
itemStore = new ArrayList<T>();
}
public synchronized void addItem(T item)
{
itemStore.add(item);
}
public synchronized void clearAll()
{
itemStore.clear();
}
public synchronized void swapItems(int index1, int index2)
{
int totalItems = itemStore.size();
if( index1 < totalItems && index2 < totalItems)
{
T temp = itemStore.get(index1);
itemStore.set(index1, itemStore.get(index2));
itemStore.set(index2, temp);
}
}
public synchronized T getItem(int index)
{
int totalItems = itemStore.size();
T item = null;
if( index < totalItems)
{
item = itemStore.get(index);
}
return item;
}
public synchronized int totalItems()
{
return itemStore.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment