Commit 9f6b67c2 authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

03. Save Record using Runnable

parent 4f91f30f
import Resource.Student;
import Service.StudentService;
public class StudentRunnable implements Runnable {
private Student student;
private StudentService studentService;
public StudentRunnable(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);
}
}
...@@ -24,7 +24,7 @@ public class ThreadTest extends TestCase { ...@@ -24,7 +24,7 @@ public class ThreadTest extends TestCase {
public void testStudentThread() { public void testStudentThread() {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
System.out.println("Name of current Thread: "+Thread.currentThread().getName()); System.out.println("Name of current Thread: " + Thread.currentThread().getName());
StudentService studentService = new StudentService(); StudentService studentService = new StudentService();
Student std = new Student("student"); Student std = new Student("student");
...@@ -42,16 +42,16 @@ public class ThreadTest extends TestCase { ...@@ -42,16 +42,16 @@ public class ThreadTest extends TestCase {
System.out.println("Execution Time: " + execTime + " ms"); System.out.println("Execution Time: " + execTime + " ms");
} }
public void testStudentsThreads(){ public void testStudentsThreads() {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
StudentService studentService = new StudentService(); StudentService studentService = new StudentService();
for (int i=1;i<=10;++i){ for (int i = 1; i <= 10; ++i) {
Student std = new Student("student "+i); Student std = new Student("student " + i);
StudentThread studentThread = new StudentThread(std, studentService); StudentThread studentThread = new StudentThread(std, studentService);
studentThread.setName("myThread-"+i); studentThread.setName("myThread-" + i);
studentThread.start(); // start thread studentThread.start(); // start thread
// studentThread.run(); // treat as a simple object not as a thread // studentThread.run(); // treat as a simple object not as a thread
try { try {
...@@ -64,16 +64,16 @@ public class ThreadTest extends TestCase { ...@@ -64,16 +64,16 @@ public class ThreadTest extends TestCase {
System.out.println("Execution Time: " + execTime + " ms"); System.out.println("Execution Time: " + execTime + " ms");
} }
public void testStudentThreadsPP(){ public void testStudentThreadsPP() {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
StudentService studentService = new StudentService(); StudentService studentService = new StudentService();
List<StudentThread> studentThreads = new ArrayList<>(); List<StudentThread> studentThreads = new ArrayList<>();
for (int i=1;i<=10;++i){ for (int i = 1; i <= 10; ++i) {
Student std = new Student("student "+i); Student std = new Student("student " + i);
StudentThread studentThread = new StudentThread(std, studentService); StudentThread studentThread = new StudentThread(std, studentService);
studentThread.setName("myThread-"+i); studentThread.setName("myThread-" + i);
studentThread.start(); // start thread studentThread.start(); // start thread
studentThreads.add(studentThread); studentThreads.add(studentThread);
// studentThread.run(); // treat as a simple object not as a thread // studentThread.run(); // treat as a simple object not as a thread
...@@ -89,4 +89,57 @@ public class ThreadTest extends TestCase { ...@@ -89,4 +89,57 @@ public class ThreadTest extends TestCase {
long execTime = System.currentTimeMillis() - start; long execTime = System.currentTimeMillis() - start;
System.out.println("Execution Time: " + execTime + " ms"); System.out.println("Execution Time: " + execTime + " ms");
} }
public void testStudentRunnable() {
StudentService studentService = new StudentService();
Student std = new Student("student");
StudentRunnable studentRunnable = new StudentRunnable(std, studentService);
Thread thread = new Thread(studentRunnable);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Completed Runnable");
}
public void testStudentRunnableAnonymousClass() {
StudentService studentService = new StudentService();
Student std = new Student("student");
// StudentRunnable studentRunnable = new StudentRunnable(std, studentService);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
studentService.saveStudent(std);
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Completed Runnable");
}
public void testStudentRunnableLambda() {
StudentService studentService = new StudentService();
Student std = new Student("student");
Thread thread = new Thread(() -> {
studentService.saveStudent(std);
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Completed Runnable");
}
} }
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