Multithreading Basics and Hashing
This commit is contained in:
Generated
+10
@@ -0,0 +1,10 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Ignored default folder with query files
|
||||||
|
/queries/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
Generated
+13
@@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<annotationProcessing>
|
||||||
|
<profile name="Maven default annotation processors profile" enabled="true">
|
||||||
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
|
<outputRelativeToContentRoot value="true" />
|
||||||
|
<module name="WS-07-Multithreading-Basics-and-Hashing" />
|
||||||
|
</profile>
|
||||||
|
</annotationProcessing>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+20
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="RemoteRepositoriesConfiguration">
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Central Repository" />
|
||||||
|
<option name="url" value="https://maven.devneeds.ir/" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Maven Central repository" />
|
||||||
|
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="jboss.community" />
|
||||||
|
<option name="name" value="JBoss Community repository" />
|
||||||
|
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||||
|
</remote-repository>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+12
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK" />
|
||||||
|
</project>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
+48
-48
@@ -4,19 +4,19 @@ import dev.service.StudentManager;
|
|||||||
import dev.thread.StudentLoader;
|
import dev.thread.StudentLoader;
|
||||||
import dev.utils.FileManager;
|
import dev.utils.FileManager;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception
|
||||||
|
{
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
StudentManager manager = new StudentManager();
|
StudentManager manager = new StudentManager();
|
||||||
|
|
||||||
while (true) {
|
while (true)
|
||||||
|
{
|
||||||
System.out.println("""
|
System.out.println("""
|
||||||
1.Load students
|
1.Load students
|
||||||
2.Search student
|
2.Search student
|
||||||
@@ -27,73 +27,73 @@ public class Main {
|
|||||||
|
|
||||||
int choice = scanner.nextInt();
|
int choice = scanner.nextInt();
|
||||||
|
|
||||||
switch (choice) {
|
switch (choice)
|
||||||
|
{
|
||||||
case 1 -> {
|
case 1 ->
|
||||||
|
{
|
||||||
List<String> data = FileManager.loadStudents("students.txt");
|
List<String> data = FileManager.loadStudents("students.txt");
|
||||||
|
|
||||||
// TODO:
|
if (data.isEmpty())
|
||||||
|
{
|
||||||
|
System.out.println("No data found in file.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Split data into 3 parts
|
// TODO: Split data into 3 parts
|
||||||
|
int totalSize = data.size();
|
||||||
|
int chunkSize = (int) Math.ceil(totalSize / 3.0);
|
||||||
|
|
||||||
List<String> part1 = null;
|
List<String> part1 = data.subList(0, Math.min(chunkSize, totalSize));
|
||||||
|
List<String> part2 = data.subList(Math.min(chunkSize, totalSize), Math.min(2 * chunkSize, totalSize));
|
||||||
|
List<String> part3 = data.subList(Math.min(2 * chunkSize, totalSize), totalSize);
|
||||||
|
|
||||||
List<String> part2 = null;
|
StudentLoader t1 = new StudentLoader(part1);
|
||||||
|
StudentLoader t2 = new StudentLoader(part2);
|
||||||
|
StudentLoader t3 = new StudentLoader(part3);
|
||||||
|
|
||||||
List<String> part3 = null;
|
t1.start();
|
||||||
|
t2.start();
|
||||||
|
t3.start();
|
||||||
|
|
||||||
// TODO:
|
t1.join();
|
||||||
|
t2.join();
|
||||||
|
t3.join();
|
||||||
|
|
||||||
StudentLoader t1 = null;
|
manager.addStudents(t1.getStudents());
|
||||||
|
manager.addStudents(t2.getStudents());
|
||||||
StudentLoader t2 = null;
|
manager.addStudents(t3.getStudents());
|
||||||
|
|
||||||
StudentLoader t3 = null;
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
|
|
||||||
// start()
|
|
||||||
|
|
||||||
// join()
|
|
||||||
|
|
||||||
// merge results
|
|
||||||
|
|
||||||
|
System.out.println("Students loaded successfully using 3 threads.");
|
||||||
}
|
}
|
||||||
|
|
||||||
case 2 -> {
|
case 2 ->
|
||||||
|
{
|
||||||
System.out.print("ID: ");
|
System.out.print("ID: ");
|
||||||
|
|
||||||
int id = scanner.nextInt();
|
int id = scanner.nextInt();
|
||||||
|
var student = manager.searchStudent(id);
|
||||||
System.out.println(manager.searchStudent(id));
|
if (student != null)
|
||||||
|
{
|
||||||
|
System.out.println("Found: " + student);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.out.println("Student not found.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case 3 -> {
|
case 3 ->
|
||||||
|
{
|
||||||
System.out.print("ID: ");
|
System.out.print("ID: ");
|
||||||
|
|
||||||
int id = scanner.nextInt();
|
int id = scanner.nextInt();
|
||||||
|
|
||||||
boolean removed = manager.removeStudent(id);
|
boolean removed = manager.removeStudent(id);
|
||||||
|
System.out.println("Removed: " + removed);
|
||||||
System.out.println(removed);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 4 ->
|
case 4 ->
|
||||||
manager.printStudents();
|
manager.printStudents();
|
||||||
|
|
||||||
case 5 -> {
|
case 5 -> {return;}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,10 @@
|
|||||||
package dev.model;
|
package dev.model;
|
||||||
|
|
||||||
public class Student {
|
public class Student
|
||||||
|
{
|
||||||
private final int id;
|
private final int id;
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
private final String major;
|
private final String major;
|
||||||
|
|
||||||
private final double gpa;
|
private final double gpa;
|
||||||
|
|
||||||
public Student(
|
public Student(
|
||||||
@@ -39,37 +36,22 @@ public class Student {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {return id % 97;}
|
||||||
|
|
||||||
// TODO:
|
@Override
|
||||||
// Return custom hash
|
public boolean equals(Object obj)
|
||||||
|
{
|
||||||
|
if (this == obj) return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass()) return false;
|
||||||
|
|
||||||
// Example:
|
Student other = (Student) obj;
|
||||||
// return id % 97;
|
|
||||||
|
|
||||||
return 0;
|
return this.id == other.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public String toString()
|
||||||
|
{
|
||||||
// TODO:
|
|
||||||
|
|
||||||
// Step 1:
|
|
||||||
// Check null
|
|
||||||
|
|
||||||
// Step 2:
|
|
||||||
// Convert obj to Student
|
|
||||||
|
|
||||||
// Step 3:
|
|
||||||
// Compare IDs
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
|
|
||||||
return "Student{" +
|
return "Student{" +
|
||||||
"id=" + id +
|
"id=" + id +
|
||||||
", name='" + name + '\'' +
|
", name='" + name + '\'' +
|
||||||
@@ -77,5 +59,4 @@ public class Student {
|
|||||||
", gpa=" + gpa +
|
", gpa=" + gpa +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,55 +1,53 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
import dev.model.Student;
|
import dev.model.Student;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class StudentManager
|
||||||
public class StudentManager {
|
{
|
||||||
|
|
||||||
private final ArrayList<Student> students = new ArrayList<>();
|
private final ArrayList<Student> students = new ArrayList<>();
|
||||||
|
|
||||||
public void addStudents(ArrayList<Student> newStudents) {
|
public void addStudents(ArrayList<Student> newStudents)
|
||||||
|
{
|
||||||
// TODO:
|
students.addAll(newStudents);
|
||||||
|
|
||||||
// Merge students
|
|
||||||
|
|
||||||
// Hint:
|
|
||||||
// addAll()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Student searchStudent(int id) {
|
public Student searchStudent(int id)
|
||||||
|
{
|
||||||
// TODO:
|
for (Student student : students)
|
||||||
|
{
|
||||||
// Loop through students
|
if (student.getId() == id) {return student;}
|
||||||
|
}
|
||||||
// Compare IDs
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeStudent(int id) {
|
public boolean removeStudent(int id)
|
||||||
|
{
|
||||||
|
Student foundStudent = null;
|
||||||
|
|
||||||
// TODO:
|
for (Student student : students)
|
||||||
|
{
|
||||||
|
if (student.getId() == id)
|
||||||
|
{
|
||||||
|
foundStudent = student;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Find student
|
if (foundStudent != null)
|
||||||
|
{
|
||||||
// Remove student
|
students.remove(foundStudent);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printStudents() {
|
public void printStudents()
|
||||||
|
{
|
||||||
for (Student student : students) {
|
for (Student student : students)
|
||||||
|
{
|
||||||
System.out.println(student);
|
System.out.println(student);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,59 +1,51 @@
|
|||||||
package dev.thread;
|
package dev.thread;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import dev.model.Student;
|
import dev.model.Student;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class StudentLoader extends Thread {
|
public class StudentLoader extends Thread
|
||||||
|
{
|
||||||
// Assigned file lines
|
// Assigned file lines
|
||||||
|
|
||||||
private final List<String> lines;
|
private final List<String> lines;
|
||||||
|
|
||||||
// Local student storage
|
// Local student storage
|
||||||
|
|
||||||
private final ArrayList<Student> students = new ArrayList<>();
|
private final ArrayList<Student> students = new ArrayList<>();
|
||||||
|
|
||||||
public StudentLoader(List<String> lines) {
|
public StudentLoader(List<String> lines) {this.lines = lines;}
|
||||||
|
|
||||||
this.lines = lines;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run()
|
||||||
|
{
|
||||||
|
for (String line : lines)
|
||||||
|
{
|
||||||
|
if (line == null || line.trim().isEmpty()) {continue;}
|
||||||
|
|
||||||
// TODO:
|
String[] parts = line.split(",");
|
||||||
|
|
||||||
/*
|
if (parts.length == 4)
|
||||||
Example line:
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int id = Integer.parseInt(parts[0].trim());
|
||||||
|
String name = parts[1].trim();
|
||||||
|
String major = parts[2].trim();
|
||||||
|
double gpa = Double.parseDouble(parts[3].trim());
|
||||||
|
|
||||||
1001,Aryan,Computer Science,3.7
|
Student student = new Student(id, name, major, gpa);
|
||||||
*/
|
|
||||||
|
|
||||||
for (String line : lines) {
|
students.add(student);
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// split line
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// read values
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// create Student
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// add to students list
|
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (NumberFormatException e)
|
||||||
|
{
|
||||||
|
System.err.println("Skipping invalid line (format error): " + line);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Student> getStudents() {
|
public ArrayList<Student> getStudents() {
|
||||||
|
|
||||||
return students;
|
return students;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,31 +2,26 @@ package dev.utils;
|
|||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class FileManager {
|
public class FileManager {
|
||||||
|
|
||||||
public static List<String> loadStudents(String path) throws Exception {
|
public static List<String> loadStudents(String path) throws Exception
|
||||||
|
{
|
||||||
List<String> lines = new ArrayList<>();
|
List<String> lines = new ArrayList<>();
|
||||||
|
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(path));
|
BufferedReader reader = new BufferedReader(new FileReader(path));
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
|
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null)
|
||||||
|
{
|
||||||
// TODO:
|
if (!line.trim().isEmpty()) {lines.add(line);}
|
||||||
|
|
||||||
// Add line to list
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user