Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By kneitzel
  • 1 Post By jim829

Thread: What are interleaving operations? Can atomic actions interleave?

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

    Default What are interleaving operations? Can atomic actions interleave?

    I have read that when two threads have two steps (of execution) and each step involves many operations, then the operations of both the threads kind of overlap one-another. For example

    I think I am not very clear about interleaving actions/operations? Can somebody give some definition.

    Especially when we say that atomic actions can not be interleaved. But

    Java Code:
    class Counter{
        private int c=0;
    
        void increment () {
        c++;
        }
    
        void readIn () {
        System.out.println("Enter value: %n");
    
        BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));// I am not sure if this is right, I am just writing an example
        c= Integer.parseInt(inp.readLine()); // *********************Reading in integers in an atomic operation right?*************
        }
        
    }
    And the following sequence of events occurs.

    1. Thread A: Retrieve c
    2. Thread A: Increment c
    3. Thread A: Store the result in c
    4. Thread B: Ask the user to enter a value
    5. Thread B: Read in the value, store it in c



    Now the value of c obtained after increment will be lost (Thread Interference). Isn't this an example of interleaving operations?

    Thank you in advance.
    Last edited by Zarah; 03-24-2014 at 12:00 PM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: What are interleaving operations? Can atomic actions interleave?

    Your question is correct but your example does not really cover atomic operations. To just increment a simple variable:

    Java Code:
    c++;
    requires 3 separate operations. Read the value, update the value, and write the value. Because that process is not atomic, it may
    occur across multiple threads. With longs and double, two reads, update, and two writes are allowed. So things can get real
    interesting. To make those operations fully atomic, synchronize access or use one of the atomic classes.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: What are interleaving operations? Can atomic actions interleave?

    Hi,

    I just want to show a simple minimalistic example that shows the core problem:
    Java Code:
    public class ExampleThread implements Runnable {
    
        static final int NUM_THREADS = 5;
    
        static int counter = 0;
        public ExampleThread() {
        }
    
        public void run() {
            for (int i = 0; i < 1000; i++)
                counter++;
        }
    
        public static int getCounter() {
            return counter;
        }
    
        public static void main (String[] args) {
            // Create and start the threads.
            Thread[] threads = new Thread[NUM_THREADS];
            for (int i = 0; i < NUM_THREADS; i++) {
                threads[i] = new Thread(new ExampleThread());
                threads[i].start();
            }
    
            // Wait for all threads to end.
            for (int i = 0; i < NUM_THREADS; i++) {
                try {
                    threads[i].join();
                }
                catch (InterruptedException ex) {
                    System.out.println("Thread was interrupted ...");
                }
            }
    
            // Show the result.
            System.out.println("Count: " + counter);
        }
    }
    In this example multiple threads are created and each thread is increasing one variable over and over. When you run this application multiple times, you should get different results.
    This shows the core problem, because 2 Threads can both read the variable (Imagine it is 100), so both threads increase it by one. (101) and then store the result again. So instead of being increased 2 times it was only increased one time.

    So instead of a 5000 you get something like 4400.

    So the ++ operator is not atomic. So as a consequence you should make sure, that the counter is not changed at the same time by multiple threads. One solution could be a synchronized block around the counter++.

    With kind regards,

    Konrad
    Zarah likes this.

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

    Default Re: What are interleaving operations? Can atomic actions interleave?

    @Jim829, Thank you very much for the informative answer.

    @kneitzel, Thank you, it was an excellent example. It just clarified my confusion.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: What are interleaving operations? Can atomic actions interleave?

    In another of your threads I gave an example where one thread repeatedly performed 'x= x++;' effectively leaving the value of 'x' unchanged; another thread checks if 'x' still has that value; it hasn't ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: What are interleaving operations? Can atomic actions interleave?

    In another of your threads I gave an example where one thread repeatedly performed 'x= x++;' effectively leaving the value of 'x' unchanged; another thread checks if 'x' still has that value; it hasn't ...
    Yes I saw your example and it was simple enough that I should have understood the exact point without beating about the bush, but I couldn't. I don't know why the program ends.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: What are interleaving operations? Can atomic actions interleave?

    Quote Originally Posted by Zarah View Post
    Yes I saw your example and it was simple enough that I should have understood the exact point without beating about the bush, but I couldn't. I don't know why the program ends.
    That's because what Konrad says (see above): the ++ operation isn't atomic, nor is the assignment operation; the sequence x= x++ does this:

    1) increment that value of variable x
    2) return the previous value of x
    3) assign it again to variable x

    Somewhere after step 1) but before step 3) the other thread tests the value of x and it fails (of course).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: What are interleaving operations? Can atomic actions interleave?

    Somewhere after step 1) but before step 3) the other thread tests the value of x and it fails (of course).
    That's right. But that's the while loop of the main thread that fails right? The other thread t should still continue to execute, isn't it?

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: What are interleaving operations? Can atomic actions interleave?

    Yes, it will still be running because the JVM is still running. If you want to make it stop, turn it into a daemon thread.
    If only daemon threads are running, then the JVM will exit.

    Java Code:
    Thread tt = new Thread(t);
    tt.setDaemon(true);
    tt.start();
    Regards,
    Jim
    Zarah likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: What are interleaving operations? Can atomic actions interleave?

    @Jim829, Thank you, that just saved me from restarting eclipse each time and provided very useful information.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,994
    Rep Power
    5

    Default Re: What are interleaving operations? Can atomic actions interleave?

    Restart eclipse, why on earth would you ever do that? Eclipse has a button (with a little red square on it) to terminate the running program if it doesn't do that on its own.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: What are interleaving operations? Can atomic actions interleave?

    Oh :-P

Similar Threads

  1. Why actions in url?
    By SomeoneHere in forum Struts
    Replies: 5
    Last Post: 01-10-2012, 05:07 AM
  2. Monitors and Atomic Transactions
    By new person in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-26-2011, 12:41 PM
  3. Actions
    By What2do in forum New To Java
    Replies: 4
    Last Post: 01-31-2011, 04:45 PM
  4. How to Use Actions
    By AJArmstron@aol.com in forum New To Java
    Replies: 4
    Last Post: 04-18-2010, 09:03 PM
  5. Actions
    By alley in forum AWT / Swing
    Replies: 2
    Last Post: 01-16-2008, 02:52 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
  •