3 Threads Printing numbers in sequence

前端 未结 14 1367
不思量自难忘°
不思量自难忘° 2021-02-05 23:45

I am trying to write a simple code to print numbers in sequence. Scenario is like

Thread  Number
T1        1
T2        2
T3        3
T1        4
T2        5
T3          


        
14条回答
  •  你的背包
    2021-02-06 00:03

    package ThreadCoreConcepts;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 3 Thread T1,T2,T3 will print output {1,2,3 4,5,6 7,8,9} Where T1 will print
     * {1,4,7} , T2 will print { 2,5,8} and T3 will print {3,6,9}
     * 
     * @author harsmahe
     *
     */
    public class ThreeThreadSequenceGen {
        private volatile static int value = 1;
    
        public static void main(String args[]) throws InterruptedException {
            ThreeThreadSequenceGen gen = new ThreeThreadSequenceGen();
            Object mutex = new Object();
            Thread t1 = new Thread(gen.new RunThread(1, mutex));
            t1.setName("1");
            Thread t2 = new Thread(gen.new RunThread(2, mutex));
            t2.setName("2");
            Thread t3 = new Thread(gen.new RunThread(3, mutex));
            t3.setName("3");
            t1.start();
            t2.start();
            t3.start();
    
        }
    
        class RunThread implements Runnable {
            private int start = 0;
            private Object mutex;
            private List list = new ArrayList();
    
            public RunThread(final int start, Object mutex) {
                // TODO Auto-generated constructor stub
                this.start = start;
                this.mutex = mutex;
            }
    
            @Override
            public void run() {
                try {
                    while (value <= 9) {
                        // while (true) {
                        // TODO Auto-generated method stub
                        int name = Integer.valueOf(Thread.currentThread().getName());
                        // System.out.println("[" + Thread.currentThread().getName()
                        // + "]");
                        // notifyAll();
    
                        synchronized (mutex) {
                            if (name == 1 && value == start) {
                                list.add(value);
                                System.out.println("[" + Thread.currentThread().getName() + "]" + value);
                                start = start + 3;
                                value++;
                                mutex.notifyAll();
                                mutex.wait();
                            } else if (name == 2 && value == start) {
                                System.out.println("[" + Thread.currentThread().getName() + "]" + value);
                                list.add(value);
                                start = start + 3;
                                value++;
                                mutex.notifyAll();
                                mutex.wait();
    
                            } else if (name == 3 && value == start) {
                                System.out.println("[" + Thread.currentThread().getName() + "]" + value);
                                list.add(value);
                                start = start + 3;
                                value++;
                                mutex.notifyAll();
                                if (value < 9) {
                                    mutex.wait();
                                }
    
                            } else {
                                mutex.notifyAll();
                                // mutex.wait();
                            }
                        }
    
                    }
    
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    // System.out.println(list);
                }
    
            }
    
        }
    }
    
        enter code here
    

提交回复
热议问题