answersLogoWhite

0


Best Answer

// 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

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

This is an example from The Java Tutorials on creating a "for" loop. They happen to use a program that counts 1 to 10 as an example.

What Is a Package? -see Related links

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a Java program to count 1 to 1000 numbers using 10 threads?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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; }


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

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


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; }


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.


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


Write a java program to count the space from the given line?

.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......