Author SHA1 Message Date
Aryan 31e359ff87 feat: Complete timeout example for threading 2026-05-24 18:29:12 +03:30
13 changed files with 99 additions and 139 deletions
-10
View File
@@ -1,10 +0,0 @@
# 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/
-13
View File
@@ -1,13 +0,0 @@
<?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>
-7
View File
@@ -1,7 +0,0 @@
<?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>
-20
View File
@@ -1,20 +0,0 @@
<?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>
-12
View File
@@ -1,12 +0,0 @@
<?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
View File
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
+13 -35
View File
@@ -11,14 +11,11 @@ public class Main {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
boolean once = true;
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
StudentManager manager = new StudentManager(); StudentManager manager = new StudentManager();
while (true) { while (true) {
System.out.println("==================================");
System.out.println(""" System.out.println("""
1.Load students 1.Load students
@@ -26,59 +23,41 @@ public class Main {
3.Remove student 3.Remove student
4.Print students 4.Print students
5.Exit 5.Exit
Chose:
"""); """);
int choice = scanner.nextInt(); int choice = scanner.nextInt();
switch (choice) { switch (choice) {
case 1 -> { case 1 -> {
if(once){
List<String> data = FileManager.loadStudents("students.txt"); List<String> data = FileManager.loadStudents("students.txt");
int n = data.size(); // TODO:
List<String> part1 = data.subList(0 , n/3 ); // Split data into 3 parts
List<String> part2 = data.subList(n/3 , (2*n)/3); List<String> part1 = null;
List<String> part3 = data.subList((2*n)/3 , n); List<String> part2 = null;
List<String> part3 = null;
// 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;
t1.start(); // TODO:
t2.start();
t3.start();
t1.join(); // start()
t2.join();
t3.join();
manager.addStudents(t1.getStudents()); // join()
manager.addStudents(t2.getStudents());
manager.addStudents(t3.getStudents());
System.out.println("Loading Done");
once = false;
}
else {
System.out.println("you have load students befor");
}
// merge results
} }
@@ -108,7 +87,6 @@ public class Main {
manager.printStudents(); manager.printStudents();
case 5 -> { case 5 -> {
System.out.println("Good by");
return; return;
} }
+17 -14
View File
@@ -41,29 +41,32 @@ public class Student {
@Override @Override
public int hashCode() { public int hashCode() {
return id % 97; // TODO:
// Return custom hash
// Example:
// return id % 97;
return 0;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(obj == null){ // TODO:
// Step 1:
// Check null
// Step 2:
// Convert obj to Student
// Step 3:
// Compare IDs
return false; return false;
} }
Student std = (Student) obj;
if(std.hashCode() == this.hashCode()){
return true;
}
else {
return false;
}
}
@Override @Override
public String toString() { public String toString() {
+14 -18
View File
@@ -1,7 +1,6 @@
package dev.service; package dev.service;
import dev.model.Student; import dev.model.Student;
import dev.utils.FileManager;
import java.util.ArrayList; import java.util.ArrayList;
@@ -12,38 +11,35 @@ public class StudentManager {
public void addStudents(ArrayList<Student> newStudents) { public void addStudents(ArrayList<Student> newStudents) {
this.students.addAll(newStudents); // TODO:
// Merge students
// Hint:
// addAll()
} }
public Student searchStudent(int id) { public Student searchStudent(int id) {
for(Student st : students){ // TODO:
if(st.getId() == id){
return st; // Loop through students
}
} // Compare IDs
return null; return null;
} }
public boolean removeStudent(int id) { public boolean removeStudent(int id) {
Student removeable = null; // TODO:
for(Student st: students){ // Find student
if(st.getId() == id){
removeable = st;
}
}
if(removeable!= null){ // Remove student
students.remove(removeable);
return true;
}
return false; return false;
} }
public void printStudents() { public void printStudents() {
+16 -4
View File
@@ -25,15 +25,27 @@ public class StudentLoader extends Thread {
@Override @Override
public void run() { public void run() {
// TODO:
/*
Example line:
1001,Aryan,Computer Science,3.7
*/
for (String line : lines) { for (String line : lines) {
String[] data = line.split(","); // TODO:
// split line
Student mystd = new Student(Integer.parseInt(data[0]) , data[1] , // TODO:
data[2] , Double.parseDouble(data[3])); // read values
students.add(mystd); // TODO:
// create Student
// TODO:
// add to students list
} }
+3 -1
View File
@@ -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);
}
}
}
}