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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:54:27+00:00 2026-05-18T02:54:27+00:00

Please bear with me on this as I’m still getting the hang on Java.

  • 0

Please bear with me on this as I’m still getting the hang on Java.

The example bellow reads from a Class named Parent that creates an instance of itself on the main method. It then makes that instance do all sorts of calculations.

Next it fires up a thread named Child passing the Parent instance as a reference to Child.

The Child just sits there monitoring things and, sometimes, fires up public methods on Parent.

It works. The question is, is this poor style? Is there a more Java thinking way of doing this sort of work?

public class Parent {

    // main function that fires up the program
    public static void main() {

        // creates an instance of himself
        // and fires go that does all sorts of fuzzy calculus
        Parent parent = new Parent();
        parent.go();

        // creates a new thread of child and starts it
        Child child = new Child(parent);
        child.start();
    }

    private void go() {
        // all sort of initializations
    }

    public void getDataFromChild(int data) {
        // will get data from running child thread
    }
}



public class Child extends Thread {
    private Parent parent;

    // child constructor grabs Parent instance into "o"
    public Child(Parent o) {
        parent = o;
    }

    // this class main loop
    public void run() {
        while(1==1) {
            doSomething();
            try {
                sleep(1000);
            }
            catch(Exception e) { }
        }
    }

    private void doSomething() {
        parent.getDataFromChild(1);
    }

}

Thank you.

  • 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-18T02:54:28+00:00Added an answer on May 18, 2026 at 2:54 am

    Subclassing Thread is considered bad style. It’s better to implement Runnable, and then hand the runnable off to a thread (hmmm… quite analogous to your Parent/Child hand-off!).

    Runnable r = new Child(parent);
    new Thread(r).start();
    

    Otherwise your Child.java code looks fine to me.

    To implement Runnable you just need to provide a run() method:

    public class Child implements Runnable {
      private Parent parent;
    
      public Child(Parent parent) { this.parent = parent; }
    
      public void run() {
        // what the thread does goes in here...
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is all hypothetical, so please bear with me. Say I'm writing a tool
This is a long text. Please bear with me. Boiled down, the question is:
This might be a bit difficult to explain in writing, so please bear with
(I still feel like a complete newbie in MS Visual environments... so please bear
Please bear with me if this question isn't well formulated. Not knowing is part
Please bear with me as I've been thrown into the middle of this project
Please bear with me Silverlight Designer Gurus, this is compicated (to me). I'm creating
This is kinda confusing so please bear with me. I have an array (listOfStates)
I'm a fairly new programmer so please bear with me on this. I am
Please bear with me, I'm just learning C++. I'm trying to write my header

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.