IUnknown
IUnknown

Reputation: 9827

Counting in a thread

Why would the program not print 9 instead of 0?
AtomicInteger did not help either (as advised)

 public class threadOne{
    //Integer i=0;
    AtomicInteger i=new AtomicInteger(0);

    class firstThread implements Runnable{
        AtomicInteger i;
        firstThread(AtomicInteger i){
            this.i=i;
        }
        public void run(){
            while(i.intValue()<10){
                i.incrementAndGet();
            }
        }       
    }

    void runThread(){ 
        Thread t = new Thread(new firstThread(i));
        t.start();
        System.out.println("Result : " + i.intValue());
    }

    public static void main(String[] args){
        new threadOne().runThread();

    }

}

Upvotes: 1

Views: 66

Answers (4)

dumbPotato21
dumbPotato21

Reputation: 5695

Remember : You're printing i of threadOne, and not firstThread

The Integer i in both the clases are independent of each other. Changes in one don't reflect on the other one. Integer is a immutable class.

For example,

Integer i = 4;
Integer j = i;
j = 1; //same as j = new Integer(1);
System.out.println(i);

It prints 4 and not 1. The same way, i+=1 in firstThread does not affect the i in threadOne, which is the one you're printing.

You can use a mutable class, like AtomicInteger, which does what you expect, or simply print i of firstThread

Edit: You need to wait for the execution of the Thread to complete to see the changes. Do

Thread t = new Thread(new firstThread(i));
t.start();
try{
    t.join();
}catch(Exception e){}
System.out.println("Result : " + i.get());

Upvotes: 1

Yury Chudnovsky
Yury Chudnovsky

Reputation: 26

Integer class is Immutable. It means that you cannot pass the value from outside and change it inside the method without returning new value!

Upvotes: 0

Sweeper
Sweeper

Reputation: 272525

This is because you declared two variables called i.

public class threadOne{
    Integer i=0; // <--- first one here
    class firstThread implements Runnable{
        Integer i; // <--- second one here
        firstThread(Integer i){
            this.i=i; // <--- here you are accessing the second one
        }
        public void run(){
            while(i<10){
                i+=1; // <--- here you are accessing the second one
            }
        }       
    }
    void runThread(){ 


        Thread t = new Thread(new firstThread(i)); // <--- here you are accessing the first one
        t.start();
        System.out.println("Result : " + i); // <--- here you are accessing the first one

    }
    public static void main(String[] args){
        new threadOne().runThread();    
    }

}

Basically what happened is that you are printing out the first i, while what the thread is changing is the second i.

To fix this, simply print the second i by doing this:

firstThread first = new firstThread(i); // we need a firstThread variable because we need to access the second i later
Thread t = new Thread(first);
t.start();
System.out.println("Result : " + first.i);

Upvotes: 0

Manish Kumar Sharma
Manish Kumar Sharma

Reputation: 13442

Because the statement,

System.out.println("Result : " + i);

refers to the field i in the threadOne class and that's what you are printing instead of the incremented i in firstThread class. Insert this statement inside the firstThread class to get 9.

So do something like,

    public void run(){
        while(i<10){
            i+=1;
        }
        // INSERT HERE
        System.out.println("Result : " + i);
    }   

Upvotes: 0

Related Questions