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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:59:22+00:00 2026-05-27T15:59:22+00:00

Here is a few bit of code: public class ShowDialog extends Thread { private

  • 0

Here is a few bit of code:

public class ShowDialog extends Thread {
private static String mTitle="Please wait";
private static String mText="Loading...";
private Activity mActivity;
private ProgressDialog mDialog;

ShowDialog(Activity activity) {
    this(activity, mTitle, mText);
}
ShowDialog(Activity activity, String title) {
    this(activity, title, mText);
}
ShowDialog(Activity activity, String title, String text) {
    super();
    mText=text;
    mTitle=title;
    mActivity=activity;
    if (mDialog == null) {
        mDialog = new ProgressDialog(mActivity);
        mDialog.setTitle(mTitle);
        mDialog.setMessage(mText);
        mDialog.setIndeterminate(true);
        mDialog.setCancelable(true);
        mDialog.setOnCancelListener(
            new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface arg0) {
                    mDialog.dismiss();
                    interrupt();
                }
            });
    }
}

public void run() {
    mDialog.show();
    while(!isInterrupted())
    mDialog.dismiss();
    mDialog=null;
}

}

And in my main activity:

ShowDialog show = new ShowDialog(this, "Please wait!","Loading badly...");
    show.start();
    SystemClock.sleep(2000);
    show.interrupt();

I know I might use an async task and all the stuff but that is not what I want. Replace the SystemClock.sleep by anything that takes some time. The idea is to execute the code between start and interrupt in the UI thread and make a seperate thread handling the ProgressDialog.
What’s wrong with my thread ?

Thanks a lot!

  • 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-27T15:59:23+00:00Added an answer on May 27, 2026 at 3:59 pm

    To wait for a Thread to complete, you should use the Thread.join() method. But now I see you’re not wanting to wait for it to complete, but you need to control when it completes. Still, you want to avoid interrupting threads in this fashion.

    In your ShowDialog class, add a dismiss() method that you can call from your main, instead of interrupt(). Also add a boolean dismiss = false instance variable. In dismiss(), add dismiss = true, then notify();. In run(), replace your constant while() loop (was running constantly, and very inefficient) with while(!dismiss){wait()}. You will still need to add synchronization blocks and exception handling, but this should get you off to a good start.

    Here is a generic-Java (non-android) simplified example:

    public static class ShowDialog extends Thread{
    
        protected boolean dismiss;
    
        public void dismiss(){
            dismiss = true;
            synchronized(this){
                notifyAll();
            }
        }
    
        @Override
        public void run(){
            System.out.println("Running...");
            // Show your dialog here.
            while(!dismiss){
                synchronized(this){
                    try{
                        wait();
                    }catch(InterruptedException ie){
                        ie.printStackTrace();
                    }
                }
            }
            System.out.println("Quitting...");
            // Dismiss your dialog here.
        }
    
    }
    
    public static void main(String[] args) throws Exception{
        ShowDialog sd = new ShowDialog();
        sd.start();
        Thread.sleep(2000);
        sd.dismiss();
    }
    

    You mentioned you didn’t want to use AsyncTask, but I would still reconsider. (What are your reasons against it?)

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

Sidebar

Related Questions

Here are a few properties of my class. There is also a public object
Basically I'm going to go a bit broad here and ask a few questions
Here's a few questions I had on a quiz in a class and just
Here are a few example of classes and properties sharing the same identifier: public
This is a bit esoteric, but there have to be a few people here
I have been developing a bit of code for the last few weeks. The
I've posted the same question here a few days ago( Java reading standard output
Here are a few possibilities to get the conversation started: Escape all input upon
There are a few pages here addressing usage of jQuery with .net masterpages -
I've got a few controllers here at work that contain methods I can use

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.