I was trying to add 10 Million records in an array list using Thread pool of size 4(On octa core processor). But it is taking double the time compared to single threaded code.
Below are the code snippets. I might be doing something wrong. Can anyone explain what is the issue in code?
package com.shree.test;
public class Student {
private int id;
private String name;
private int age;
private int std;
public Student(int id, String name, int age, int std) {
super();
this.id = id;
this.name = name;
this.age = age;
this.std = std;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getStd() {
return std;
}
public void setStd(int std) {
this.std = std;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", std=" + std + "]";
}
}
Multi Threaded code(with Threadpool):
package com.shree.test;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
class Task implements Callable<Student>{
private static final String CHAR_SET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private int id;
private List<Student> studentList;
public Task(int id,List<Student> studentList) {
this.id = id;
this.studentList = studentList;
}
@Override
public Student call() throws Exception {
Student student = new Student(id, RandomStringUtils.random(RandomUtils.nextInt(5, 10), CHAR_SET), RandomUtils.nextInt(10, 15), RandomUtils.nextInt(4, 9));
studentList.add(student);
return student;
}
}
public class MultiThreadStudentListGenerator {
private List<Student> students = Collections.synchronizedList(new ArrayList<>());
private ExecutorService threadPool = Executors.newFixedThreadPool(4);
public void generateStudentList() {
for(int i=0;i<10000000;i++) {
threadPool.submit(new Task(i, students));
}
threadPool.shutdown();
}
public void process() {
generateStudentList();
}
public int getSize() {
return students.size();
}
public void addShutDownhook(LocalDateTime dateTime1 ) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
LocalDateTime dateTime2 = LocalDateTime.now();
long diffInMilli = java.time.Duration.between(dateTime1, dateTime2)
.toMillis();
System.out.println("Time taken in Miliseconds: " + diffInMilli);
System.out.println("List Size: " + getSize());
}
});
}
public static void main(String[] args) {
MultiThreadStudentListGenerator multiThreadStudentListGenerator = new MultiThreadStudentListGenerator();
LocalDateTime dateTime1 = LocalDateTime.now();
multiThreadStudentListGenerator.addShutDownhook(dateTime1);
multiThreadStudentListGenerator.process();
}
}
Single Threaded code:
package com.shree.test;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
public class SingleThreadStudentListGenerator {
private static final String CHAR_SET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private List<Student> students = new ArrayList<>();
public void generateStudentList() {
for (int i = 0; i < 10000000; i++) {
Student student = new Student(i, RandomStringUtils.random(RandomUtils.nextInt(5, 10), CHAR_SET),
RandomUtils.nextInt(10, 15), RandomUtils.nextInt(4, 9));
students.add(student);
}
}
public void process() {
generateStudentList();
}
public int getSize() {
return students.size();
}
public void addShutDownhook(LocalDateTime dateTime1) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
LocalDateTime dateTime2 = LocalDateTime.now();
long diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();
System.out.println("Time taken in Miliseconds: " + diffInMilli);
System.out.println("Size: " + getSize());
}
});
}
public static void main(String[] args) {
SingleThreadStudentListGenerator mainClass = new SingleThreadStudentListGenerator();
LocalDateTime dateTime1 = LocalDateTime.now();
mainClass.addShutDownhook(dateTime1);
mainClass.process();
}
}