Synchronization with threads

Discussion in 'Java' started by javateamPL@gmail.com, Feb 8, 2008.

  1. Guest

    Hello,

    I have a class which is not a thread which executes the following
    code:
    public void myFunc() {
    MyMainThread thread = new MyMainThread();
    thread.start();
    ....
    exec(arrayListFromThread);
    ....
    }

    A thread creates many subthreads which role is to fill in the
    ArrayList (defined for thread). Next, I would like to pass this
    arrayList to myFunc in the moment when all subthreads have finished
    its job (so that I am sure that arraylist is complete) and immediately
    start exec method.

    I have two problems:
    - how to pass this arrayList from thread (can I simply use method from
    thread to return it? will the thread be still active?)
    - how can I suspend an execution of myFunc() till the mainThread
    finish creating arrayList?

    Would be very grateful for effective solutions of these problems.

    Regards, Mark
     
    , Feb 8, 2008
    #1
    1. Advertising

  2. Janusch Guest

    On 8 Feb., 12:43, wrote:
    > Hello,
    >
    > I have a class which is not a thread


    ======================================
    1. If you use a main method, you use the main thread.
    In this case it is ok to wait for your MyMainThread.

    public void myFunc() {
    MyMainThread thread = new MyMainThread();
    thread.start();
    ....
    while (thread.isAlive()) try{ Thraed.sleep(100); } catch(Exception err)
    {}
    exec(thread.getArrayListFromThread());
    ....

    }

    class MyMainThread extends Thread{
    private ArayList arrayListFromThread;

    public void run(){
    //start all Subthreads
    while ( /*one of the subtreads runs */) {
    try{ Thraed.sleep(100); } catch(Exception err){}
    }
    }

    public ArrayList getArrayListFromThread(){return arrayListFromThread;}
    }
    ======================================
    2. If you use a Swing UI, you use the Swing-Thread.
    In this case is it better to invoke exec(arrayListFromThread) in
    MyMainThread.

    public void myFunc() {
    MyMainThread thread = new MyMainThread();
    thread.start();
    }

    class MyMainThread extends Thread{
    private ArayList arrayListFromThread = new ArrayList();

    public void run(){
    //start all Subthreads
    while ( /*one of the subthreads runs */) {
    try{ Thraed.sleep(100); } catch(Exception err){}
    }
    exec(arrayListFromThread);
    ....
    }
    }

    ======================================
    All Subthreads have to synchronize all access to ArayList!

    I have two solution for starting all Subthreads and waiting:
    solution 1:

    Thread[] t=new Thread[200];

    public void run(){
    for (int i...){
    t=new Thread();
    t.start();
    }
    while ( oneSubThreadIsAlive()) {
    ry{ Thraed.sleep(100); } catch(Exception err){}
    }
    ....
    }

    public boolean oneSubThreadIsAlive(){
    for (int i...) if (t.isAlive()) retrun true;
    return false;
    }


    solution 2:

    Thread[] t=new Thread[200];
    int runs=0;
    Object mon=new Object();

    public void run(){
    for (int i...){
    t=new SubThread();
    t.start();
    synchronized(mon){runs++;}
    }
    while ( runs>0) {
    try{ Thraed.sleep(100); } catch(Exception err){}
    }
    ....
    }

    class SubThread extends Thread{
    public void run(){
    try{
    ...
    // fill ArayList used synchronized-block
    ...
    // ready:
    }finally{ synchronized(mon){runs--;} }
    }
    }

    ================================
    Filling a ArayList used synchronized-block:
    synchronized(mon){arrayListFromThread.add("something");}

    Janush
     
    Janusch, Feb 8, 2008
    #2
    1. Advertising

  3. Daniel Pitts Guest

    wrote:
    > Hello,
    >
    > I have a class which is not a thread which executes the following
    > code:
    > public void myFunc() {
    > MyMainThread thread = new MyMainThread();
    > thread.start();
    > ....
    > exec(arrayListFromThread);
    > ....
    > }
    >
    > A thread creates many subthreads which role is to fill in the
    > ArrayList (defined for thread). Next, I would like to pass this
    > arrayList to myFunc in the moment when all subthreads have finished
    > its job (so that I am sure that arraylist is complete) and immediately
    > start exec method.
    >
    > I have two problems:
    > - how to pass this arrayList from thread (can I simply use method from
    > thread to return it? will the thread be still active?)
    > - how can I suspend an execution of myFunc() till the mainThread
    > finish creating arrayList?
    >
    > Would be very grateful for effective solutions of these problems.
    >
    > Regards, Mark

    this sounds like a bad idea, unless you make sure your using proper
    synchronization...

    I suggest looking into the standard ExecutorService and
    CompletionService classes.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Feb 8, 2008
    #3
  4. In article <47ac73d4$0$14886$>,
    Daniel Pitts <> wrote:

    > wrote:
    > > Hello,
    > >
    > > I have a class which is not a thread which executes the following
    > > code:
    > > public void myFunc() {
    > > MyMainThread thread = new MyMainThread();
    > > thread.start();
    > > ....
    > > exec(arrayListFromThread);
    > > ....
    > > }
    > >
    > > A thread creates many subthreads which role is to fill in the
    > > ArrayList (defined for thread). Next, I would like to pass this
    > > arrayList to myFunc in the moment when all subthreads have finished
    > > its job (so that I am sure that arraylist is complete) and immediately
    > > start exec method.
    > >
    > > I have two problems:
    > > - how to pass this arrayList from thread (can I simply use method from
    > > thread to return it? will the thread be still active?)
    > > - how can I suspend an execution of myFunc() till the mainThread
    > > finish creating arrayList?
    > >
    > > Would be very grateful for effective solutions of these problems.
    > >
    > > Regards, Mark

    > this sounds like a bad idea, unless you make sure your using proper
    > synchronization...
    >
    > I suggest looking into the standard ExecutorService and
    > CompletionService classes.


    There's nothing wrong with that. The ArrayList can be accessed if it's
    final and the Thread/Runnable is an anonymous inner class. It can also
    be accessed if the array is a field of the class. It can be passed in
    the constructor for a Thread subclass. Many options.

    Each thread will have to synchronize on a common object, probably the
    ArrayList itself, while working on it. Call join() on each Thread to
    block until it completes.

    --
    I don't read Google's spam. Reply with another service.
     
    Kevin McMurtrie, Feb 8, 2008
    #4
  5. Daniel Pitts Guest

    Kevin McMurtrie wrote:
    > In article <47ac73d4$0$14886$>,
    > Daniel Pitts <> wrote:
    >
    >> wrote:
    >>> Hello,
    >>>
    >>> I have a class which is not a thread which executes the following
    >>> code:
    >>> public void myFunc() {
    >>> MyMainThread thread = new MyMainThread();
    >>> thread.start();
    >>> ....
    >>> exec(arrayListFromThread);
    >>> ....
    >>> }
    >>>
    >>> A thread creates many subthreads which role is to fill in the
    >>> ArrayList (defined for thread). Next, I would like to pass this
    >>> arrayList to myFunc in the moment when all subthreads have finished
    >>> its job (so that I am sure that arraylist is complete) and immediately
    >>> start exec method.
    >>>
    >>> I have two problems:
    >>> - how to pass this arrayList from thread (can I simply use method from
    >>> thread to return it? will the thread be still active?)
    >>> - how can I suspend an execution of myFunc() till the mainThread
    >>> finish creating arrayList?
    >>>
    >>> Would be very grateful for effective solutions of these problems.
    >>>
    >>> Regards, Mark

    >> this sounds like a bad idea, unless you make sure your using proper
    >> synchronization...
    >>
    >> I suggest looking into the standard ExecutorService and
    >> CompletionService classes.

    >
    > There's nothing wrong with that. The ArrayList can be accessed if it's
    > final and the Thread/Runnable is an anonymous inner class. It can also
    > be accessed if the array is a field of the class. It can be passed in
    > the constructor for a Thread subclass. Many options.

    I didn't say the array wasn't accessible.
    >
    > Each thread will have to synchronize on a common object, probably the
    > ArrayList itself, while working on it. Call join() on each Thread to
    > block until it completes.


    That would be the way to do it with his current approach, but I was
    suggesting he look into a much cleaner, more efficient, and
    easier-to-get-correct solution.


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Feb 8, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yoda
    Replies:
    2
    Views:
    477
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  2. Replies:
    7
    Views:
    411
  3. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    446
    William Ahern
    Aug 27, 2004
  4. Replies:
    21
    Views:
    750
    Lasse Reichstein Nielsen
    Jun 27, 2008
  5. Zd Yu
    Replies:
    7
    Views:
    139
    Zd Yu
    May 16, 2011
Loading...

Share This Page