Skip to content

Commit

Permalink
6. Прерывание блокированной нити.
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzor committed Sep 11, 2024
1 parent 54f9479 commit 6470ae3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ru/j4j/concurrent/ConsoleProgress.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void run() {
i = 0;
}
} catch (InterruptedException e) {
break;
Thread.currentThread().interrupt();
}
}
}
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/ru/j4j/concurrent/ThreadStop.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package ru.j4j.concurrent;


public class ThreadStop {
public static void main(String[] args) throws InterruptedException {
Thread first = new Thread(() -> {
int counter = 0;
while (!Thread.currentThread().isInterrupted()) {
System.out.println(counter++);
}
});
first.start();
Thread progress = new Thread(
() -> {
while (!Thread.currentThread().isInterrupted()) {
try {
System.out.println("start ...");
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Thread is interrupted: " + Thread.currentThread().isInterrupted());
System.out.println("Thread state: " + Thread.currentThread().getState());
Thread.currentThread().interrupt();
}
}
}
);
progress.start();
Thread.sleep(1000);
first.interrupt();
progress.interrupt();
progress.join();
}
}
}

0 comments on commit 6470ae3

Please sign in to comment.