We have a class Storage, which can contain products. This class has a parameter productCount and a method addProduct() that increments the product count:
class Storage {
var productCount = 0
fun addProduct() {
productCount++
}
}
The program reads the number of threads that will add products to Storage. After that, we read the number of products that each thread adds to Storage – each thread has its own number of products to be added to our storage, and each number should be on a separate line.
Synchronize the addProduct() method so that the number of products in Storage will be correct.
You need to change only the Storage class, don't add anything else!
Explanation of the example:
In the first line, we have a value of 4, which is the number of threads.
In the next lines, we have numbers 1, 2, 3, and 4. So, the first thread adds one product to Storage, the second thread adds two products, and so on.