Commit 4f91f30f authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

02. Save Records in Parallel

parent 0995dcb6
......@@ -12,6 +12,7 @@ public class StudentService {
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Student "+ std.getName()+ " is saved.");
return UUID.randomUUID().toString();
}
}
import Resource.Student;
import Service.StudentService;
public class StudentThread extends Thread {
private Student student;
private StudentService studentService;
public StudentThread(Student student, StudentService studentService) {
this.student = student;
this.studentService = studentService;
}
@Override
public void run() {
System.out.println("Name of current Thread: "+Thread.currentThread().getName());
studentService.saveStudent(student);
}
}
public class Threads {
}
......@@ -2,6 +2,9 @@ import Resource.Student;
import Service.StudentService;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
public class ThreadTest extends TestCase {
public void testStudent() {
......@@ -17,4 +20,73 @@ public class ThreadTest extends TestCase {
System.out.println("Execution Time: " + execTime + " ms");
}
public void testStudentThread() {
long start = System.currentTimeMillis();
System.out.println("Name of current Thread: "+Thread.currentThread().getName());
StudentService studentService = new StudentService();
Student std = new Student("student");
StudentThread studentThread = new StudentThread(std, studentService);
studentThread.setName("myThread");
studentThread.start(); // start thread
// studentThread.run(); // treat as a simple object not as a thread
try {
studentThread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
long execTime = System.currentTimeMillis() - start;
System.out.println("Execution Time: " + execTime + " ms");
}
public void testStudentsThreads(){
long start = System.currentTimeMillis();
StudentService studentService = new StudentService();
for (int i=1;i<=10;++i){
Student std = new Student("student "+i);
StudentThread studentThread = new StudentThread(std, studentService);
studentThread.setName("myThread-"+i);
studentThread.start(); // start thread
// studentThread.run(); // treat as a simple object not as a thread
try {
studentThread.join(); //wait thread to complete
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
long execTime = System.currentTimeMillis() - start;
System.out.println("Execution Time: " + execTime + " ms");
}
public void testStudentThreadsPP(){
long start = System.currentTimeMillis();
StudentService studentService = new StudentService();
List<StudentThread> studentThreads = new ArrayList<>();
for (int i=1;i<=10;++i){
Student std = new Student("student "+i);
StudentThread studentThread = new StudentThread(std, studentService);
studentThread.setName("myThread-"+i);
studentThread.start(); // start thread
studentThreads.add(studentThread);
// studentThread.run(); // treat as a simple object not as a thread
}
studentThreads.forEach(e -> {
try {
e.join(); //wait thread to complete
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
});
long execTime = System.currentTimeMillis() - start;
System.out.println("Execution Time: " + execTime + " ms");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment