complete code
This commit is contained in:
+19
-13
@@ -33,31 +33,37 @@ public class Main {
|
||||
|
||||
List<String> data = FileManager.loadStudents("students.txt");
|
||||
|
||||
// TODO:
|
||||
int size = data.size();
|
||||
|
||||
// Split data into 3 parts
|
||||
int partSize = size / 3;
|
||||
|
||||
List<String> part1 = null;
|
||||
List<String> part1 = data.subList(0, partSize);
|
||||
|
||||
List<String> part2 = null;
|
||||
List<String> part2 = data.subList(partSize, 2 * partSize);
|
||||
|
||||
List<String> part3 = null;
|
||||
List<String> part3 = data.subList(2 * partSize, size);
|
||||
|
||||
// TODO:
|
||||
|
||||
StudentLoader t1 = null;
|
||||
|
||||
StudentLoader t2 = null;
|
||||
StudentLoader t1 = new StudentLoader(part1);
|
||||
|
||||
StudentLoader t3 = null;
|
||||
StudentLoader t2 = new StudentLoader(part2);
|
||||
|
||||
// TODO:
|
||||
StudentLoader t3 = new StudentLoader(part3);
|
||||
|
||||
// start()
|
||||
t1.start();
|
||||
t2.start();
|
||||
t3.start();
|
||||
|
||||
// join()
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
|
||||
// merge results
|
||||
manager.addStudents(t1.getStudents());
|
||||
manager.addStudents(t2.getStudents());
|
||||
manager.addStudents(t3.getStudents());
|
||||
|
||||
System.out.println("Data loaded successfully.");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,24 @@
|
||||
package lecture.multithreading.timeout;
|
||||
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// TODO
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("You have 10 second: ");
|
||||
System.out.println("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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,19 @@
|
||||
package lecture.multithreading.timeout;
|
||||
|
||||
public class MyRunnable {
|
||||
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