Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6984437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:32:57+00:00 2026-05-27T18:32:57+00:00

public class thread extends Thread { static volatile boolean done = false;// volatile keyword

  • 0
public class thread extends Thread {

    static volatile boolean done = false;// volatile keyword is used            

@Override
public void run() {
    while (!done) {
        for (int i = 1; i < 10; i++) {

            try {
                thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(currentThread());
            System.out.println("1st thread>> " + i);
        }
    }
}

public static void main(String[] args) throws InterruptedException {

    Thread th = new Thread(new thread(),"mythread");
    th.start();
    for (int i = 1; i < 10; i++) {
        thread.sleep(400);
        System.out.println(currentThread());
        System.out.println("2nd thread>> " + i);
        if (i == 4) {
            done = true;// safe way to stop a thread
            break;
        }
    }

   }
  }

I am using volatile static variable here.Is it a safe way to stop a thread and also
when I print currentThread() method I get the output like Thread[mythread,5,main]
what does the 5 and main refer to??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:32:57+00:00Added an answer on May 27, 2026 at 6:32 pm

    It’s a safe way to stop a thread, but there is no reason for the variable to be static: you want to stop one thread, not all threads of the same class.
    Moreover, there is a more standard and less fragile way to stop a thread: interrupting it.

    public void run() {
        while (!Thread.currentThread().isInterrupted() {
            ...
        }
    }
    
    ...
    
    th.interrupt();
    

    This has the additional advantage that a thread which is sleeping or waiting, or blocked in an interruptible IO method will be woken up with an InterruptedException. When such an exception happens, it means that the thread should stop running, so you shouldn’t swallow the exception as you did. Instead, you should return from the run method as fast as possible:

    try {
        thread.sleep(200);
    } 
    catch (InterruptedException e) {
        return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class TowThreads { public static class FirstThread extends Thread { public void run()
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
package pkg_1; public class ExpOnWaitMethod extends Thread { static Double x = new Double(20);
public class PlayText extends Thread { private int duration; private String text; private PlayerScreen
public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class SieveGenerator{ static int N = 50; public static void main(String args[]){ int
Assume I have a class like this: public class Server { public static void
I have a class ThreadClass which looks as follows: public class ThreadClass extends Thread{
In other words, is this Singleton implementation thread safe: public class Singleton { private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.