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

  • SEARCH
  • Home
  • 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 8790821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:45:19+00:00 2026-06-13T22:45:19+00:00

As in the subject do I correctly shutdown these simultaneous threads? I assigned a

  • 0

As in the subject do I correctly shutdown these simultaneous threads?

I assigned a volatile field and check it repeatedly in while loop.

Is there alternative way to do it(like using synchronize or wait() method), please show me.

EDIT I edited code. Is there any way of checking if Thread is alive by different method thatn isAlive();?
Perhaps:

boolean isAlive(){
    return running;
}

import javax.swing.JOptionPane;

public class Wat extends Thread {
    private char c;
    private int interv;
    private volatile boolean running = true;
    Object synchObj;

    public Wat(char c, int interv) {
        this.c = c;
        this.interv = interv;
        synchObj = new Object();
    }

    public void run() {
        while (running) {
            synchronized (synchObj) {
                try {
                    showChar(c);
                    synchObj.wait(interv * 100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public synchronized static void showChar(char c) {
        System.out.println(c);
    }

    public void shutdown() {
        running = false;
        synchronized (synchObj) {
            synchObj.notify();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Wat w1 = new Wat('A', 3);
        Wat w2 = new Wat('B', 4);
        Wat w3 = new Wat('C', 5);
        w1.start();
        w2.start();
        w3.start();
        Object[] options = { "Shutdown A", "Shutdown B", "Shutdown C" };
        int option;
        while (w1.isAlive() || w2.isAlive() || w3.isAlive()) {
            option = JOptionPane.showOptionDialog(null,
                    "Which one would you like to shut?", "Threads",
                    JOptionPane.YES_NO_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
            switch (option) {
            case JOptionPane.YES_OPTION:
                w1.shutdown();
                break;
            case JOptionPane.NO_OPTION:
                w2.shutdown();
                break;
            case JOptionPane.CANCEL_OPTION:
                w3.shutdown();
                break;
            }
            Thread.sleep(1);
        }
    }
}
  • 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-06-13T22:45:20+00:00Added an answer on June 13, 2026 at 10:45 pm

    Your code will probably work fine, but the Thread.sleep is not very elegant. I would do something along these lines, calling the shutdown() method to quit the thread

     Object synchObj = new Object();
    
     public void run() {
        while (running) {
          synchronized (synchObj) {
             try {
                System.out.println(new Date());
                synchObj.wait(5000);
             } catch (InterruptedException e) {
                 // error handling
             }
          }
        }
     }
    
     public void shutdown() {
        running = false;
        synchronized (synchObj) {
            synchObj.notify();
        }
     }
    
    
    public static void main(String[] args) throws InterruptedException,
            IOException {
        ThreadTest test = new ThreadTest();
        test.start();
        BufferedReader tReader = new BufferedReader(new InputStreamReader(
                System.in));
        tReader.readLine();
        test.shutdown();
    }
    

    EDIT added test code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if I worded the subject correctly but essentially I'm curious if
Subject says it all really...Is there anywhere a good tutorial for Xcode's Debugger out
Maybe there is already a question about this subject, but I couldn't find it.
I know there are some questions touching this subject, but none have helped me
I've read a lot on this subject, including a lot on SO. So while
I have an MSpec test to check whether my forms auth is correctly redirecting
I'm developing a facebook app. The tab removes correctly, but is there a way
I'm not sure if I worded the subject correctly. I am looping through an
Pretty sure these tests are working correctly. Got them to fail by removing the
I know there are several questions on this subject but none of them seem

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.