CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Baljinder Singh
Level 20
Toronto
  • 29.10.2018
  • 1400views
  • 6comments

Give me a hint, how to interrupt without using interrupt()

Question about the task No interrupt, no dice?
Java Core,  Level 6,  Lesson 10
Under discussion

Figure out how the program works.
Make it so that the ourInterrupt method allows the TestThread to terminate itself.
Don't use the interrupt method.

Requirements:
  • The Solution class must have a public static method called ourInterrupt with no parameters.
  • The run method should display "he-he" every half second until the ourInterrupt method is called.
  • You must change the condition of the while loop in the run method.
  • The main method must create a Thread object by passing a TestThread object to the constructor.
  • The main method must call the start method on the Thread object.
  • The main method must call the ourInterrupt method.
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) { } } } } }
0
Comments (6)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Ramen
Level 18 , South Lake Tahoe, United States
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
Level 19 , Pretoria, South-Africa
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
Level 26 , Bishkek, Kyrgyzstan
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
Level 16 , Mumbai, India
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
Level 31 , Vijayawada, India
14 April 2019, 16:03
#1 You need to Add a public static boolean like
public static boolean isCancel = true;
#2 You need to change the while loop to your boolean name like
while (isCancel)
#3 You need to change the ourInterrupt method like
public static void ourInterrupt() {
isCancel = false;
    }
+4
Roman
Level 41
2 November 2018, 10:25
You can use some boolean flag and check it...
+4
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.