Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Summer extends Thread {
- int begin;
- int end;
- int[] sharedBuffer;
- int sum = 0;
- Summer(int begin, int end, int[] sharedBuffer) {
- this.begin = begin;
- this.end = end;
- this.sharedBuffer = sharedBuffer;
- }
- @Override
- public void run() {
- for (int i = begin; i < end; i++) {
- sum += sharedBuffer[i];
- }
- }
- int getSum() {
- return sum;
- }
- }
- public class Main {
- public static void main(String[] args) {
- System.out.println("Hello World!");
- int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- Summer thread1 = new Summer(0, 5, numbers);
- Summer thread2 = new Summer(5, 10, numbers);
- thread1.start();
- thread2.start();
- try {
- thread1.join();
- thread2.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- int sum = thread1.getSum() + thread2.getSum();
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement