File tree Expand file tree Collapse file tree 2 files changed +9
-8
lines changed
src/com/codecafe/concurrency/thread/basics Expand file tree Collapse file tree 2 files changed +9
-8
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ Simple and easy to understand code examples for most of the Concurrent APIs prov
12
12
- [ Callable] ( ./src/com/codecafe/concurrency/thread/basics/designathread/CallableDemo.java )
13
13
- [ ExecutorService] ( notes/executorservice.md ) - [[ code] ( ./src/com/codecafe/concurrency/executorservice/ExecutorServiceDemo.java )]
14
14
- [ Thread States] ( ./notes/thread-states.md ) - [[ code] ( ./src/com/codecafe/concurrency/thread/basics/designathread/ThreadStates.java )]
15
- - [ Stop a Thread] ( ./src/com/codecafe/concurrency/thread/basics/StopAThreadInMiddle .java )
15
+ - [ Thread Termination ] ( ./src/com/codecafe/concurrency/thread/basics/Main .java )
16
16
- [ Thread Signalling] ( ./src/com/codecafe/concurrency/threadsignalling )
17
17
- [ Daemon Thread] ( ./notes/daemon-thread.md ) - [[ code] ( ./src/com/codecafe/concurrency/thread/daemonthread )]
18
18
Original file line number Diff line number Diff line change 1
1
package com .codecafe .concurrency .thread .basics ;
2
2
3
- class MyThread1 extends Thread {
3
+ class LongComputationTask implements Runnable {
4
4
5
+ @ Override
5
6
public void run () {
6
- // Intentional infinite loop
7
+ // Intentional infinite loop to simulate a long computation task
7
8
for (; ; ) {
8
9
// Returns true if the thread is interrupted
9
- if (interrupted ()) {
10
+ if (Thread . currentThread (). isInterrupted ()) {
10
11
// You are supposed to roll back or reverse the operation in progress and stop
11
- System .out .println ("Thread is interrupted hence stopping..." );
12
+ System .out .println ("\n Thread is interrupted hence stopping..." );
12
13
// Terminates the loop
13
14
break ;
14
15
}
@@ -18,14 +19,14 @@ public void run() {
18
19
19
20
}
20
21
21
- public class StopAThreadInMiddle {
22
+ public class Main {
22
23
23
24
public static void main (String [] args ) {
24
- MyThread1 th = new MyThread1 ( );
25
+ Thread th = new Thread ( new LongComputationTask () );
25
26
th .start ();
26
27
27
28
try {
28
- Thread .sleep (10 );
29
+ Thread .sleep (50 );
29
30
} catch (InterruptedException e ) {
30
31
e .printStackTrace ();
31
32
}
You can’t perform that action at this time.
0 commit comments