running 3 threads in sequence java

前端 未结 10 564
天涯浪人
天涯浪人 2020-11-29 04:35

I have 3 threads 1st printing A 2nd printing B 3rd printing C

I want to print in sequence A B C A B C A B C and so on.....

So I wrote the program below, but

相关标签:
10条回答
  • 2020-11-29 05:25

    Here is my solution please try and let me know

    package thread;
    
    class SyncPrinter {
        public static void main(String[] args) {
            SyncPrinterAction printAction1 = new SyncPrinterAction(new int[]{1,5,9,13}, true);
            SyncPrinterAction printAction2 = new SyncPrinterAction(new int[]{2,6,10,14}, true);
            SyncPrinterAction printAction3 = new SyncPrinterAction(new int[]{3,7,11,15}, true);
            SyncPrinterAction printAction4 = new SyncPrinterAction(new int[]{4,8,12,16}, false);
    
            printAction1.setDependentAction(printAction4);
            printAction2.setDependentAction(printAction1);
            printAction3.setDependentAction(printAction2);
            printAction4.setDependentAction(printAction3);
    
            new Thread(printAction1, "T1").start();;        
            new Thread(printAction2, "T2").start();
            new Thread(printAction3, "T3").start();     
            new Thread(printAction4, "T4").start();
    
    
    
        }
    }
    
    class SyncPrinterAction implements Runnable {
    
        private volatile boolean dependent;
        private SyncPrinterAction dependentAction;
        int[] data;
    
        public void setDependentAction(SyncPrinterAction dependentAction){
            this.dependentAction = dependentAction;
    
        }
    
        public SyncPrinterAction( int[] data, boolean dependent) {
            this.data = data;
            this.dependent = dependent;
        }
    
        public SyncPrinterAction( int[] data, SyncPrinterAction dependentAction, boolean dependent) {
            this.dependentAction = dependentAction;
            this.data = data;
            this.dependent = dependent;
        }
    
        @Override
        public void run() {
            synchronized (this) {
    
                for (int value : data) {
    
                    try {
                        while(dependentAction.isDependent())
                            //System.out.println("\t\t"+Thread.currentThread().getName() + " :: Waithing for dependent action to complete");
                        wait(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    dependentAction.setDependent(true);
    
                    System.out.println(Thread.currentThread().getName() + " :: " +value);
                    dependent = false;
                }
    
            }
    
        }
    
        private void setDependent(boolean dependent) {
            this.dependent = dependent;
    
        }
    
        private boolean isDependent() {
            return dependent;
        }
    
    }
    
    0 讨论(0)
  • 2020-11-29 05:26
        public class Main {
            public static void main(String[] args) throws IOException{
            Thread t1 = new Thread(new A(), "1");
            Thread t2 = new Thread(new A(), "2");
            Thread t3 = new Thread(new A(), "3");
    
            t1.start();
            try{
                t1.join();
            }catch (Exception e){
    
            }
            t2.start();
            try{
                t2.join();
            }catch (Exception e){
    
            }
            t3.start();
            try{
                t3.join();
            }catch (Exception e){
    
            }
    
    
        }
    }
    
        class A implements Runnable{
        public void run(){
            System.out.println(Thread.currentThread().getName());
        }
    }
    

    or you can use Executor Framework

    public class Sequence {
        int valve = 1;
        public static void main(String[] args){
            Sequence s = new Sequence();
            ExecutorService es = Executors.newFixedThreadPool(3);
    
            List<Runnable> rList = new ArrayList<>();
            rList.add(new A(s));
            rList.add(new B(s));
            rList.add(new C(s));
    
            for(int i = 0; i < rList.size(); i++){
                es.submit(rList.get(i));
            }
            es.shutdown();
    
        }
    }
    
    class A implements Runnable{
        Sequence s;
    
        A(Sequence s){
            this.s = s;
        }
    
        public void run(){
            synchronized (s) {
                for (int i = 0; i < 10; i++) {
                    while (s.valve != 1) {
                        try {
                            s.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("A");
                    s.valve = 2;
                    s.notifyAll();
                }
            }
        }
    }
    
    class B implements Runnable{
        Sequence s;
    
        B(Sequence s){
            this.s = s;
        }
    
        public void run() {
            synchronized (s) {
                for (int i = 0; i < 10; i++) {
                    while (s.valve != 2) {
                        try {
                            s.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("B");
                    s.valve = 3;
                    s.notifyAll();
                }
            }
        }
    }
    
    class C implements Runnable{
        Sequence s;
    
        C(Sequence s){
            this.s = s;
        }
    
        public void run() {
            synchronized (s) {
                for(int i = 0; i < 10; i++) {
                    while (s.valve != 3) {
                        try {
                            s.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("C");
                    s.valve = 1;
                    s.notifyAll();
                }
            }
        }
    }
    

    In the first case the join for each thread causes the threads to wait for one another. In the second case a list stores the threads and executor executes them one after another creating 3 threads

    Another way to do this is where only one runnable class is present and communication between thread is done via static variable in the main class and a variable in the runnable class

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.Executor;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Seq {
        int i = 1;
        public static void main(String[] args){
            Seq s = new Seq();
            Common c1 = new Common(s, 1);
            Common c2 = new Common(s, 2);
            Common c3 = new Common(s, 3);
    
            List<Runnable> l = new ArrayList<>();
            l.add(c1);
            l.add(c2);
            l.add(c3);
    
            ExecutorService es = Executors.newFixedThreadPool(3);
            for(int i = 0; i < 3; i++){
                es.submit(l.get(i));
            }
            es.shutdown();
        }
    }
    
    class Common implements Runnable{
        Seq s;
        int o;
    
        Common(Seq s, int o){
            this.s = s;
            this.o = o;
        }
    
        public void run(){
            synchronized (s) {
                for (int z = 0; z < 100; z++) {
                    if(s.i > 3)
                        s.i = 1;
                    while (s.i != o) {
                        try {
                            s.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(o);
                    s.i++;
                    s.notifyAll();
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 05:28

    You need to replace

    if (notifyAllExample.status!=1)
    

    with

    while (notifyAllExample.status!=1)
    

    and same thing in the other 2 classes. If not, then as soon as the wait exits the thread continues without knowing if it is its turn.

    0 讨论(0)
  • I think it's simpler to achieve this using join. Example:

        public static void main(String[] args) {    
        final Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                System.out.println("i am thread: " + Thread.currentThread().getName());
            }
        };
    
        final Thread t2 = new Thread(t1, "t2") {
            @Override
            public void run() {
                t1.start();
                try {
                    t1.join();
                } catch ( InterruptedException e ) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("i am thread: " + Thread.currentThread().getName());
            }
        };
    
        Thread t3 = new Thread(t2, "t3") {
            @Override
            public void run() {
                t2.start();
                try {
                    t2.join();
                } catch ( InterruptedException e ) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("i am thread: " + Thread.currentThread().getName());
            }
        };
    
        t3.start();
    

    }

    0 讨论(0)
提交回复
热议问题