STIW3054-A182 / Main-Issues

5 stars 1 forks source link

CyclicBarrier #16

Open zhamri opened 5 years ago

zhamri commented 5 years ago

Instruction:

Write a Java program to execute THREE (3) threads. Each of the thread will display 10 sequence numbers from 1 to 10. After all the threads have been terminated, then the program will sum 10 sequence numbers from 1 to 100. You MUST use CyclicBarrier and implement Runnable Interface to solve this problem.

Submission

  1. Java code.
  2. Screenshot of the output.
  3. References
muhammadbimo1 commented 5 years ago
import java.util.Date;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class runclass {

    public static void main(String[] args) {

        //3 threads are part of the barrier, ServiceOne, ServiceTwo and this main thread calling them.
        final CyclicBarrier barrier = new CyclicBarrier(3);
        int sum = 0;

        Thread serviceOneThread = new Thread(new displaynumber(barrier));
        Thread serviceTwoThread = new Thread(new displaynumber(barrier));
        Thread serviceThreeThread = new Thread(new displaynumber(barrier));

        serviceOneThread.start();
        serviceTwoThread.start();
        serviceThreeThread.start();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        }
        System.out.println("Ending all the services at "+new Date());
        for(int i=0;i<=100;i++){
            sum = sum+i;
            }
        System.out.println("sum= "+sum);
        }
    }
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class displaynumber implements Runnable {

    private final CyclicBarrier cyclicBarrier;

    public displaynumber(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        try {
            for(int i=1;i<=10;i++){
                System.out.println(Thread.currentThread().getName()+"--"+i);
                Thread.sleep(50);
            }
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" finished its work... waiting for others...");
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            System.out.println("Service one interrupted!");
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            System.out.println("Service one interrupted!");
            e.printStackTrace();
        }
        System.out.println("The wait is over, lets complete "+Thread.currentThread().getName());
    }

}
Thread-2--1
Thread-0--1
Thread-1--1
Thread-2--2
Thread-1--2
Thread-0--2
Thread-2--3
Thread-1--3
Thread-0--3
Thread-2--4
Thread-0--4
Thread-1--4
Thread-2--5
Thread-1--5
Thread-0--5
Thread-2--6
Thread-1--6
Thread-0--6
Thread-2--7
Thread-0--7
Thread-1--7
Thread-2--8
Thread-1--8
Thread-0--8
Thread-2--9
Thread-0--9
Thread-1--9
Thread-2--10
Thread-0--10
Thread-1--10
Thread-2 finished its work... waiting for others...
Thread-0 finished its work... waiting for others...
Thread-1 finished its work... waiting for others...
The wait is over, lets complete Thread-1
The wait is over, lets complete Thread-2
The wait is over, lets complete Thread-0
Ending all the services at Wed May 08 11:56:49 SGT 2019
sum= 5050

Process finished with exit code 0

reference: https://examples.javacodegeeks.com/core-java/util/concurrent/cyclicbarrier/java-util-concurrent-cyclicbarrier-example/

SINHUI commented 5 years ago

Java Code

Issue16

import java.util.concurrent.CyclicBarrier;

public class issue16 {
  static int total=0;
    public static void main(String[] args) {     
         CyclicBarrier barrier = new CyclicBarrier (3,new Runnable() {
             @Override
             public void run() {
                 for (int i=0;i<=100;i++) {
                     total+=i;  
                    }
                 System.out.println(total);
             }});

         Thread t1 = new Thread(new testing(barrier));
         Thread t2 = new Thread(new testing(barrier));
         Thread t3 = new Thread(new testing(barrier));

         t1.start();
         t2.start();
         t3.start();         
    }
    }

testing


import java.util.concurrent.CyclicBarrier;

public class testing implements Runnable {
 CyclicBarrier cyclicBarrier;

    public testing ( CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier=cyclicBarrier;
    }

    public void run() {

         try {
                for(int i=1;i<=10;i++){
                    System.out.println(Thread.currentThread().getName()+"---> "+i);
                    Thread.sleep(50);
                }
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        try {   
            cyclicBarrier.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output

image

References

https://github.com/zhamri/MyClass-STIW3054/wiki/Example-of-Questions (Question no. 6)

trinanda98 commented 5 years ago
package CyclicBarrierIssue;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class ThreadClass {
      public static void main(String[] args) {

            //3 threads are part of the barrier, ServiceOne, ServiceTwo and this main thread calling them.
            final CyclicBarrier barrier = new CyclicBarrier(3);

            Thread thread1 = new Thread(new Thread1(barrier));
            Thread thread2 = new Thread(new Thread1(barrier));
            Thread thread3 = new Thread(new Thread1(barrier));

            thread1.start();

            thread2.start();

            thread3.start();

            try {
                barrier.await();
            } catch (InterruptedException e) {
                System.out.println("Main Thread interrupted!");
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                System.out.println("Main Thread interrupted!");
                e.printStackTrace();
            }
            double sum1 = 0;
            for(int a = 1; a<=100;a++) {
                sum1 += a;
            }
            System.out.println("Sum : " + sum1);

        }

    }
package CyclicBarrierIssue;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Thread1 implements Runnable {

    private final CyclicBarrier cyclicBarrier;

    public Thread1(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        try {
            for(int a = 1; a<=10;a++) {
                System.out.println(Thread.currentThread().getName()+"--"+a);
                Thread.sleep(50);
            }
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
         System.out.println(Thread.currentThread().getName() + " has finished its work... waiting for others...");

        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            System.out.println("Service one interrupted!");
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            System.out.println("Service one interrupted!");
            e.printStackTrace();
        }

    }

}

Output: Screenshot (82)

Reference https://www.geeksforgeeks.org/java-util-concurrent-cyclicbarrier-java/

lingzhongli commented 5 years ago
import java.util.concurrent.CyclicBarrier;

public class main {

    public static void main(String[] args) {

        CyclicBarrier barrier =new CyclicBarrier(3,new sum());

        Thread t1 = new Thread(new display(barrier));
        Thread t2 = new Thread(new display(barrier));
        Thread t3 = new Thread(new display(barrier));

        t1.start();
        t2.start();
        t3.start();
    }
}
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class display implements Runnable{

    CyclicBarrier barrier;

    public display(CyclicBarrier barrier){
        this.barrier=barrier;

    }

    public void run(){
        try {
        for(int i=1; i<=10; i++){
        System.out.println(Thread.currentThread().getName()+"="+i);
        Thread.sleep(50);}}
        catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

    }
}
public class sum implements Runnable {

    public void run(){

        int sum=0;
        for (int i=0;i<=100;i++){
            sum=sum+i;}
        System.out.println(sum);
    }
}

image

prevenkumar commented 5 years ago
package issue16;

import java.io.IOException;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class MainClass {

public static void main(String[] args) {

    final CyclicBarrier barrier = new CyclicBarrier(3);

    Thread t1 = new Thread(new App(barrier));
    Thread t2 = new Thread(new App(barrier));
    Thread t3 = new Thread(new App(barrier));

        t1.start();
        t2.start();
        t3.start();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        }

    int sum = 0;
    for(int i = 1; i<=100;i++) {
        sum += i;
    }
    System.out.println("Sum : " + sum);
}
} 
package issue16;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class App implements Runnable  {

    private final CyclicBarrier cyclicBarrier;

    public App(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    public void run() {

        try{
           for(int i = 1; i <= 10; i++){
              System.out.println(i);
              Thread.sleep(100);
           }
        }catch(Exception e){
           e.printStackTrace();
        }
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            System.out.println("Service one interrupted!");
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            System.out.println("Service one interrupted!");
            e.printStackTrace();
        }

    }

}

issue16

mimiothman commented 5 years ago

Java Code

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class issue16_cyclic {

    public static void main(String[] args) {

        CyclicBarrier barrier =new CyclicBarrier(3);

        Thread t1 = new Thread(new issue16_output(barrier));
        Thread t2 = new Thread(new issue16_output(barrier));
        Thread t3 = new Thread(new issue16_output(barrier));

        t1.start();
        t2.start();
        t3.start();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        }
        double sum1 = 0;
        for(int a = 1; a<=100;a++) {
            sum1 += a;
        }
        System.out.println("Total  : " + sum1);
    }
}
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class issue16_output implements Runnable{

    CyclicBarrier barrier;

    public issue16_output(CyclicBarrier barrier){
        this.barrier=barrier;

    }

    public void run(){
        try {
        for(int i=1; i<=10; i++){
        System.out.println(Thread.currentThread().getName()+"="+i);
        Thread.sleep(50);}}
        catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

    }
}

Output

image

Reference

  1. https://examples.javacodegeeks.com/core-java/util/concurrent/cyclicbarrier/java-util-concurrent-cyclicbarrier-example/
  2. https://www.baeldung.com/java-cyclic-barrier
rahimahsahar commented 5 years ago

Java Code

package Issues16;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class MainClass {

    public static void main(String[] args) {

        CyclicBarrier barrier =new CyclicBarrier(3);

        Thread t1 = new Thread(new Output(barrier));
        Thread t2 = new Thread(new Output(barrier));
        Thread t3 = new Thread(new Output(barrier));

        t1.start();
        t2.start();
        t3.start();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        }
        double sum1 = 0;
        for(int a = 1; a<=100;a++) {
            sum1 += a;
        }
        System.out.println("Total  : " + sum1);
    }
}
package Issues16;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Output implements Runnable {
    CyclicBarrier barrier;

    public Output(CyclicBarrier barrier){
        this.barrier=barrier;

    }

    public void run(){
        try {
        for(int i=1; i<=10; i++){
        System.out.println(Thread.currentThread().getName()+"="+i);
        Thread.sleep(50);}}
        catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

        double sum1 = 0;
        for(int a = 1; a<=100;a++) {
            sum1 += a;
        }
        System.out.println("Total  : " + sum1);

    }
}

Output issues16

References https://www.geeksforgeeks.org/java-util-concurrent-cyclicbarrier-java/

roflinasuha commented 5 years ago

Java Code


import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CyclicBarrierExample {

    //Runnable task for each thread
    private static class Task implements Runnable {

        private CyclicBarrier barrier;
        int sum =0;

        public Task(CyclicBarrier barrier) {
            this.barrier = barrier;
        }

        @Override
        public void run() {
            for ( int i=1; i<=10; i++)
            {
            try {
                System.out.println(Thread.currentThread().getName() + " = " +i);
                barrier.await();
            } catch (InterruptedException ex) {
                Logger.getLogger(CyclicBarrierExample.class.getName()).log(Level.SEVERE, null, ex);
            } catch (BrokenBarrierException ex) {
                Logger.getLogger(CyclicBarrierExample.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

            for ( int j=1; j<=100; j++)
            {

                sum = sum +j;

            }
            System.out.println("Sum = " + sum);

    }

    public static void main(String args[]) {

        //creating CyclicBarrier with 3 parties i.e. 3 Threads needs to call await()
        final CyclicBarrier cb = new CyclicBarrier(3, new Runnable(){
            @Override
            public void run(){
                //This task will be executed once all thread reaches barrier

            }
        });

        //starting each of thread
        Thread t1 = new Thread(new Task(cb), "Thread 1");
        Thread t2 = new Thread(new Task(cb), "Thread 2");
        Thread t3 = new Thread(new Task(cb), "Thread 3");

        t1.start();
        t2.start();
        t3.start();

    }
}
}

image

References 1) https://www.geeksforgeeks.org/java-util-concurrent-cyclicbarrier-java/ 2) http://tutorials.jenkov.com/java-util-concurrent/cyclicbarrier.html

lishaobin commented 5 years ago

issue15

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class issue15 {

    public static void main(String[] args){
        int sum=0;
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new sum());
       Thread t1 =new Thread(new thread1(cyclicBarrier));
       Thread t2 =new Thread(new thread1(cyclicBarrier));
       Thread t3 =new Thread(new thread1(cyclicBarrier));
       t1.start();
       t2.start();
       t3.start();

}

}

sum.java

public class sum implements Runnable{
    int sum;

    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println(" sum 10 sequence numbers from 1 to 100 ");
         for(int i=0;i<=100;i++){
               sum = sum+i;
               }
           System.out.println("sum= "+sum);

    }

}

thread1.java

import java.util.concurrent.CyclicBarrier;

public class thread1 implements Runnable {
     int x;
     private CyclicBarrier cyclicBarrier;
 public  thread1 (CyclicBarrier cyclicBarrier){
     this.cyclicBarrier = cyclicBarrier;
 }

    @Override
    public void run() {

        // TODO Auto-generated method stub
      try{
          for(x=1;x<=10;x++)
              System.out.println(Thread.currentThread().getName()+"   :"+x);
             cyclicBarrier.await();

      }
      catch(Exception e){

      }
    }

}

image

shyyahcheang commented 5 years ago

1. Java code

package Issue16;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class MainClass {
    public static void main (String[] args) {
        final CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        int sum = 0;

        Thread t1 = new Thread(new DisplayNumber(cyclicBarrier));
        Thread t2 = new Thread(new DisplayNumber(cyclicBarrier));
        Thread t3 = new Thread(new DisplayNumber(cyclicBarrier));

        t1.start();
        t2.start();
        t3.start();

        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

        for (int x=0; x<=100; x++) {
            sum += x;
        }
        System.out.println("\nSUM --> " + sum);
    }
}
package Issue16;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class DisplayNumber implements Runnable {
    private final CyclicBarrier cyclicBarrier;

    public DisplayNumber(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        try {
            for (int a=0; a<=10; a++) {
                System.out.println(Thread.currentThread().getName() + " --> " + a);
                Thread.sleep(50);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}

2. Screenshot of the output

issue 16 - output1

issue 16 - output2

3. References

limxinyii commented 5 years ago

1. Java code

package issue16;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class issue16 {

    public static void main(String[] args) {
        final CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        int sum = 0;

        Thread t1 = new Thread(new number(cyclicBarrier));
        Thread t2 = new Thread(new number(cyclicBarrier));
        Thread t3 = new Thread(new number(cyclicBarrier));

        t1.start();
        t2.start();
        t3.start();

        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

        for (int x=0; x<=100; x++) {
            sum += x;
        }
        System.out.println("SUM -----> " + sum);
    }
}
package issue16;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class number implements Runnable {
    private final CyclicBarrier cyclicBarrier;

    public number(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        try {
            for (int a=0; a<=10; a++) {
                System.out.println(Thread.currentThread().getName() + " --> " + a);
                Thread.sleep(50);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}

2. Screenshot of the output

image

3. References

  1. https://www.geeksforgeeks.org/java-util-concurrent-cyclicbarrier-java/
  2. https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html
  3. https://netjs.blogspot.com/2016/01/cyclicbarrier-in-java-concurrency.html
Yvevonwen commented 5 years ago

JavaCode

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class MainIssue16 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        int sum = 0;

        Thread thread1 = new Thread(new DisplayOutput(cyclicBarrier));
        Thread thread2 = new Thread(new DisplayOutput(cyclicBarrier));
        Thread thread3 = new Thread(new DisplayOutput(cyclicBarrier));

        thread1.start();
        thread2.start();
        thread3.start();

        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

        for (int x=0; x<=100; x++) {
            sum += x;
        }
        System.out.println("Sum of the number = " + sum);
    }
    }
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class DisplayOutput implements Runnable{
     private final CyclicBarrier cyclicBarrier;

        public DisplayOutput(CyclicBarrier cyclicBarrier) {
            this.cyclicBarrier = cyclicBarrier;
        }

        @Override
        public void run() {
            try {
                for (int a=0; a<=10; a++) {
                    System.out.println(Thread.currentThread().getName() + " = " + a);
                    Thread.sleep(50);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
}

Output

image

Reference

  1. https://www.geeksforgeeks.org/java-util-concurrent-cyclicbarrier-java/
  2. http://tutorials.jenkov.com/java-util-concurrent/cyclicbarrier.html
  3. https://examples.javacodegeeks.com/core-java/util/concurrent/cyclicbarrier/java-util-concurrent-cyclicbarrier-example/
mwng97 commented 5 years ago

JavaCode

import java.util.concurrent.*;

public class Issue16 implements Runnable {

    private CyclicBarrier cyclicBarrier;

    Issue16(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        try {
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName() + ": " + i);
                Thread.sleep(50);
            }
            cyclicBarrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}

class Run {
    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(new Thread(new Issue16(cyclicBarrier)));
        executorService.submit(new Thread(new Issue16(cyclicBarrier)));
        executorService.submit(new Thread(new Issue16(cyclicBarrier)));
        cyclicBarrier.await();
        int sum = 0;
        for (int j = 1; j <= 100; j++) {
            sum += j;
        }
        System.out.println("Sum : " + sum);
        executorService.shutdown();
    }
}

Output Output1

Reference https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html

slyee96 commented 5 years ago
import java.util.Date;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class runClass {
        public static void main(String[] args) {
        // TODO Auto-generated method stub
            final CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
            int sum = 0;

            Thread thread1 = new Thread(new displaynumber(cyclicBarrier));
            Thread thread2 = new Thread(new displaynumber(cyclicBarrier));
            Thread thread3 = new Thread(new displaynumber(cyclicBarrier));

            thread1.start();
            thread2.start();
            thread3.start();

            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }

            for (int x=0; x<=100; x++) {
                sum += x;
            }
            System.out.println("Sum of the number = " + sum);
        }
   }
package issue16;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class displaynumber implements Runnable{
     private final CyclicBarrier cyclicBarrier;

        public displaynumber(CyclicBarrier cyclicBarrier) {
            this.cyclicBarrier = cyclicBarrier;
        }

        @Override
        public void run() {
            try {
                for (int a=0; a<=10; a++) {
                    System.out.println(Thread.currentThread().getName() + " = " + a);
                    Thread.sleep(50);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
}

image

lancelot9927 commented 5 years ago

issues 16

package issues16;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class issues16 {

    public static void main(String[] args) {

        CyclicBarrier barrier =new CyclicBarrier(3);

        Thread t1 = new Thread(new output(barrier));
        Thread t2 = new Thread(new output(barrier));
        Thread t3 = new Thread(new output(barrier));

        t1.start();
        t2.start();
        t3.start();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        }
        double sum1 = 0;
        for(int a = 1; a<=100;a++) {
            sum1 += a;
        }
        System.out.println("Total  : " + sum1);
    }
}

output

package issues16;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class output implements Runnable {

     CyclicBarrier barrier;

        public output(CyclicBarrier barrier){
            this.barrier=barrier;

        }

        public void run(){
            try {
            for(int i=1; i<=10; i++){
            System.out.println(Thread.currentThread().getName()+"="+i);
            Thread.sleep(50);}}
            catch (InterruptedException e1) {
                e1.printStackTrace();
            }

            try {
                barrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }

        }
    }

Output

issues16

alfmrza commented 5 years ago

MAIN

package COM.STIW3054.Test;
import java.util.Date;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class issue16 {

    public static void main(String[] args) {

        final CyclicBarrier barrier = new CyclicBarrier(3);
        int sum = 0;

        Thread one = new Thread(new issue16Display(barrier));
        Thread two = new Thread(new issue16Display(barrier));
        Thread three = new Thread(new issue16Display(barrier));

        one.start();
        two.start();
        three.start();

        try {
            barrier.await();
        } catch (InterruptedException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            System.out.println("Main Thread interrupted!");
            e.printStackTrace();
        }
        System.out.println("Ending all the services at "+new Date());
        for(int i=0;i<=100;i++){
            sum = sum+i;
            }
        System.out.println("sum= "+sum);
        }
    }

DISPLAY

package COM.STIW3054.Test;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class issue16Display implements Runnable {

    private final CyclicBarrier cyclicBarrier;

    public issue16Display(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    public void run() {
        try {
            for(int i=1;i<=10;i++){
                System.out.println(Thread.currentThread().getName()+"--"+i);
                Thread.sleep(50);
            }
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" finished its work... waiting for others...");
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            System.out.println("Thread interrupted!");
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            System.out.println("Thread interrupted!");
            e.printStackTrace();
        }
        System.out.println("The wait is over, lets complete "+Thread.currentThread().getName());
    }

}

image