Question:

What will be the output of the program?

public class SyncTest 
{
    public static void main (String [] args) 
    {
        Thread t = new Thread() 
        {
            Foo f = new Foo();
            public void run() 
            {
                f.increase(20);
            }
        };
    t.start();
    }
}
class Foo 
{
    private int data = 23;
    public void increase(int amt) 
    {
        int x = data;
        data = x + amt;
    }
}

and assuming that data must be protected from corruption, what—if anything—can you add to the preceding code to ensure the integrity of data?

Answers:


Keywords:

© 2017 QuizBucket.org