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 8779623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:50:21+00:00 2026-06-13T19:50:21+00:00

I would like to make that OptionPanel makes threads shutdown themselves. I know it

  • 0

I would like to make that OptionPanel makes threads shutdown themselves. I know it suppose to be done in the run method of the thread class but I don’t know if I should to synchronize something there or not.

How to make that these threads will shut themselves after clicking correct option in JOptionPanel?

import javax.swing.JOptionPane;

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

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

    public void run() {

        while (running) {
            try {
                System.out.println(c);
                Thread.sleep(interv * 100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        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.running = false;
                break;
            case JOptionPane.NO_OPTION:
                w2.running = false;
                break;
            case JOptionPane.CANCEL_OPTION:
                w3.running = false;
                break;
            }
        }

    }

}
  • 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-13T19:50:23+00:00Added an answer on June 13, 2026 at 7:50 pm

    Given that you have:

    Thread.sleep(interv * 100);
    

    I would look at sending an interrupt to each thread. The interrupt will do just that to the sleep() method and it will be more responsive than the usual practise of looping around a boolean (e.g. while (!done) {....}).

    As well as the tutorial linked above, check out this DeveloperWorks article on handling InteruptedExceptions.

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

Sidebar

Related Questions

I would like to make a generic class that when tapped, makes the element
I would like to make sure that my circular buffer is thread safe. I
I would like to make sure that everything I know about UTF-8 is correct.
I would like to make sure that this method call is correct. I have
I would like to make sure that an applescript can be converted to bash.
I would like to make a function that takes in a list of integers
I would like to make a JComboBox that has check boxes for items instead
I would like to make a file that only my program has permissions to
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to make a php script that can capture a page from

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.