I am using the following code fragment to process the java collection concurrently. Basically i am using the TaskExecutors to process the collection in multiple threads that check for the duplicate transaction in the collection based on the transaction id. There is no relationship between the transactions except the duplicate check.
I want to know the following code has any concurrent issue?
public class Txn {
private long id;
private String status;
@Override
public boolean equals(Object obj) {
return this.getId() == ((Txn) obj).getId();
}
}
public class Main {
public static void main(String[] args) throws Exception {
List<Txn> list = new ArrayList<Txn>();
List<Txn> acceptedList = new ArrayList<Txn>();
List<Txn> rejectedList = new ArrayList<Txn>();
for (long i = 0; i < 10000l; i++) {
Txn txn = new Txn();
txn.setId(i % 1000);
list.add(txn);
}
final ConcurrentHashMap<Long, Integer> map = new ConcurrentHashMap<>();
ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
for (int i = 0; i < list.size(); i++) {
final Txn txn = list.get(i);
Callable<Void> callable = new Callable<Void>() {
@Override
public Void call() throws Exception {
if (map.putIfAbsent(txn.getId(), 1) != null) {
txn.setStatus("duplicate");
}
return null;
}
};
executorService.submit(callable);
}
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
for (Txn txn : list) {
if (txn.getStatus() != null && txn.getStatus().equalsIgnoreCase("duplicate")) {
rejectedList.add(txn);
} else {
acceptedList.add(txn);
}
}
Set<Txn> set = new HashSet<>(acceptedList);
if (set.size() != acceptedList.size()) {
throw new Exception("11111111");
}
System.out.println(acceptedList.size());
System.out.println(rejectedList.size());
}
}
Appreciate your comments. Thanks