package com.codegym.task.task16.task1632;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static List<Thread> threads = new ArrayList<>(5);
static {
threads.add ( new T1 () );
threads.add ( new T2 () );
threads.add ( new T3 () );
threads.add ( new T4 () );
threads.add ( new T5 () );
}
public static void main(String[] args) {
}
}
class T1 extends Thread{
@Override
public void run() {
while(true){
}
}
}
class T2 extends Thread{
@Override
public void run() {
if(this.isInterrupted ()) {
System.out.println ( "InterruptedException" );
}
}
}
class T3 extends Thread{
@Override
public void run() {
try {
while (true) {
System.out.println ( "Hurray" );
Thread.sleep ( 500 );
}
}catch (InterruptedException i){
}
}
}
class T4 extends Thread implements Message{
boolean going = true;
@Override
public void showWarning() {
this.stop ();
}
@Override
public void run() {
while(isAlive ()){
}
}
}
class T5 extends Thread{
@Override
public void run() {
BufferedReader reader = new BufferedReader ( new InputStreamReader (System.in) );
try {
String data = reader.readLine ();
int total = 0;
while(!data.equals ( "N" )){
int num =Integer.parseInt ( data );
total = total + num;
data = reader.readLine ();
}
System.out.println ( total );
}catch (IOException o){
}
}
}
Can somebody from codegym explain why stop works and interrupt doesnt
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
8 October 2019, 02:08
Interrupt() triggers a Boolean flag inside the thread class to be false, but does not stop it. You can then call that flag with (Thread).Interrupted(), like you did with your second Thread.
https://stackoverflow.com/questions/20527893/java-thread-stop-vs-thread-interrupt
0