Question

In: Computer Science

When multiple thread access the setLower and setUpper methods of the NumberRange object, the isValid method...

When multiple thread access the setLower and setUpper methods of the NumberRange object, the isValid method return false. Explain why?

public class NumberRange {

private final AtomicInteger lower = new AtomicInteger(0);

private final AtomicInteger upper = new AtomicInteger(0);

  public void setLower(int i) {

if(i>lower upper.get()) throw new IllegalArgumentException();

lower.set(i);

}

public void setUpper(int i) {

if (i < lower.get()) throw new IllegalArgumentException();

upper.set(i);

}

public boolean isValid() {

return (lower.get() <= upper.get());

}

}

Solutions

Expert Solution

The setLower method compares the passed value with the upper value, and sets only if given value is less than upper.. Similarly, the setUpper method sets the passed value to upper, only if passed value is more than the lower value..

Now when multiple thread concurrently execute setLower and setUpper methods below situation may occur:

Assume start values of lower and upper as:
Lower => 100
Upper => 1000

Lets say thread T1 tries to call setLower(500)
and thread T2 tries to call setUpper(200) almost at the same time.

Suppose execution happens like below:
T1 checks if(i>upper.get()), which comes to false, since 500 is lesser than 1000. Hence it do not throws the exception.
But then execution goes to T2.. T2 checks if (i < lower.get()), and finds that 200 is more than current lower 100, Hence it also do not throw any exception. It then sets value of upper to 200..
So now, Lower=>100, Upper=>200
Now, Execution goes back to T1, which has already done the error checking and now just sets the lower value to 500..
So, Now: Lower=>500, Upper=>200

Now, if we call isValid function, it will return false, since lower value is more than upper value

**************************************************


Please consider providing a thumbs up to this question.


Related Solutions

1) Provide an overview of the multiple methods to access health insurance today. 2) Provide a...
1) Provide an overview of the multiple methods to access health insurance today. 2) Provide a scenario of what options you would explore in the event that you became ineligible to access your current health insurance policy
Write a method that returns the result when the calling object is multiplied by a scalar...
Write a method that returns the result when the calling object is multiplied by a scalar value. For example, the PolyTerm 2.4x^3 multiplied by -1.2 should return the PolyTerm object representing -2.88x^3. Language: Java. Method name be like: scalarMultiply(double) Some Outputs: Test 1: Coefficient =1, Exponent = 1 scalarMultiply(1.2).coefficient return 1.2; scalarMultiply(1.2).exponent returns 1. Test 2: Coefficient =2.4, Exponent = 3 scalarMultiply(-1.2).coefficient returns -2.88 scalarMultiply(-1.2).exponent return 3 Test 3: Coefficient =-1.5 Exponent = 0 scalarMultiply(0).coefficient returns 0 scalarMultiply(0).exponent returns 3...
There are multiple stock valuation methods. The price-earnings (PE) method is one of the most popular....
There are multiple stock valuation methods. The price-earnings (PE) method is one of the most popular. The PE method is provided by the Wall Street Journal in all of its stock quotations. The PE logic is that future earnings are important determinants of a firm’s value. The PE method has several variations which can result in different valuations. Discuss the assumptions underlying the calculation of the PE ratio. What are its shortcomings?
1. A constructor is a special Class member method. It is automatically called when an object...
1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and abstraction are both the pillars of Object-Oriented...
Access the main shortcomings of the payback method.
Access the main shortcomings of the payback method.
Please discuss the two most common traditional valuation methods: Earnings Multiple, and Discounted Cash Flow Method....
Please discuss the two most common traditional valuation methods: Earnings Multiple, and Discounted Cash Flow Method. How can they be applied to various actual situations? How effective are these methods?
In a memory system, when the access time of the cache is 10ns and the access...
In a memory system, when the access time of the cache is 10ns and the access time of the main memory is 50ns, what is the hit ratio of the cache if the effective access time is 10% larger than the access time of the cache? (Up to 4 digits below the decimal point) please detail explanation
What advantages does object-oriented exception handling provide? When a program contains multiple catch blocks, how are...
What advantages does object-oriented exception handling provide? When a program contains multiple catch blocks, how are they handled? Write the statement to declare a three-by-four array of integers with the elements initialized to zero. Name the array myArray. What is the difference between volatile and nonvolatile storage in Java programming? Give examples of different storage situations. What are some of the advantages of the ArrayList class over the Arrays class? How can you use the enhanced for loop? What does...
in ACL (access control List) : 1.How do you determine who can access object x? 2....
in ACL (access control List) : 1.How do you determine who can access object x? 2. How do you determine which objects user y can access? 3. What happens if a file is deleted?
pick from the multiple choice Under the full goodwill method, a control premium is recognised when:...
pick from the multiple choice Under the full goodwill method, a control premium is recognised when: a. the parent paid more than the fair value for the shares they acquired. b. the parent paid less than the fair value for the shares they acquired. c. the consideration transferred by the parent is more than the fair value of the identifiable net assets acquired. d. the consideration transferred by the parent is less than the fair value of the identifiable net...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT