Results 1 to 4 of 4
  1. #1
    hqt
    hqt is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Question Explain the result of concurrent threads

    I have this piece of code and I print answer to see. But I don't know why the answer like that:
    Java Code:
    public class ThreadExample implements Runnable{
        
        public Thread thread;
        static int i = 0;
        ThreadExample(){
            thread = new Thread(this);
            thread.start();
        }
    
        public static void main(String[] args) {
            ThreadExample example = new ThreadExample();
            for(int n =0; n<1000; n++){
                System.out.println("main thread "+i);
                i++;
            }
        }
    
        public void run(){
            for(int index=0; index<1000; index++){
                System.out.println("Sub thread "+i);
                i++;
            }
        }
    }
    for some results is:
    Java Code:
    main thread 0
    Sub thread 0
    Sub thread 2
    Sub thread 3
    Sub thread 4
    Sub thread 5
    Sub thread 6
    Sub thread 7
    Sub thread 8
    Sub thread 9
    Sub thread 10
    Sub thread 11
    Sub thread 12
    main thread 1
    ...
    The thing I don't understand is: when sub thread take i goto 12. why main thread just print 1. (because they share variable i together)

    I don't know exactly what thread does together. Please explain for me this point.

    thanks for all :)
    Last edited by hqt; 02-04-2012 at 07:32 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: Explain the result of concurrent threads

    What did you expect to see?

  3. #3
    hqt
    hqt is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Explain the result of concurrent threads

    I think at line 14 of result, I think this line should be: main thread:12. because i has been changed to 12 at sub-Thread.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: Explain the result of concurrent threads

    You're thinking of the lines of code as atomic.
    They aren't.

    The call to println() in the main thread was clearly interrupted at some point inside the method, so that the other thread could run (12 times).
    Then the actual print() took place, with the text "main thread 1" already in place.

Similar Threads

  1. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 10:15 AM
  2. Replies: 1
    Last Post: 04-11-2011, 01:39 AM
  3. tutorials/ebooks that explain Threads
    By andyman99008 in forum Advanced Java
    Replies: 0
    Last Post: 12-20-2010, 07:59 PM
  4. Replies: 3
    Last Post: 12-13-2010, 07:22 AM
  5. FileLock and file access from concurrent threads..
    By fxRichard in forum Advanced Java
    Replies: 5
    Last Post: 01-02-2009, 08:08 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
  •