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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:01:44+00:00 2026-06-13T02:01:44+00:00

Say I have an Object Foo that wants to get informed by several running

  • 0

Say I have an Object Foo that wants to get informed by several running instances of a Thread using a listener interface. E.g.

The interface:

public interface ThreadListener {
   public void onNewData(String blabla);
}

The class Foo:

public class Foo implements ThreadListener {
   public Foo() {
      FooThread th1 = new FooThread();
      FooThread th2 = new FooThread();
      ...

      th1.addListener(this);
      th2.addListener(this);
      ...

      th1.start();
      th2.start();
      ...
   }

   @Override
   public void onNewData(String blabla) {
     ...
   }
}

The Thread:

public FooThread extends Thread {
   private ThreadListener listener = null;

   public void addListener(ThreadListener listener) {
      this.listener = listener;
   }

   private void informListener() {
      if (listener != null) {
         listener.onNewData("Hello from " + this.getName());
      }
   }

   @Override
   public void run() {
     super.run();

     while(true) {
        informListener();
     }
   }
}

In the worst case onNewData(..) is invoked by several threads at the same time. What will happen with Foo? Is it going to crash or not?

  • 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-13T02:01:45+00:00Added an answer on June 13, 2026 at 2:01 am
    • Your Foo class has no state (fields), so unless it uses external shared resources (e.g. files…) it is thread safe
    • Starting thread from a constructor is generally a bad idea although in the case of a state-less object, I suppose it is fine
    • if onNewData does not access shared data it will work as expected, if it does, the outcome will depend on how the method is implemented.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have some Foo class that contains public void setOnSomethingListener(OnSomethingListener listener); We
Say I have an object: struct Foo { int bar_; Foo(int bar) bar_(bar) {}
Let's say I have an object who's class definition looks like: class Command: foo
Say I have an object containing a value. I wish to get the index
Say I have an object MyObj stuff; To get the address of stuff, I
Lets say I have an object with two boolean properties: public bool AdvancedMode{ get;
Lets say I have an object that has stringProp1, stringProp2. I wish to store
Say I have a tableview class that lists 100 Foo objects. It has: @property
Say I have a TabControl that is presenting a collection of Foo objects (each
I have a JSON object, say: { foo: { bar: 1 }, baz: 2

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.