Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0868ceb2e1 | ||
|
|
cd5836bd79 | ||
|
|
539315cd15 | ||
|
|
bf8e91e732 | ||
|
|
31e359ff87 |
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>
|
||||
+18
-10
@@ -36,29 +36,37 @@ public class Main {
|
||||
// TODO:
|
||||
|
||||
// Split data into 3 parts
|
||||
int size = data.size();
|
||||
int partSize = (int) Math.ceil(size / 3.0);
|
||||
|
||||
List<String> part1 = null;
|
||||
|
||||
List<String> part2 = null;
|
||||
|
||||
List<String> part3 = null;
|
||||
List<String> part1 = data.subList(0, Math.min(partSize, size));
|
||||
List<String> part2 = data.subList(Math.min(partSize, size), Math.min(partSize * 2, size));
|
||||
List<String> part3 = data.subList(Math.min(partSize * 2, size), size);
|
||||
|
||||
// TODO:
|
||||
|
||||
StudentLoader t1 = null;
|
||||
|
||||
StudentLoader t2 = null;
|
||||
|
||||
StudentLoader t3 = null;
|
||||
StudentLoader t1 = new StudentLoader(part1);
|
||||
StudentLoader t2 = new StudentLoader(part2);
|
||||
StudentLoader t3 = new StudentLoader(part3);
|
||||
|
||||
// TODO:
|
||||
|
||||
// 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("Students loaded successfully.");
|
||||
}
|
||||
|
||||
case 2 -> {
|
||||
|
||||
@@ -45,9 +45,7 @@ public class Student {
|
||||
// Return custom hash
|
||||
|
||||
// Example:
|
||||
// return id % 97;
|
||||
|
||||
return 0;
|
||||
return id % 97;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,14 +55,17 @@ public class Student {
|
||||
|
||||
// Step 1:
|
||||
// Check null
|
||||
if (obj == null || getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Step 2:
|
||||
// Convert obj to Student
|
||||
Student other = (Student) obj;
|
||||
|
||||
// Step 3:
|
||||
// Compare IDs
|
||||
|
||||
return false;
|
||||
return this.id == other.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,9 +14,9 @@ public class StudentManager {
|
||||
// TODO:
|
||||
|
||||
// Merge students
|
||||
|
||||
// Hint:
|
||||
// addAll()
|
||||
if (newStudents != null){
|
||||
this.students.addAll(newStudents);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,7 +27,11 @@ public class StudentManager {
|
||||
// Loop through students
|
||||
|
||||
// Compare IDs
|
||||
|
||||
for (Student student : students){
|
||||
if (student.getId() == id){
|
||||
return student;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -38,11 +42,19 @@ public class StudentManager {
|
||||
// Find student
|
||||
|
||||
// Remove student
|
||||
|
||||
for (int i = 0; i < students.size(); i++){
|
||||
if (students.get(i).getId() == id){
|
||||
students.remove(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void printStudents() {
|
||||
if (students.isEmpty()){
|
||||
System.out.println("No students found.");
|
||||
}
|
||||
|
||||
for (Student student : students) {
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ public class StudentLoader extends Thread {
|
||||
public void run() {
|
||||
|
||||
// TODO:
|
||||
|
||||
/*
|
||||
Example line:
|
||||
|
||||
@@ -37,17 +36,33 @@ public class StudentLoader extends Thread {
|
||||
|
||||
// TODO:
|
||||
// split line
|
||||
String[] parts = line.split(",");
|
||||
|
||||
// TODO:
|
||||
// read values
|
||||
if (parts.length == 4){
|
||||
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());
|
||||
|
||||
// TODO:
|
||||
// create Student
|
||||
// TODO:
|
||||
// create Student
|
||||
Student student = new Student(id, name, major, gpa);
|
||||
|
||||
// TODO:
|
||||
// add to students list
|
||||
// TODO:
|
||||
// add to students list
|
||||
students.add(student);
|
||||
}
|
||||
catch (NumberFormatException e){
|
||||
System.err.println("Skipping invalid line format: " + line);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// to check if threads are working
|
||||
// System.out.println(Thread.currentThread().getName() + " -> " + lines.size());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ public class FileManager {
|
||||
// TODO:
|
||||
|
||||
// Add line to list
|
||||
lines.add(line);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,25 @@
|
||||
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 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user