multihreading
This commit is contained in:
+141
-20
@@ -1,22 +1,34 @@
|
||||
package dev;
|
||||
|
||||
|
||||
import dev.model.Student;
|
||||
import dev.service.StudentManager;
|
||||
import dev.thread.StudentLoader;
|
||||
import dev.utils.FileManager;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
StudentManager manager = new StudentManager();
|
||||
|
||||
StudentManager manager =
|
||||
new StudentManager();
|
||||
|
||||
|
||||
|
||||
while (true) {
|
||||
|
||||
|
||||
System.out.println("""
|
||||
1.Load students
|
||||
2.Search student
|
||||
@@ -25,71 +37,180 @@ public class Main {
|
||||
5.Exit
|
||||
""");
|
||||
|
||||
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
|
||||
|
||||
switch (choice) {
|
||||
|
||||
|
||||
|
||||
case 1 -> {
|
||||
|
||||
List<String> data = FileManager.loadStudents("students.txt");
|
||||
|
||||
// TODO:
|
||||
List<String> data =
|
||||
FileManager.loadStudents("students.txt");
|
||||
|
||||
// Split data into 3 parts
|
||||
|
||||
List<String> part1 = null;
|
||||
|
||||
List<String> part2 = null;
|
||||
int size = data.size();
|
||||
|
||||
List<String> part3 = null;
|
||||
|
||||
// TODO:
|
||||
int part = (int)Math.ceil(size / 3.0);
|
||||
|
||||
StudentLoader t1 = null;
|
||||
|
||||
StudentLoader t2 = null;
|
||||
|
||||
StudentLoader t3 = null;
|
||||
List<String> part1 =
|
||||
data.subList(
|
||||
0,
|
||||
Math.min(part,size)
|
||||
);
|
||||
|
||||
// TODO:
|
||||
|
||||
// start()
|
||||
List<String> part2 =
|
||||
data.subList(
|
||||
Math.min(part,size),
|
||||
Math.min(part*2,size)
|
||||
);
|
||||
|
||||
// join()
|
||||
|
||||
// merge results
|
||||
List<String> part3 =
|
||||
data.subList(
|
||||
Math.min(part*2,size),
|
||||
size
|
||||
);
|
||||
|
||||
|
||||
|
||||
StudentLoader t1 =
|
||||
new StudentLoader(part1);
|
||||
|
||||
|
||||
StudentLoader t2 =
|
||||
new StudentLoader(part2);
|
||||
|
||||
|
||||
StudentLoader t3 =
|
||||
new StudentLoader(part3);
|
||||
|
||||
|
||||
|
||||
t1.start();
|
||||
|
||||
t2.start();
|
||||
|
||||
t3.start();
|
||||
|
||||
|
||||
|
||||
t1.join();
|
||||
|
||||
t2.join();
|
||||
|
||||
t3.join();
|
||||
|
||||
|
||||
|
||||
|
||||
ArrayList<Student> finalList =
|
||||
new ArrayList<>();
|
||||
|
||||
|
||||
finalList.addAll(t1.getStudents());
|
||||
|
||||
finalList.addAll(t2.getStudents());
|
||||
|
||||
finalList.addAll(t3.getStudents());
|
||||
|
||||
|
||||
|
||||
manager.addStudents(finalList);
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"Students loaded: "
|
||||
+ finalList.size()
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
case 2 -> {
|
||||
|
||||
|
||||
System.out.print("ID: ");
|
||||
|
||||
|
||||
int id = scanner.nextInt();
|
||||
|
||||
System.out.println(manager.searchStudent(id));
|
||||
|
||||
|
||||
Student student =
|
||||
manager.searchStudent(id);
|
||||
|
||||
|
||||
|
||||
if(student != null){
|
||||
|
||||
System.out.println("Found:");
|
||||
|
||||
System.out.println(student);
|
||||
|
||||
}
|
||||
|
||||
else{
|
||||
|
||||
System.out.println("Student not found.");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
case 3 -> {
|
||||
|
||||
|
||||
System.out.print("ID: ");
|
||||
|
||||
|
||||
int id = scanner.nextInt();
|
||||
|
||||
boolean removed = manager.removeStudent(id);
|
||||
|
||||
System.out.println(removed);
|
||||
System.out.println(
|
||||
manager.removeStudent(id)
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
case 4 ->
|
||||
manager.printStudents();
|
||||
|
||||
|
||||
case 4 -> {
|
||||
|
||||
|
||||
manager.printStudents();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
case 5 -> {
|
||||
|
||||
|
||||
return;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user