Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c70e709cd | ||
|
|
bf54c701ab |
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="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>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://mirror-maven.runflare.com/maven2" />
|
||||
</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>
|
||||
+17
-20
@@ -33,32 +33,29 @@ public class Main {
|
||||
|
||||
List<String> data = FileManager.loadStudents("students.txt");
|
||||
|
||||
// TODO:
|
||||
int total = data.size();
|
||||
int partSize = total / 3;
|
||||
int rem = total % 3;
|
||||
|
||||
// Split data into 3 parts
|
||||
int end1 = partSize + (rem > 0 ? 1 : 0);
|
||||
int end2 = end1 + partSize + (rem > 1 ? 1 : 0);
|
||||
|
||||
List<String> part1 = null;
|
||||
List<String> p1 = data.subList(0, end1);
|
||||
List<String> p2 = data.subList(end1, end2);
|
||||
List<String> p3 = data.subList(end2, total);
|
||||
|
||||
List<String> part2 = null;
|
||||
StudentLoader t1 = new StudentLoader(p1);
|
||||
StudentLoader t2 = new StudentLoader(p2);
|
||||
StudentLoader t3 = new StudentLoader(p3);
|
||||
|
||||
List<String> part3 = null;
|
||||
t1.start(); t2.start(); t3.start();
|
||||
t1.join(); t2.join(); t3.join();
|
||||
|
||||
// TODO:
|
||||
|
||||
StudentLoader t1 = null;
|
||||
|
||||
StudentLoader t2 = null;
|
||||
|
||||
StudentLoader t3 = null;
|
||||
|
||||
// TODO:
|
||||
|
||||
// start()
|
||||
|
||||
// join()
|
||||
|
||||
// merge results
|
||||
manager.addStudents(t1.getStudents());
|
||||
manager.addStudents(t2.getStudents());
|
||||
manager.addStudents(t3.getStudents());
|
||||
|
||||
System.out.println("Students loaded.");
|
||||
}
|
||||
|
||||
case 2 -> {
|
||||
|
||||
@@ -40,31 +40,15 @@ public class Student {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
// TODO:
|
||||
// Return custom hash
|
||||
|
||||
// Example:
|
||||
// return id % 97;
|
||||
|
||||
return 0;
|
||||
return id % 97;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
// TODO:
|
||||
|
||||
// Step 1:
|
||||
// Check null
|
||||
|
||||
// Step 2:
|
||||
// Convert obj to Student
|
||||
|
||||
// Step 3:
|
||||
// Compare IDs
|
||||
|
||||
return false;
|
||||
if (this == obj) return true;
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
Student other = (Student) obj;
|
||||
return this.id == other.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,36 +10,18 @@ public class StudentManager {
|
||||
private final ArrayList<Student> students = new ArrayList<>();
|
||||
|
||||
public void addStudents(ArrayList<Student> newStudents) {
|
||||
|
||||
// TODO:
|
||||
|
||||
// Merge students
|
||||
|
||||
// Hint:
|
||||
// addAll()
|
||||
|
||||
students.addAll(newStudents);
|
||||
}
|
||||
|
||||
public Student searchStudent(int id) {
|
||||
|
||||
// TODO:
|
||||
|
||||
// Loop through students
|
||||
|
||||
// Compare IDs
|
||||
|
||||
for (Student s : students) {
|
||||
if (s.getId() == id) return s;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean removeStudent(int id) {
|
||||
|
||||
// TODO:
|
||||
|
||||
// Find student
|
||||
|
||||
// Remove student
|
||||
|
||||
return false;
|
||||
return students.removeIf(s -> s.getId() == id);
|
||||
}
|
||||
|
||||
public void printStudents() {
|
||||
|
||||
@@ -9,11 +9,11 @@ import java.util.List;
|
||||
|
||||
public class StudentLoader extends Thread {
|
||||
|
||||
// Assigned file lines
|
||||
|
||||
|
||||
private final List<String> lines;
|
||||
|
||||
// Local student storage
|
||||
|
||||
|
||||
private final ArrayList<Student> students = new ArrayList<>();
|
||||
|
||||
@@ -24,29 +24,13 @@ public class StudentLoader extends Thread {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
// TODO:
|
||||
|
||||
/*
|
||||
Example line:
|
||||
|
||||
1001,Aryan,Computer Science,3.7
|
||||
*/
|
||||
|
||||
for (String line : lines) {
|
||||
|
||||
// TODO:
|
||||
// split line
|
||||
|
||||
// TODO:
|
||||
// read values
|
||||
|
||||
// TODO:
|
||||
// create Student
|
||||
|
||||
// TODO:
|
||||
// add to students list
|
||||
|
||||
String[] parts = line.split(",");
|
||||
int id = Integer.parseInt(parts[0]);
|
||||
String name = parts[1];
|
||||
String major = parts[2];
|
||||
double gpa = Double.parseDouble(parts[3]);
|
||||
students.add(new Student(id, name, major, gpa));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,22 +11,14 @@ public class FileManager {
|
||||
public static List<String> loadStudents(String path) throws Exception {
|
||||
|
||||
List<String> lines = new ArrayList<>();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new FileReader(path));
|
||||
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
|
||||
// TODO:
|
||||
|
||||
// Add line to list
|
||||
|
||||
lines.add(line);
|
||||
}
|
||||
|
||||
reader.close();
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +1,10 @@
|
||||
package lecture.multithreading.timeout;
|
||||
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
System.out.println("you have 10 seconds: ");
|
||||
System.out.print("enter your name: ");
|
||||
|
||||
MyRunnable myRunnable = new MyRunnable();
|
||||
Thread thread = new Thread(myRunnable);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
|
||||
String name = scanner.nextLine();
|
||||
System.out.println("hello " + name);
|
||||
|
||||
scanner.close();
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package lecture.multithreading.timeout;
|
||||
|
||||
public class MyRunnable implements Runnable{
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
|
||||
for (int i = 1; i <= 10 ; i++){
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("thread was interrupted");
|
||||
}
|
||||
|
||||
if(i == 10){
|
||||
System.out.println("time is up");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user