package com.codegym.task.task16.task1619;
/*
No interrupt, no dice?
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new TestThread());
t.start();
Thread.sleep(3000);
ourInterrupt();
}
public static void ourInterrupt() {
}
public static class TestThread implements Runnable {
public void run() {
while (true) {
try {
System.out.println("he-he");
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
}
}
Give me a hint, how to interrupt without using interrupt()
Under discussion
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Ramen
30 December 2022, 05:48
There's 2 ways to stopping a Thread. Use the lesson that explains the unofficial method to stopping a Thread by using a boolean value. The interrupt method is the official method to stopping a Thread, but sometimes we want to be able to use the unofficial way too.
0
Henk
28 May 2019, 13:19
I wanted to do that, but they said to change the while loop, and didn't know I was allowed to modify the MAIN section
+4
Ulan Mukanbetov
21 August 2019, 06:09
I think, if they didn't warn about changing MAIN method, that means you can do it )
0
Shreyas Joshi
22 April 2019, 14:10
public class Solution {
public static boolean isCancel = true;
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new TestThread());
t.start();
Thread.sleep(3000);
ourInterrupt();
}
public static void ourInterrupt() {
isCancel = false;
}
public static class TestThread implements Runnable {
public void run() {
while (isCancel) {
try {
System.out.println("he-he");
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
}
}
+2
Satya Vath
14 April 2019, 16:03
#1 You need to Add a public static boolean like
#2 You need to change the while loop to your boolean name like
#3 You need to change the ourInterrupt method like
+4
Roman
2 November 2018, 10:25
You can use some boolean flag and check it...
+4