Java threads calling parent functions

Im making some software written in java, it involves a single 'Parent' class that invokes dozens of classes(nodes) that run in their own thread (extends Thread).

The classes store the parent class as a variable. If the node class invokes a method from the parent class, does each node store a copy of the parent object or do all the nodes act upon the same object? if all nodes act on the same object, do statements in the called function run in the thread of the node or in the thread of the parent class?

EX code:

class parent { ArrayList<node> nodes = new ArrayList<node>(); void createStartNodes() { for(int i =0; i < 36;++i) { nodes.put(i,new node(this)); nodes.get(i).start(); } } void callSomeFunc() { /* things */ } } class node extends Thread { parent par; public node(parent p) { par=p; } @Override public void run() { par.callSomeFunc(); } }

-------------Problems Reply------------

does each node store a copy of the parent object or do all the nodes act upon the same object?

They each store a pointer to the memory location of the parent object. They indeed act upon the same object.

if all nodes act on the same object, do statements in the called function run in the thread of the node or in the thread of the parent class?

They run in the thread of the node that tries to modify the parent object. You may want to make sure you use synchronization properly when doing that, for example by using a synchronized block. Otherwise, when there is concurrent access to the parent object, you may get incorrect / inconsistent behaviour. Example:

class Node extends Thread
Parent par;

public Node(Parent p)

public void run()
synchronized(par) {

The synchronized block will give this thread exclusive access to the parent object.

Also, thread specific logic should be in the run method, not in the start method.

I think you're confusing things. I just want to clarify these before answering your questions:

  • A class does not "run in a thread", because it does not "run". You can run methods, not classes. A method of a class could be executed in different threads depending on where you called it.
  • When extending the Thread class (note the capital T), you should override the run() method, not the start() method, otherwise the multithreading stuff won't happen. However, you indeed have to call start(), defined in the Thread class, which in turn will call your run() method in the created thread, as you would expect. [EDIT: this point was apparently solved in the OP's edit]

That being said, back to your actual questions:

does each node store a copy of the parent object or do all the nodes act upon the same object?

Every java object (read non-primitive type) is just a reference, you don't pass object values. Therefore, all your nodes store a reference to the same object, because you gave them the reference (this) to the same object when calling their constructor.

do statements in the called function run in the thread of the node or in the thread of the parent class?

In the current state of your code, you're not overriding run() but start(), so you don't have multiple threads anyway. (solved by OP)

The code executed in the run() method will be executed in the thread of the node, even when calling other methods from run().

Sidenote: this is because you called node.start() properly. If you directly called, it would be executed in the thread from which you call run().

First of all you are not supposed to override the start() method of Thread class, you are supposed to call it. And Similarly you are not supposed to call run() method of Thread class, you supposed to override it.

So when you call the method which is written in the your "parent" class from the run() method, it acts in the new instance and its not shared by all the other thread that you have created.

Edit: As you have passed "this" to constructor, they will share the same object.

Category:java Views:6 Time:2018-03-13
Tags: java

Related post

  • How can I log which thread called which function from which class and at what time throughout my whole project? 2010-06-29

    I am working on a fairly large project that runs on embedded systems. I would like to add the capability of logging which thread called which function from which class and at what time. E.g., here's what a typical line of the log file would look like

  • Thread calling enqueue function in C 2011-05-24

    I have: struct elem { data d; struct elem *next; }; typedef struct elem elem; struct queue { int cnt; elem *front; elem *rear; }; void enqueue(data d, queue *q); void enqueue(data d, queue *q) { elem *p; p = malloc(sizeof(elem)); p -> d = d; p -

  • Two threads call one function > Does local variable access need to be synchronized? 2011-08-18

    If two threads call one function "simultaneously," do variables local to the function (not ivars) need to be protected in a mutex/synchronization block to keep other threads from altering/munging them? Or, does each thread get its own copy of the loc

  • php calling parent function makes parent unable to load own vars 2012-03-25

    I have a Handler class that looks like this: class Handler{ public $group; public function __construct(){ $this->group = $this->database->mysql_fetch_data("blabla query"); //if i print_r($this->group) here it gives proper result new Child

  • Java : Thread calling Runnable 2011-07-09

    I am newbie to java. One doubt. I read that, internally, Thread class's run() method calls the Runnable interface's run(). My Question is, How the Thread class's run() method calls the Runnable interface's run() ? thanks in advance. --------------Sol

  • Call parent function from an itemRenderer 2010-02-06

    I want to call the parent function called "edit_groups()" from the itemRenderer. The code for my itemRenderer is: <mx:VBox id="vbx_container" paddingBottom="4" paddingLeft="4" paddingRight="4" paddingTop="4" borderStyle="solid" dropShadowEnabled="

  • Java Threads calling methods on common Data Collector Object possible? 2011-07-16

    The idea: I have a JAX-RS webservice servlet (Object called webServlet) which instantiates a data collecting Object dataCollector and passes this object on to multiple threads in their constructor. These threads query websites for results and then ca

  • Calling Parent Function with the Same Signature/Name 2011-11-16

    A and B have function with the same signature -- let's assume : foo($arg) -- and that class A extends B. Now I have an instance: $a = new A(); $$data); Can I also run the parent's (B's) foo() function through $a or was it overridden? Thanks! Ni

  • Call parent function from iframe created with jquery 2012-04-12

    Consider this code: function callme() { alert('call'); } $(function(){ var iframe = $('<iframe />').attr('src', 'b.php').appendTo('body'); //call 'callme' function from parent }); I don't want to edit b.php. Is there any way I can call the pare

  • Call parent function in xcode iphone development 2010-10-04

    We have 2 files 'MainViewController' and 'View01' The first file addSubView the second one. In the first file we have a function named displayView. I want to be able to call this function from the View01 file is the following code correct part of Vie

  • PHP - "Call to a member function - on a non-object" when calling parent function 2012-02-13

    This question already has an answer here: Call to a member function on a non-object [duplicate] 8 answers All, I'm a fairly new and I mostly studied OOP with Java. With this said, here is the relevant code: Model.php class Model{ // Object that repre

  • Java (Android): calling a function from Context without cast 2011-12-02

    First off - I'm rather novice at Java so if the question makes no sense do let me know. Basically I'm making an Android app which communicates with my web service and so I've made a separate class to deal with the communication, which also includes t

  • Flash Builder Child Popup call Parent Function 2012-01-13

    I have a custom component that is created via as: public function myFunc():void { //some code } public function createComp():void { var myVar:customComp = new customComp(); myVar.button01.label = "Some label"; PopUpManager.addPopUp(myVar, this, true)

  • java script calling 3 function at one time 2011-05-06

    I am calling 3 JQuery function in only click ok link. Please tell me is this right or how i can do it. Its not working properly. <a href=# onClick="fill(\''.$result->pname.'\');fill1(\''.$result->person_name.'\');fill2(\''.$result->mobile

  • Calling self executing anonymous parent function in JS 2011-09-09

    given the following code (function(){console.log("ello");setTimeout("**CALL PARENT FUNCTION**",100);}()) how would one call this anonymous parent function every X duration.. --------------Solutions------------- Use the callee attribute on the argumen

  • Crosswalk call js function from java on android 2014-07-06

    I m trying to use crosswalk runtime in my android app. I tried this on android 4+. I got some js & html codes and it worked perfect for me. But it is not working like android webview. In the webview i can call javascript functions from java code.

  • Calling static functions from a thread in java? 2009-12-14

    Lets say I have a class like this in Java: public class Function { public static int foo(int n) { return n+1; } } What happens if I call the foo method like this from a thread? x =; Can I do that with two threads, without them waiting

  • Java - how post event to UI thread or call a function of UI thread? 2012-04-02

    I have a java code that looks like this: //UI thread //Some code Job j = new Job(jobName) { @Override public IStatus run(IProgressMonitor monitor) { try { //Some code SomeFunc(); //Some code return Status.OK_STATUS; } catch(Exception e) { } finally {

  • Is there a way to call a function in a parent class in java? 2011-09-10

    I'm making a base class for my androids screen, it is has a list control. Is there a way for when the base class's onListItemClick gets called, saying a list item was selected. It could call a function in the parent class?? Code: public class cHome e

  • call different child function from same parent function 2009-05-29

    Im looking to have a common parent function like so void main (param 1, param 2) { <stuff to do> param1(); print("The function %s was called", param 2); <more stuff to do> } Where param 1 will be the name of the function to be called and

  • Calling a function in class A from a thread in class B [C#] 2009-08-28

    Now with all the WaitOne and ManualResetEvent stuff working (thanks!) I've got one last problem, that is running a function in Class A from a thread which is part of Class B - again allow me to illustrate ... Look at the function "DoIt(obejct param)"

  • Calling a function in child thread in Qt? 2009-09-11

    I have a main thread that invokes a child thread function at different times but I am not sure whether that is right way of doing it in Qt.What is wrong with the below code and looking for better alternative There is a main thread running infinitly w

  • What happens to Java thread after a join call with timeout 2011-04-14

    What state is a Java thread in after you call join with a timeout value, and the timeout passes. So for instance you have the following code: Thread thread = new Thread(); thread.start(); thread.join(TIMEOUT); and the timeout passes and the thread ha

  • (Java Swing) Calling function in time intervals and displaying time to next call in label 2011-05-21

    I have function like this in Java Swing app. I need after click on button call many function in intervals and in cycles. Function must be called inside jbutton because I have some global parameters in View that are used in jbutton function. Can you p

  • OpenGL on Android: Any conflicts when calling OpenGL functions in both Java and C++? 2011-06-02

    In my application, I'm using a 3rd party custom view that calls OpenGL functions in Java, and I'm also calling OpenGL functions in my native C++ code. Should this be a problem? Is there any risk that they could be called at the same time? What is the

Copyright (C), All Rights Reserved.

processed in 0.089 (s). 11 q(s)