Created
March 18, 2011 20:43
-
-
Save amadamala/876800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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