Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f09b91d8c2 |
@@ -37,19 +37,19 @@ public class Main {
|
||||
|
||||
// Split data into 3 parts
|
||||
|
||||
List<String> part1 = null;
|
||||
List<String> part1 = data.subList(0, data.size()*35/100);
|
||||
|
||||
List<String> part2 = null;
|
||||
List<String> part2 = data.subList(data.size()*35/100, data.size()*7/10);
|
||||
|
||||
List<String> part3 = null;
|
||||
List<String> part3 = data.subList(data.size()*7/10, data.size());
|
||||
|
||||
// TODO:
|
||||
|
||||
StudentLoader t1 = null;
|
||||
StudentLoader t1 = new StudentLoader(part1);
|
||||
|
||||
StudentLoader t2 = null;
|
||||
StudentLoader t2 = new StudentLoader(part2);
|
||||
|
||||
StudentLoader t3 = null;
|
||||
StudentLoader t3 = new StudentLoader(part3);
|
||||
|
||||
// TODO:
|
||||
|
||||
@@ -57,8 +57,20 @@ public class Main {
|
||||
|
||||
// join()
|
||||
|
||||
t1.start();
|
||||
t2.start();
|
||||
t3.start();
|
||||
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
|
||||
// merge results
|
||||
|
||||
manager.addStudents(t1.getStudents());
|
||||
manager.addStudents(t2.getStudents());
|
||||
manager.addStudents(t3.getStudents());
|
||||
|
||||
}
|
||||
|
||||
case 2 -> {
|
||||
|
||||
@@ -47,7 +47,7 @@ public class Student {
|
||||
// Example:
|
||||
// return id % 97;
|
||||
|
||||
return 0;
|
||||
return id % 97;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,7 +64,12 @@ public class Student {
|
||||
// Step 3:
|
||||
// Compare IDs
|
||||
|
||||
return false;
|
||||
if (obj == null)
|
||||
return false;
|
||||
|
||||
Student st = (Student) obj;
|
||||
|
||||
return this.getId() == st.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -18,6 +18,8 @@ public class StudentManager {
|
||||
// Hint:
|
||||
// addAll()
|
||||
|
||||
students.addAll(newStudents);
|
||||
|
||||
}
|
||||
|
||||
public Student searchStudent(int id) {
|
||||
@@ -28,6 +30,12 @@ public class StudentManager {
|
||||
|
||||
// Compare IDs
|
||||
|
||||
for (Student st : students) {
|
||||
|
||||
if (st.getId() == id)
|
||||
return st;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -39,6 +47,15 @@ public class StudentManager {
|
||||
|
||||
// Remove student
|
||||
|
||||
for (Student st : students) {
|
||||
|
||||
if (st.getId() == id) {
|
||||
students.remove(st);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,15 +38,25 @@ public class StudentLoader extends Thread {
|
||||
// TODO:
|
||||
// split line
|
||||
|
||||
List<String> parts = List.of(line.split(","));
|
||||
|
||||
// TODO:
|
||||
// read values
|
||||
|
||||
int id = Integer.parseInt(parts.get(0));
|
||||
String name = parts.get(1);
|
||||
String major = parts.get(2);
|
||||
double gpa = Double.parseDouble(parts.get(3));
|
||||
|
||||
// TODO:
|
||||
// create Student
|
||||
|
||||
Student currentStudent = new Student(id, name, major, gpa);
|
||||
|
||||
// TODO:
|
||||
// add to students list
|
||||
|
||||
students.add(currentStudent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,9 +18,7 @@ public class FileManager {
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
|
||||
// TODO:
|
||||
|
||||
// Add line to list
|
||||
lines.add(line);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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