Skip to content

Instantly share code, notes, and snippets.

@amadamala
Created March 18, 2011 20:43
Show Gist options
  • Save amadamala/876800 to your computer and use it in GitHub Desktop.
Save amadamala/876800 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
public class SumOfThreads implements Runnable {
static int sum[] = new int[3];
static int idx = 0;
int n1, n2;
SumOfThreads(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public void run() {
int s1 = ( n1 * ( n1 - 1 ) ) / 2;
int s2 = ( n2 * ( n2 + 1 ) ) / 2;
sum[idx] = (s2 - s1);
idx++;
}
public static void main(String[] args) {
new Thread(new SumOfThreads( 1 , 100)).start();
new Thread(new SumOfThreads( 101 , 200)).start();
new Thread(new SumOfThreads( 201 , 300)).start();
System.out.println(Arrays.toString(sum));
int result = 0;
for(int s : sum) {
result += s;
}
System.out.println("Result :: " + result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment