Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How can understand this example of threads?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can understand this example of threads?

    Consider we have something like this:

    public class Test {
     
        public static void main(String[] args) throws InterruptedException {
     
            Example b = new Example();
            b.start();
        }
    }
     
     
    class Example extends Thread {
        int total,count;
     
        @Override
        public void run(){
                for(int i =0;i<5;i++){
                    total+=i;
                    System.out.println(i+" "+total);
                }
     
        } 
    }

    The result is:

    0 0
    1 1
    2 3
    3 6
    4 10

    How to understand these results? Iteration is only 4, but class field 'total' is 10. How?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,495
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: How can understand this example of threads?

    That for loop would do the same thing by itself in a main() method. The first value printed out is the for loop control variable, i, the second variable is the sum of all for loop control variable values for that pass through the loop, ending with 0 + 1 + 2 + 3 + 4 = 10 on the 5th pass.

    Hope that helps.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can understand this example of threads?

    Oh, I see. I thought that was about threads...but it seems it is simple loop...and I thought that it is more complex and related with threads behaviour

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can understand this example of threads?

    Hi,

    Thread is very simple concept.

    You can learn thread form here ...
    and difference between two threads is here

    and difference between two thread is ...
    Last edited by copeg; February 7th, 2014 at 08:25 AM. Reason: Removed links

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,495
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: How can understand this example of threads?

    @manik90: A thread with no activity for a week - certainly two - is dead. I don't believe solvable has been back since then, but that could be checked. Please check the date of the last post in a thread, and don't resurrect dead threads.

    Closing this one, sending it back to the grave.

    Oh, and Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

Similar Threads

  1. regarding threads
    By chinnu&manu in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 6th, 2013, 06:56 AM
  2. Threads
    By RABNAWAZ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 17th, 2013, 12:56 PM
  3. threads
    By peter_parker in forum Java Theory & Questions
    Replies: 3
    Last Post: April 1st, 2013, 01:49 PM
  4. Please help to Understand x=++x+ + +x+x++
    By rayan2004 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 2nd, 2012, 06:47 PM
  5. threads
    By crazed8s in forum Threads
    Replies: 2
    Last Post: December 14th, 2010, 04:33 AM

Tags for this Thread