Results 1 to 9 of 9
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Synchronization, Thread.join(), Memory Consistency Errors, and Happens-before.

    In this tutorial on Memory Consistency Errors in Concurrency and Synchronization, a case of the creation of a "happens-before relationship" has been given as:

    "When a thread terminates and causes a Thread.join in another thread to return, then all the statements executed by the terminated thread have a happens-before relationship with all the statements following the successful join. The effects of the code in the thread are now visible to the thread that performed the join."

    They say in the tutorial that an example of this has already been given. The example already given is the code given below.

    My question is that

    "When a thread terminates and causes a Thread.join in another thread to return..."

    When Thread.join() is called, the thread from which it is called halts temporarily (main thread in the example given), waiting for another thread (MessageLoop in the example) to finish execution.

    I don't understand this phrase in the statement: "causes a Thread.join in another thread to return".

    1. The thread terminating here (assuming that "terminate" here means suspending temporarily by calling Thread.join() on itself) is the main thread. How does it cause a Thread.join() in another thread (the other thread is MessageLoop)?

    2. "to return" - What to return? Who is to return? I don't get it.

    Java Code:
    public class SimpleThreads {
    
        // Display a message, preceded by
        // the name of the current thread
        static void threadMessage(String message) {
            String threadName =
                Thread.currentThread().getName();
            System.out.format("%s: %s%n",
                              threadName,
                              message);
        }
    
        private static class MessageLoop
            implements Runnable {
            public void run() {
                String importantInfo[] = {
                    "Mares eat oats",
                    "Does eat oats",
                    "Little lambs eat ivy",
                    "A kid will eat ivy too"
                };
                try {
                    for (int i = 0;
                         i < importantInfo.length;
                         i++) {
                        // Pause for 4 seconds
                        Thread.sleep(4000);
                        // Print a message
                        threadMessage(importantInfo[i]);
                    }
                } catch (InterruptedException e) {
                    threadMessage("I wasn't done!");
                }
            }
        }
    
        public static void main(String args[])
            throws InterruptedException {
    
            // Delay, in milliseconds before
            // we interrupt MessageLoop
            // thread (default one hour).
            long patience = 1000 * 60 * 60;
    
            // If command line argument
            // present, gives patience
            // in seconds.
            if (args.length > 0) {
                try {
                    patience = Long.parseLong(args[0]) * 1000;
                } catch (NumberFormatException e) {
                    System.err.println("Argument must be an integer.");
                    System.exit(1);
                }
            }
    
            threadMessage("Starting MessageLoop thread");
            long startTime = System.currentTimeMillis();
            Thread t = new Thread(new MessageLoop());
            t.start();
    
            threadMessage("Waiting for MessageLoop thread to finish");
            // loop until MessageLoop
            // thread exits
            while (t.isAlive()) {
                threadMessage("Still waiting...");
                // Wait maximum of 1 second
                // for MessageLoop thread
                // to finish.
                t.join(1000);
                if (((System.currentTimeMillis() - startTime) > patience)
                      && t.isAlive()) {
                    threadMessage("Tired of waiting!");
                    t.interrupt();
                    // Shouldn't be long now
                    // -- wait indefinitely
                    t.join();
                }
            }
            threadMessage("Finally!");
        }
    }
    If somebody can help me with this (probably silly) question of mine, I'll be grateful. Thank you.

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

    Default Re: Synchronization, Thread.join(), Memory Consistency Errors, and Happens-before.

    "causes a Thread.join in another thread to return".
    I would assume that the join() method has been called in another thread and is blocked from returning until the joined thread ends. When the joined thread ends then the call to the join() method will return to continue execution at the next statement after the call to join().
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Synchronization, Thread.join(), Memory Consistency Errors, and Happens-before.

    I would assume that the join() method has been called in another thread and is blocked from returning until the joined thread ends. When the joined thread ends then the call to the join() method will return to continue execution at the next statement after the call to join().
    I think I am not very clear about the Thread.join() itself. I'll like to decompose your answer into the following and try to understand it. Please let me know if I am getting it right.

    1. Let's say our main thread is named as A. Another thread named B has been started from A.
    2. Thread.join() has been called in B.
    3. I am assuming the working of the Thread.join() as:
    • Thread.join() is called
    • Some code in the implementation of join() causes the suspension of the thread from which join() was called, until another thread ends.
    • As the other thread ends, join() returns to the thread which called it. As join() returns, the thread which called join() continues execution from the next statement.

    4. So B is suspended until A finishes.
    5. As A finishes, Thread.join() returns to B and the execution of B continues from the next statement.

    Although I think I am very wrong, please let me know because this is what I could make out of it.

    And is a "joined thread" the one from which join() has been called or the other one?
    Thank you.
    Last edited by Zarah; 03-22-2014 at 12:57 PM.

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

    Default Re: Synchronization, Thread.join(), Memory Consistency Errors, and Happens-before.

    That sounds right.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Synchronization, Thread.join(), Memory Consistency Errors, and Happens-before.

    That sounds right.
    @Norm Thank you for your answer then, and did you call the thread calling the join() as the joined thread? or the other one?

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

    Default Re: Synchronization, Thread.join(), Memory Consistency Errors, and Happens-before.

    I guess the joined thread could be the thread that was joined. The thread that called the join() method would be the joining thread.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    ocean39 is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: Synchronization, Thread.join(), Memory Consistency Errors, and Happens-before.

    its simple the join method will tell all the running threads to wait until the thread with join method has not completed the execution.
    for the simple example and out put you can check The join method in Java Threads | Trust In Geeks

  8. #8
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Synchronization, Thread.join(), Memory Consistency Errors, and Happens-before.

    Quote Originally Posted by ocean39 View Post
    its simple the join method will tell all the running threads to wait until the thread with join method has not completed the execution.
    for the simple example and out put you can check The join method in Java Threads | Trust In Geeks
    What is Multi in that program?

    The program will execute only after removing the errors :s

  9. #9
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Synchronization, Thread.join(), Memory Consistency Errors, and Happens-before.

    @Norm Thank you!

Similar Threads

  1. thread synchronization
    By ashish12169 in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-22-2012, 11:17 PM
  2. Thread and Synchronization
    By yolo in forum New To Java
    Replies: 1
    Last Post: 04-25-2012, 03:21 AM
  3. Thread synchronization
    By rajanis in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-18-2011, 12:30 PM
  4. Thread synchronization
    By rajanis in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-07-2011, 07:38 AM
  5. question about so-called "memory consistency errors"
    By gib65 in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-20-2010, 04:20 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
  •