feat: Add comprehensive examples from multithreading and hashing lectures
This commit is contained in:
+16
-39
@@ -9,15 +9,11 @@ import java.util.Scanner;
|
||||
|
||||
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) {
|
||||
|
||||
@@ -29,17 +25,13 @@ public class Main {
|
||||
5.Exit
|
||||
""");
|
||||
|
||||
int choice =
|
||||
scanner.nextInt();
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case 1 -> {
|
||||
|
||||
List<String> data =
|
||||
FileManager
|
||||
.loadStudents(
|
||||
"students.txt");
|
||||
List<String> data = FileManager.loadStudents("students.txt");
|
||||
|
||||
// TODO:
|
||||
|
||||
@@ -53,14 +45,11 @@ public class Main {
|
||||
|
||||
// TODO:
|
||||
|
||||
StudentLoader t1 =
|
||||
null;
|
||||
StudentLoader t1 = null;
|
||||
|
||||
StudentLoader t2 =
|
||||
null;
|
||||
StudentLoader t2 = null;
|
||||
|
||||
StudentLoader t3 =
|
||||
null;
|
||||
StudentLoader t3 = null;
|
||||
|
||||
// TODO:
|
||||
|
||||
@@ -74,40 +63,28 @@ public class Main {
|
||||
|
||||
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 -> {
|
||||
|
||||
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 ->
|
||||
manager
|
||||
.printStudents();
|
||||
manager.printStudents();
|
||||
|
||||
case 5 -> {
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package lecture.hash;
|
||||
|
||||
public class User {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public User(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
User user = (User) o;
|
||||
return id == user.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Integer.hashCode(id);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
User u1 = new User(101, "Ali");
|
||||
User u2 = new User(101, "Ali");
|
||||
|
||||
System.out.println("Are they logically equal? " + u1.equals(u2));
|
||||
|
||||
System.out.println("Do they have the same Hash? " + (u1.hashCode() == u2.hashCode()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
public class InterruptExample {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread worker = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("👷 Worker started...");
|
||||
|
||||
try {
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
System.out.println(" worker: stage " + i);
|
||||
Thread.sleep(1000); // Each step takes 1 second
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("🛑 Worker: Hey! Someone cut me off!");
|
||||
System.out.println("👷 Worker: OK, I'm finishing...");
|
||||
}
|
||||
|
||||
System.out.println("👷 Goodbye worker!");
|
||||
}
|
||||
});
|
||||
|
||||
worker.start();
|
||||
|
||||
Thread.sleep(6500); // Let the worker work for 6.5 seconds
|
||||
|
||||
System.out.println("💀 Main thread: That's enough! Cut it!");
|
||||
worker.interrupt(); // Tells the worker to stop
|
||||
|
||||
worker.join();
|
||||
System.out.println("Everything is finished!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
public class IsAliveExample {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread worker = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("👷 Worker started...");
|
||||
try {
|
||||
Thread.sleep(15000); // Runs for 15 seconds
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("👷 The worker is done!");
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("before start: " + worker.isAlive()); // false
|
||||
|
||||
worker.start();
|
||||
|
||||
System.out.println("Immediately after start: " + worker.isAlive()); // true
|
||||
|
||||
Thread.sleep(1000); // We wait 1 second
|
||||
System.out.println("After 1 second: " + worker.isAlive()); // true (still works)
|
||||
|
||||
worker.join(); // We wait for the worker to finish
|
||||
|
||||
System.out.println("After completion of work: " + worker.isAlive()); // false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
public class IsInterruptedExample {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
|
||||
Thread counter = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int number = 0;
|
||||
|
||||
// Continue until interrupted
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
number++;
|
||||
System.out.println("Count: " + number);
|
||||
|
||||
try {
|
||||
Thread.sleep(50); // 50 milliseconds between each number
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Interrupted while sleeping!");
|
||||
break;
|
||||
}
|
||||
|
||||
if (number >= 100) {
|
||||
break; // We reached 100, don't continue
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Thread stopped! Last count: " + number);
|
||||
}
|
||||
});
|
||||
|
||||
counter.start();
|
||||
|
||||
// Let it work for 2 seconds, then stop it
|
||||
Thread.sleep(2000);
|
||||
System.out.println("I'm disconnecting...");
|
||||
counter.interrupt();
|
||||
|
||||
counter.join();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
class WorkerTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("👷 Worker started...");
|
||||
try {
|
||||
Thread.sleep(10000); // Runs for 10 seconds
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("👷 The worker finished his work!");
|
||||
}
|
||||
}
|
||||
|
||||
public class JoinSimpleExample {
|
||||
public static void main(String[] args) {
|
||||
WorkerTask task = new WorkerTask();
|
||||
Thread worker = new Thread(task);
|
||||
|
||||
worker.setName("Worker-Thread");
|
||||
worker.start();
|
||||
|
||||
System.out.println("The main thread waits until the worker finishes...");
|
||||
|
||||
try {
|
||||
worker.join(); // The main thread stops here until the worker finishes
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println("Main thread continues - now that the worker is finished!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
public class MyRunnable implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Runnable is running: " + Thread.currentThread().getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
public class MyThread extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Thread is running: " + Thread.currentThread().getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
public class SleepExample {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("starting program");
|
||||
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
System.out.println("Count: " + i);
|
||||
|
||||
try {
|
||||
Thread.sleep(1000); // 1 second stop
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Thread terminated!");
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("End of program");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
public class StandardPattern {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
|
||||
Thread worker = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Both together: checking in the loop
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
// doing work
|
||||
System.out.println("Working...");
|
||||
|
||||
// Work simulation
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
// When we are interrupted in sleep
|
||||
System.out.println("Interrupted during sleep!");
|
||||
Thread.currentThread().interrupt(); // Reset status
|
||||
// break;
|
||||
}
|
||||
}
|
||||
System.out.println("Thread stopped!");
|
||||
}
|
||||
});
|
||||
|
||||
worker.start();
|
||||
Thread.sleep(1000);
|
||||
worker.interrupt(); // Send a sign
|
||||
worker.join();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
public class ThreadNamingExample {
|
||||
public static void main(String[] args) {
|
||||
// Create a thread without a specified name
|
||||
Thread thread1 = new MyThread();
|
||||
|
||||
// Set thread name
|
||||
thread1.setName("Worker-1");
|
||||
|
||||
// Get and print the name of the thread
|
||||
System.out.println("Thread name before start: " + thread1.getName());
|
||||
thread1.start();
|
||||
|
||||
// Main thread name
|
||||
Thread mainThread = Thread.currentThread();
|
||||
System.out.println("main thread name: " + mainThread.getName());
|
||||
|
||||
// Rename the main thread
|
||||
mainThread.setName("Main-Thread");
|
||||
System.out.println("New name of main thread: " + mainThread.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package lecture.multithreading.basics;
|
||||
|
||||
|
||||
public class ThreadVsRunnable {
|
||||
public static void main(String[] args) {
|
||||
|
||||
MyThread thread1 = new MyThread();
|
||||
thread1.start();
|
||||
|
||||
Thread thread2 = new Thread(new MyRunnable());
|
||||
thread2.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package lecture.multithreading.timeout;
|
||||
|
||||
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user