Results 1 to 8 of 8
  1. #1
    sachinj13 is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default passing a value from parent thread to child thread

    Hi All,

    I am calling a java program from a unix script .
    My oblectives are
    1)to pass value from scripts to main java class
    2)main class should create a child thread and pass data to child and should return control back to script
    3)child thread should run independtly of parent
    The calling unix script is part of process and hence should return control back to its calling script immediately.

    Findings
    1)Without passing data thru setter getter /constructor method to child thread my objectives are met
    2)When I pass the data from parent thread to child thread calling unix scripts wait till child thread finishesh its working

    call.scr
    java Main <list of Arguments>
    Main.java
    Java Code:
     public class Main
          {
                 public static void main(String args[]) throws Exception
              {
                 String data2="Z";
                 String data1=null;
                 for(int i=0;i<args.length;i++)
                 {
                      data2=data2+","+args[i];
                 }
                 data1=data2;
                 Child cu=new Child();
                 cu.setData(data1);
                 data2=null;
                 data1=null;
                 cu.start();
              }
          }
    Child.java
    Java Code:
          class Child extends Thread
          {
                  public String data;
                  void setData(String data)
                 {
                        this.data=data;
                 }
                  public void run()
                   {
                     ---------->             processing on data
                   }
    }


    I think due to passing of data from parent thread to child thread (Inter thread data communication/Inter process communication)
    the threads are not working as desired.

    Plz anybody can suggest something.....

    Thanx.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default

    the threads are not working as desired.
    To debug your problem you need to add some println() statements to see where the threads are going and what the values are. When you see what is happening, that should help you to change the code so you get the desired results.

  3. #3
    sachinj13 is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default

    Hi Norm,

    this is filtered code i have posted,
    but when i hv actually deguued it the threads r getting generated succesfully n getting killed in manner of their life cycle.
    But the cotrol is not getting back to script till child get killed .
    Plz help

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default

    cotrol is not getting back to script till child get killed .
    The jvm will keep control until all the non-daemon threads have ended.
    Are you asking how to start a java program from a script and then have the java program return control to the script and still be running? Sounds like an OS interface problem. Can your script start the java program as another process (or whatever) and continue on, leaving the java program running?
    Perhaps you could change your design to put all the logic in the java program so there isn't a script/java interface problem.

  5. #5
    sachinj13 is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The jvm will keep control until all the non-daemon threads have ended.
    Hi Norm,

    the unwanted results are just because off passing data from parent to child. All other things are working fine.
    I am able to call the java prog from the script .But as i desired after parent passes d data and child get Started,control immediately shuld get back to script ,but in case of ppasing data its nt happening
    But when I donot pass d data but for testing use hardcoded data in child then all things are getting done as i desired.

    Thanx

  6. #6
    sachinj13 is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by sachinj13 View Post
    Hi Norm,

    the unwanted results are just because off passing data from parent to child. All other things are working fine.

    Thanx
    Or is it the case that for calling script whole jvm will be a process and not the underlying threads.And hence its behaving in this manner.
    But still in case of hardcoded data in child how its working fine is still matter of concern for me.
    Thanx

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default

    whole jvm will be a process
    I beleive that is true.

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post process boundaries

    Quote Originally Posted by Norm View Post
    I beleive that is true.
    In general, should be true. Subject to testing, variables do not cross compilation unit boundaires. To say that something is file scope is to say that you can see it in an editor window, without tabbing to another editor window: That is the definition of a compilation unit.

    The JVM is actually the process, as the system sees it. public static void main is a thread within the JVM, which may launch a few threads of it's own - eg console and screen and maybe a monitor to wait on your main. An interesting safety issue is raised here: new Thread().setDaemon; in main and starting it when the jvm was started from a script, is likely to introduce interesting study cases ~ cases that should be studied in rareified stratospherics of thesis subjects and not be done on commercial work.

    The jvm likely runs in the process space of the script, carefull attention to code correctness would reveal any intra-thread risks that are not subject to Security Manager issues.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Thread
    By sims in forum New To Java
    Replies: 4
    Last Post: 08-25-2008, 01:59 PM
  2. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  3. Replies: 0
    Last Post: 01-28-2008, 07:02 AM
  4. using Thread
    By one198 in forum New To Java
    Replies: 1
    Last Post: 11-21-2007, 08:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •