Results 1 to 11 of 11
  1. #1
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default Thread, determine process status

    Hello All

    I have 3 Threads, they do their tasks ..how to determine all of tasks have been done completely?

    Java Code:
    Runnable r1 = new Runnable() {
            public void run() {
                try {
    		...
    		...
    		// some process 1
    		//						
                } catch (SQLException ex) {}
            }
        };
    
        Runnable r2 = new Runnable() {
            public void run() {
                try {
    		...
    		...
    		// some process 2
    		//
                }
                catch (SQLException ex) {}
            }
        };
        
        Runnable r3 = new Runnable() {
            public void run() {
                try {
    		...
    		...
    		// some process 3
    		//
                }
                catch (SQLException ex) {}
            }
        };
    
    
    
        private void doAllProcess() {
            Thread t1 = new Thread(r1);
            Thread t2 = new Thread(r2);
            Thread t3 = new Thread(r3);
            t1.start();
            t2.start();
            t3.start();
        }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I would add a variable in each r1, r2, and r3, something like isDone, which is intially false. In each thread, when the process is finished, you set isDone to true. Then you can simply read t1.isDone, t2.isDone, and t3.isDone.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Or you could allow the Runnables notify listeners of their states by giving them PropertyChangeSupport fields.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    All threads have to increment a counter when they are finished (in a synchronized block); the 'master thread' waits (synchronized on the same lock) until the counter == 3. The other threads of course notify() the waiting thread after they have incremented the counter.

    kind regards,

    Jos

  5. #5
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    so, I thought I have to create custom event that will monitor t1,t2,t3 .. if all of them finished then the event should fired automatically?

    am I correct? .. or is there any simpler ones?
    Last edited by newbiejava; 08-22-2010 at 04:01 AM.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by newbiejava View Post
    so, I thought I have to create custom event that will monitor t1,t2,t3 .. if all of them finished then the event should fired automatically?

    am I correct? .. or is there any simpler ones?
    Nothing happens automatically; did you read my reply #4? Your 'master' thread should have some code like this:

    Java Code:
    synchronized(lock) {
       while (counter != 3) lock.wait();
    }
    and your other threads should do this when they have finished doing their job:

    Java Code:
    synchronized(lock) {
       count++;
       lock.notifyAll();
    }
    kind regards,

    Jos

  7. #7
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    Hi JosAh..
    do you have simple example for this? .. I am learning it now..just for clue..
    my codes above does not have master thread?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    The doAllProcess method could be called the master.
    Add the pieces of code JosAH gave you in the worker threads and after the starts in the doAllProcess method. Add a lock object and a counter and try it.

  9. #9
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    Thanks, now my doAllProcess method and threads are as follow:

    is there any way to just monitor the counter .. until the counter = 3 then it automatically fired .. (like an event)..?

    please help

    TIA


    Java Code:
    final Object lock = new Object();
    int counter = 0;
    
    Runnable r1 = new Runnable() {
            public void run() {
                try {
    		...
    		...
    		// some process 1
    		//						
                } catch (SQLException ex) {}
                    synchronized(lock) {
                       counter++;
                       lock.notifyAll();
                    }
            }
        };
    
        Runnable r2 = new Runnable() {
            public void run() {
                try {
    		...
    		...
    		// some process 2
    		//
                }
                catch (SQLException ex) {}
                    synchronized(lock) {
                       counter++;
                       lock.notifyAll();
                    }
            }
        };
        
        Runnable r3 = new Runnable() {
            public void run() {
                try {
    		...
    		...
    		// some process 3
    		//
                }
                catch (SQLException ex) {}
                    synchronized(lock) {
                       counter++;
                       lock.notifyAll();
                    }
            }
        };
    
    
    
        synchronized(lock) {
                try {
                    t1 = new Thread(r1);
                    t2 = new Thread(r2);
                    t3 = new Thread(r3);
                    t1.start();
                    t2.start();
                    t3.start();
                    while (counter != 3) {
                        lock.wait();
                    }
                    cls1.msgBox(this, "here counter = 3", Class1.MESSAGE_INFO);
                    counter = 0;
                } catch (InterruptedException ex) {}
            }
        }

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    Look at the java.util.concurrent package. There are several classes that might do what you want: CountDownLatch or Semaphore for example.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by newbiejava View Post
    Thanks, now my doAllProcess method and threads are as follow:

    is there any way to just monitor the counter .. until the counter = 3 then it automatically fired .. (like an event)..?
    But your code is 'just monitoring the counter'. What more do you want? If your code has passed that while loop it is free to do what it wants, i.e. all the other Threads have finished. IOW feel free to fire events there if anything else is interested ...

    kind regards,

    Jos

Similar Threads

  1. How to determine CPU usage using Java?????
    By JavaEmpires in forum New To Java
    Replies: 5
    Last Post: 03-16-2011, 05:49 AM
  2. How can I determine the type of an input
    By QJack in forum New To Java
    Replies: 4
    Last Post: 03-18-2010, 11:22 AM
  3. Which thread do the process first if we use multithreading ?
    By cinjojose in forum Threads and Synchronization
    Replies: 4
    Last Post: 10-12-2009, 12:23 PM
  4. how to determine the primary key
    By osval in forum JDBC
    Replies: 1
    Last Post: 08-07-2007, 03:31 AM
  5. Replies: 1
    Last Post: 05-20-2007, 12:19 PM

Posting Permissions

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