Commit 0995dcb6 authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

01. Save Records Sequentially

parent 8c591987
# ForkJoin Framework # ForkJoin Framework
## Review Threads in Java
### Why we need threads?
Save 10 Records sequentially as Example.
package Resource;
public class Student {
private String name;
public String getName() {
return name;
}
public Student(String name) {
this.name = name;
}
}
package Service;
import Resource.Student;
import java.util.UUID;
public class StudentService {
public String saveStudent(Student std){
System.out.println("Saving Student: " + std.getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return UUID.randomUUID().toString();
}
}
public class Threads {
}
import Resource.Student;
import Service.StudentService;
import junit.framework.TestCase;
public class ThreadTest extends TestCase {
public void testStudent() {
long start = System.currentTimeMillis();
StudentService studentService = new StudentService();
for (int i = 1; i <= 10; ++i) {
Student std = new Student("student " + i);
studentService.saveStudent(std);
}
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