Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31e359ff87 |
Generated
-10
@@ -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/
|
|
||||||
Generated
-13
@@ -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>
|
|
||||||
Generated
-7
@@ -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>
|
|
||||||
Generated
-20
@@ -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="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>
|
|
||||||
<remote-repository>
|
|
||||||
<option name="id" value="central" />
|
|
||||||
<option name="name" value="Central Repository" />
|
|
||||||
<option name="url" value="https://maven.aliyun.com/repository/public" />
|
|
||||||
</remote-repository>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
Generated
-12
@@ -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="homebrew-26" project-jdk-type="JavaSDK" />
|
|
||||||
</project>
|
|
||||||
Generated
-7
@@ -1,7 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="" vcs="Git" />
|
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
+34
-31
@@ -12,7 +12,7 @@ public class Main {
|
|||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
// ساخت شیء مدیریت دانشجویان
|
|
||||||
StudentManager manager = new StudentManager();
|
StudentManager manager = new StudentManager();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -30,67 +30,70 @@ public class Main {
|
|||||||
switch (choice) {
|
switch (choice) {
|
||||||
|
|
||||||
case 1 -> {
|
case 1 -> {
|
||||||
// برای جلوگیری از تکرار دادهها، مدیر قبلی را با یک ابجکت جدید جایگزین میکنیم
|
|
||||||
manager = new StudentManager();
|
|
||||||
|
|
||||||
// خواندن خطوط فایل
|
|
||||||
List<String> data = FileManager.loadStudents("students.txt");
|
List<String> data = FileManager.loadStudents("students.txt");
|
||||||
|
|
||||||
// تقسیم خطوط به ۳ بخش مساوی
|
// TODO:
|
||||||
int totalLines = data.size();
|
|
||||||
int chunkSize = totalLines / 3;
|
|
||||||
|
|
||||||
List<String> part1 = data.subList(0, chunkSize);
|
// Split data into 3 parts
|
||||||
List<String> part2 = data.subList(chunkSize, chunkSize * 2);
|
|
||||||
List<String> part3 = data.subList(chunkSize * 2, totalLines);
|
|
||||||
|
|
||||||
// ساختن ۳ ترد مجزا
|
List<String> part1 = null;
|
||||||
StudentLoader t1 = new StudentLoader(part1);
|
|
||||||
StudentLoader t2 = new StudentLoader(part2);
|
|
||||||
StudentLoader t3 = new StudentLoader(part3);
|
|
||||||
|
|
||||||
// شروع کار همزمان تردها
|
List<String> part2 = null;
|
||||||
t1.start();
|
|
||||||
t2.start();
|
|
||||||
t3.start();
|
|
||||||
|
|
||||||
// انتظار برای پایان کار تردها
|
List<String> part3 = null;
|
||||||
t1.join();
|
|
||||||
t2.join();
|
|
||||||
t3.join();
|
|
||||||
|
|
||||||
// ادغام نتایج در لیست اصلی
|
// TODO:
|
||||||
manager.addStudents(t1.getStudents());
|
|
||||||
manager.addStudents(t2.getStudents());
|
StudentLoader t1 = null;
|
||||||
manager.addStudents(t3.getStudents());
|
|
||||||
|
StudentLoader t2 = null;
|
||||||
|
|
||||||
|
StudentLoader t3 = null;
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
|
||||||
|
// start()
|
||||||
|
|
||||||
|
// join()
|
||||||
|
|
||||||
|
// merge results
|
||||||
|
|
||||||
System.out.println("Students loaded successfully.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 2 -> {
|
case 2 -> {
|
||||||
|
|
||||||
System.out.print("ID: ");
|
System.out.print("ID: ");
|
||||||
|
|
||||||
int id = scanner.nextInt();
|
int id = scanner.nextInt();
|
||||||
// جستجوی دانشجو
|
|
||||||
System.out.println(manager.searchStudent(id));
|
System.out.println(manager.searchStudent(id));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 3 -> {
|
case 3 -> {
|
||||||
|
|
||||||
System.out.print("ID: ");
|
System.out.print("ID: ");
|
||||||
|
|
||||||
int id = scanner.nextInt();
|
int id = scanner.nextInt();
|
||||||
// حذف دانشجو و چاپ نتیجه
|
|
||||||
boolean removed = manager.removeStudent(id);
|
boolean removed = manager.removeStudent(id);
|
||||||
|
|
||||||
System.out.println(removed);
|
System.out.println(removed);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 4 ->
|
case 4 ->
|
||||||
// چاپ همه دانشجویان
|
|
||||||
manager.printStudents();
|
manager.printStudents();
|
||||||
|
|
||||||
case 5 -> {
|
case 5 -> {
|
||||||
// خروج از برنامه
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -41,22 +41,30 @@ public class Student {
|
|||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
|
|
||||||
// بازگرداندن هش سفارشی طبق فرمول خواسته شده
|
// TODO:
|
||||||
return this.id % 97;
|
// Return custom hash
|
||||||
|
|
||||||
|
// Example:
|
||||||
|
// return id % 97;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
|
|
||||||
// بررسی یکی بودن رفرنس یا نال بودن ابجکت ورودی
|
// TODO:
|
||||||
if (this == obj) return true;
|
|
||||||
if (obj == null || getClass() != obj.getClass()) return false;
|
|
||||||
|
|
||||||
// تبدیل ابجکت ورودی به کلاس Student
|
// Step 1:
|
||||||
Student student = (Student) obj;
|
// Check null
|
||||||
|
|
||||||
// مقایسه IDها برای تعیین برابری
|
// Step 2:
|
||||||
return this.id == student.id;
|
// Convert obj to Student
|
||||||
|
|
||||||
|
// Step 3:
|
||||||
|
// Compare IDs
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,42 +1,44 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
import dev.model.Student;
|
import dev.model.Student;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
|
||||||
public class StudentManager {
|
public class StudentManager {
|
||||||
|
|
||||||
private final ArrayList<Student> students = new ArrayList<>();
|
private final ArrayList<Student> students = new ArrayList<>();
|
||||||
|
|
||||||
public void addStudents(ArrayList<Student> newStudents) {
|
public void addStudents(ArrayList<Student> newStudents) {
|
||||||
|
|
||||||
// ادغام لیست دانشجویان جدید با لیست اصلی
|
// TODO:
|
||||||
if (newStudents != null) {
|
|
||||||
students.addAll(newStudents);
|
// Merge students
|
||||||
}
|
|
||||||
|
// Hint:
|
||||||
|
// addAll()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Student searchStudent(int id) {
|
public Student searchStudent(int id) {
|
||||||
|
|
||||||
// چرخیدن بین تک تک دانشجویان لیست
|
// TODO:
|
||||||
for (Student s : students) {
|
|
||||||
// مقایسه شناسهها برای پیدا کردن دانشجوی مورد نظر
|
// Loop through students
|
||||||
if (s.getId() == id) {
|
|
||||||
return s;
|
// Compare IDs
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeStudent(int id) {
|
public boolean removeStudent(int id) {
|
||||||
|
|
||||||
// پیدا کردن موقعیت دانشجو در لیست برای حذف آن
|
// TODO:
|
||||||
for (int i = 0; i < students.size(); i++) {
|
|
||||||
if (students.get(i).getId() == id) {
|
// Find student
|
||||||
// حذف دانشجو از لیست اصلی
|
|
||||||
students.remove(i);
|
// Remove student
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,6 +47,9 @@ public class StudentManager {
|
|||||||
for (Student student : students) {
|
for (Student student : students) {
|
||||||
|
|
||||||
System.out.println(student);
|
System.out.println(student);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,49 +1,59 @@
|
|||||||
package dev.thread;
|
package dev.thread;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import dev.model.Student;
|
import dev.model.Student;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class StudentLoader extends Thread {
|
public class StudentLoader extends Thread {
|
||||||
|
|
||||||
// خطوط اختصاص داده شده از فایل به این ترد
|
// Assigned file lines
|
||||||
|
|
||||||
private final List<String> lines;
|
private final List<String> lines;
|
||||||
|
|
||||||
// محل ذخیرهسازی محلی دانشجویان برای همین ترد
|
// Local student storage
|
||||||
|
|
||||||
private final ArrayList<Student> students = new ArrayList<>();
|
private final ArrayList<Student> students = new ArrayList<>();
|
||||||
|
|
||||||
public StudentLoader(List<String> lines) {
|
public StudentLoader(List<String> lines) {
|
||||||
|
|
||||||
this.lines = lines;
|
this.lines = lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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) {
|
||||||
|
|
||||||
// اگر خط خالی بود از آن رد میشویم
|
// TODO:
|
||||||
if (line.trim().isEmpty()) {
|
// split line
|
||||||
continue;
|
|
||||||
|
// TODO:
|
||||||
|
// read values
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// create Student
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// add to students list
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// قطعهبندی خط بر اساس ویرگول
|
|
||||||
String[] tokens = line.split(",");
|
|
||||||
|
|
||||||
// خواندن مقادیر از آرایه توکنها
|
|
||||||
int id = Integer.parseInt(tokens[0].trim());
|
|
||||||
String name = tokens[1].trim();
|
|
||||||
String major = tokens[2].trim();
|
|
||||||
double gpa = Double.parseDouble(tokens[3].trim());
|
|
||||||
|
|
||||||
// ساختن شیء دانشجو
|
|
||||||
Student student = new Student(id, name, major, gpa);
|
|
||||||
|
|
||||||
// اضافه کردن به لیست محلی همین ترد
|
|
||||||
students.add(student);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Student> getStudents() {
|
public ArrayList<Student> getStudents() {
|
||||||
|
|
||||||
return students;
|
return students;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package dev.utils;
|
|||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -17,14 +18,15 @@ public class FileManager {
|
|||||||
|
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
|
|
||||||
// خطوط خوانده شده را به لیست اضافه میکنیم
|
// TODO:
|
||||||
if (!line.trim().isEmpty()) {
|
|
||||||
lines.add(line);
|
// Add line to list
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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