answersLogoWhite

0

// need a class to do customized counting
// (this can be a local class)
final class CounterThread extends Thread {


private final int rangeStart;
private final int rangeEnd;
private final int[] nums;
private int count;

private CounterThread(final int rangeStart, final int rangeEnd, final int[] nums) {
super();

this.rangeStart = rangeStart;
this.rangeEnd = rangeEnd;
this.nums = nums;

count = 0;

}

public void run() {
// actual counting process
for (int i = rangeStart; i <= rangeEnd; ++i) {
count += nums[i];

}

}

}

// define the number of ints we want to add up
final int ARRAY_SIZE = 1000;
// define the number of threads to use
final int NUM_THREADS = 10;
// define the number of ints each thread will add
final int NUMS_PER_ARRAY = ARRAY_SIZE / NUM_THREADS;

// NOTE: if ARRAY_SIZE is not evenly divisible by NUM_THREADS, then the
// code will need some tweaking to get the correct answer

int[] nums = new int[ARRAY_SIZE]; // assuming this is filled with numbers to count

// create our array of worker threads
CounterThread[] threads = new CounterThread[NUM_THREADS];
for (int i = 0; i < threads.length; ++i) {

// create the new thread which will count the given range
threads[i] = new CounterThread((i * NUMS_PER_ARRAY), (((i + 1) * NUMS_PER_ARRAY) - 1), nums);
// start the thread
threads[i].start();

}

// wait for all threads to finish
for (int i = 0; i < threads.length; ++i) {

threads[i].join();

}

// add up the individual counts from each thread
int totalCount = 0;
for (int i = 0; i < threads.length; ++i) {

totalCount += threads[i].count;

}

System.out.println(totalCount);

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

Program to count the number of numbers in an array using 8085 microprocessor?

A program which is used to count the number of numbers in an array using a 8085 microprocessor is known as a assembly language program.


What is a thread count?

Technically, thread count means the number of threads woven together in a square inch. You count both lengthwise (warp) and widthwise (weft) threads. So 100 lengthwise threads woven with 100 widthwise threads produce a thread count of 200.&Acirc;_


How do you write a program to find out the percentage of given numbers using the structure in C?

Count all the given numbers then count all the numbers of a given value. Divide one by the other and multiply by 100. For instance, if there are 50 numbers in total and 5 of them have the value 42, then the percentage of numbers with the value 42 is 5 / 50 * 100 = 10%.


How do you write a program in c that will count from 1 to 10 and its square for each count?

int i; for (i=1; i&lt;=10; i++) printf ("%d %d\n", i, i*i);


Write a c or c plus plus program to print first 50 odd numbers using do while loop?

unsigned count = 0;unsigned num=1; do { std::cout &lt;&lt; num &lt;&lt; std::endl; num +=2; } while (++count&lt;50);


Write a program that ask the user to enter a number n and display the first n even numbers?

#include "stdio.h" int main() { unsigned int number, count; printf("Enter the Number \t"); scanf("%d", &amp;number); printf("The even numbers are: \n"); for(count = 0x01; (count &lt; number &amp;&amp; number!= 0x00)) { if(count%2) { }else { printf("%d\n", count); } count++; } return 0; }


Write a program using while loop?

//program to find the factorial value f any number using while loop #include&lt;stdio.h&gt; void main() { int i,n,fact=1; printf("Enter the number\n"); scanf("%d",&amp;n); i=n; while (i&gt;=1) { fact=fact*i; i--; } printf("The factorial value=%d",fact); } the above is a program for calculating tha factorial value of any number which is entered by the user


How do you count the number of objects available in a program in c?

Use ordinal numbers: 0, 1, 2, ...


It 4-1 solved question papers November -2008?

Write a program to count the number of IS in any number in register B and put the count in R5.


Write a program to print prime number using loop?

#include&lt;stdio.h&gt; int main() { int i=1,j,count,n; printf("enter number range\n"); scanf("%d", &amp;n); printf("following numbers are prime numbers:\t"); while(i&lt;=n) { j=1; count=0; while(j&lt;=n) { if(i%j==0) { count++; } j++; } if(count==2) { printf("%d\t", i); } i++; } return 0; }


What is 400 thread count in cotton percale?

400 thread count means that the weaving loom wove the threads together so tightly that there is 400 threads in every inch of fabric. Usually designated 400 TPI (threads per inch).


Algoritm source code for write a c-program to count lines words and characters in a given text?

yyu5uty