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