Question

In: Computer Science

How do you add all the values that return by all the threads from threadpools? I...

How do you add all the values that return by all the threads from threadpools?
I have created a threadpool with 1000 of threads and run the task 1000 times. However, when I try to print out the result it gives a whole bunch of values that return by all the threads, it looks like...
Thread-1 xxxx
Thread-2 xxxx
Thread-3 xxxx
Thread-4 xxxx
.
.
.
.
.

Is there a possible way to get a single value back instead of whole bunch of value? Taking the average value of all the threads are fine as well.

This question is based on Java only. Thank you!!

Solutions

Expert Solution

I believe what you are looking for is "atomic Object" to hold values from all the Threads in a single variable.

In Java for concurrent execution we have java.util.concurrent.atomic package

a reference to the same can be find here

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html

Example: you have 1000 threads and each thread is doing "task" 1000 times. You want to keep a counter of how many times task was executed in total.

Solution : Each time the task is executed by any thread we will increment atomicInteger variable.

/*****Sample Code********/

AtomicInteger counter = new AtomicInteger(0); //Declaring the atomic integer variable

counter.incrementAndGet() //do this each time task is executed.

/*************Sample Code********/

Moreover you can create your atomic variable static so that it is shared between all the object of the class and there remains only a single instance of atomic variable.

In case you need more help on this, Comment :) Happy to help.


Related Solutions

HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT BY THE USER AS SPECIFIED IN THEH FIRST PART OF THE QUESTION? Ask the user to enter a number and display the number, followed by ***, followed by the number squared, followed by &&&, and followed by the number cubed. Allow the user to enter as many numbers as he/she wishes. So, use a reasonable sentinel value to end the loop (for example, -999)....
C Program: (Knapsack Exhaustive): How do I add the 5 random generated values into the val...
C Program: (Knapsack Exhaustive): How do I add the 5 random generated values into the val [] arrays and how to add the total maximum weights ?    // returns maximum of two integers    int max(int a, int b) { return (a > b)? a : b; }       // Returns the maximum value that can be put in a knapsack of capacity W    int knapSackExhaustive(int W, int wt[], int val[], int n){    if (n ==...
How can I return in a list all possible paths from a grid if I can...
How can I return in a list all possible paths from a grid if I can only go right and down? For example consider the following table: A B C D E F If I go right I need to insert in my list 'H' if I go down I need to insert in my list 'V'. For example the path A - > B -> C -> F would be H - H - V The path A ->...
How do I covert a char from a sting that is made up of hex values...
How do I covert a char from a sting that is made up of hex values into a long int, without using istringstream and istring in c++?
What do you understand from the following, describe all in detail, and ADD the specimens wherever...
What do you understand from the following, describe all in detail, and ADD the specimens wherever applicable……… Audit Engagement Letter Audit Appointment Letter Audit Planning and Initial Assessments / Walk Through in Audits Audit Verification and Six Point Technique Vouching – the first step towards substantial analysis Verification of Assets, Verification of Liabilities and Revenue Accounts Professional Ethics for CPAs and External Audits ( All aspects ) Code of Corporate Governance in Saudi Arabia do not forget to have references...
When already given tool values, how do I create cut-off values from scratch for a screening...
When already given tool values, how do I create cut-off values from scratch for a screening tool?
how do you add the chart in excel?
how do you add the chart in excel?
How do I add white space in the beginning of my printf statement in C? I...
How do I add white space in the beginning of my printf statement in C? I also need the white space to be dynamic by using an int value as reference for the amount of spaces. Thanks!
How do you add an error bar when the add error bar is not included in...
How do you add an error bar when the add error bar is not included in the add chart element box? How do you do the dependent t-test calculation in excel?
how do i construct a scatterplot if L1 lists negative values and L2 has values such...
how do i construct a scatterplot if L1 lists negative values and L2 has values such as 0.00339156, 0.00326318, 0.00313725 ? Do i enter the L2 values as exponents??
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT