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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:17:20+00:00 2026-06-13T12:17:20+00:00

I have a main class which spawns a thread, let’s call them MainClass and

  • 0

I have a main class which spawns a thread, let’s call them MainClass and MyThread.

public class MainClass extends javax.swing.JFrame {
   int sharedVariable;
   MyThread threadInstance;
   public MainClass (){
      sharedVariable = 2;
      threadInstance = new MyThread(this);
      threadInstance.run();
   }

  public int getSharedVariable(){ return sharedVariable; }

  public static void main(String[] args){
    //begin main class
 }
} 


public class MyThread implements Runnable {

     MainClass class;
     public MyThread(MainClass main_class){
         this.main_class= main_class;
      }

     @Override
     public run(){

     while(this.main_class is still active){

      //grab status of sharedVariable and wait for x amount of time.
      }
     }
    } 

The problem is I do not know how to implement the while condition which checks if the MainClass instance is still alive and if it is, it has to use the this.main_class.getSharedVariable() to get the value of sharedVariable, then wait for x amount of time. MainClass has the main method .

  • 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-13T12:17:21+00:00Added an answer on June 13, 2026 at 12:17 pm

    I would recommend holding onto the Thread instance and then calling threadInstance.interrupt() right before the main(...) method exits.

    Something like:

     public static void main(String[] args){
        MainClass mainClass = new MainClass();
        try {
            ...
            // do main stuff here
            ...
        } finally {
            mainClass.threadInstance.interrupt();
        }
     }
    

    Then in your thread you’d do:

     while(!Thread.currentThread().isInterrupted()){
         ...
     }
    

    You’d also want to handle InterruptedException correctly:

     try {
         Thread.sleep(1000);
     } catch (InterruptedException e) {
         // always a good pattern to re-interrupt the thread here
         Thread.currentThread().interrupt();
         // if we are interrupted quit
         return;
     }
    

    Btw, it is very bad form to leak the instance of an object during construction to another thread:

     new MyThread(this);
    

    See here: Why shouldn't I use Thread.start() in the constructor of my class?

    Also, you aren’t starting a thread when you call threadInstance.run();. You are just running it in the current thread. You should use threadInstance.start() but not inside of the constructor like that.

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

Sidebar

Related Questions

I have a main class which extends Activity which should spawn a ListActivity and
I currently have a main class which might call another class which might call
In the application I am writing, I have a main class which extends the
I have this code: class FinalUI1 extends javax.swing.JFrame { //do something Thread t; try
I have my main class from which i call my sub class. My sub
I have a class which is extending Application public class MyContext extends Application {
I have a main class(which is basically a netbeans form;drag and drop) from where
I have a Service class and an main Acitivity class which is supposed to
I have a main class and two extended classes: class Main { public $foo;
I have a question about the 'Event Dispatch Thread'. I have a Main class

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.