Odd even number printing using thread

前端 未结 13 1955
挽巷
挽巷 2020-11-29 08:39

Odd even number printing using thread.Create one thread class, two instance of the thread. One will print the odd number and the other will print the even number.

相关标签:
13条回答
  • 2020-11-29 09:15

    Here's my solution without any waits or notify. wait() and notify()/notifyAll() ,
    I dont see any reason to use them for this problem statement.

    package threading;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class EvenOddPrinting {
    
        int count=0;
        boolean isOdd = false;
    
        public static void main(String[] args) {
            ExecutorService exec = Executors.newCachedThreadPool();
            EvenOddPrinting obj = new EvenOddPrinting();
            exec.submit(new EvenPrinter(obj));
            exec.submit(new OddPrinter(obj));
            exec.shutdown();
    
    
    
        }
    
    }
    
    class EvenPrinter implements Runnable{
        EvenOddPrinting obj;
        public EvenPrinter(EvenOddPrinting obj) {
            this.obj=obj;
        }
    
        @Override
        public void run() {
            while(obj.count < 100){
                if(!obj.isOdd){
                    System.out.println("Even:"+obj.count);
                    obj.count++;
                    obj.isOdd = true;
                }
            }
    
        }
    }
    
    
    class OddPrinter implements Runnable{
    
        EvenOddPrinting obj;
        public OddPrinter(EvenOddPrinting obj) {
            this.obj = obj;
        }
    
        @Override
        public void run() {
            while(obj.count < 100){
                if(obj.isOdd){
                    System.out.println("Odd:"+obj.count);
                    obj.count++;
                    obj.isOdd = false;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 09:16

    Your code corrected with using Lock interface:

    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class NumberPrinter implements Runnable {
        private Lock lock;
        private Condition condition;
        private String type;
        private static boolean oddTurn = true;
    
        public NumberPrinter(String type, Lock lock, Condition condition) {
            this.type = type;
            this.lock = lock;
            this.condition = condition;
        }
    
        public void run() {
            int i = type.equals("odd") ? 1 : 2;
            while (i <= 10) {
                if (type.equals("odd"))
                    printOdd(i);
                if (type.equals("even"))
                    printEven(i);
                i = i + 2;
            }
        }
    
        private void printOdd(int i) {
            // synchronized (lock) {
            lock.lock();
            while (!oddTurn) {
                try {
                    // lock.wait();
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(type + " " + i);
            oddTurn = false;
            // lock.notifyAll();
            condition.signalAll();
            lock.unlock();
        }
    
        // }
    
        private void printEven(int i) {
            // synchronized (lock) {
            lock.lock();
            while (oddTurn) {
                try {
                    // lock.wait();
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(type + " " + i);
            oddTurn = true;
            // lock.notifyAll();
            condition.signalAll();
            lock.unlock();
        }
    
        // }
    
        public static void main(String[] args) {
            Lock lock = new ReentrantLock();
            Condition condition = lock.newCondition();
            Thread odd = new Thread(new NumberPrinter("odd", lock, condition));
            Thread even = new Thread(new NumberPrinter("even", lock, condition));
            odd.start();
            even.start();
        }
    }
    
    0 讨论(0)
  • 2020-11-29 09:17

    I have implemented in such a way, based on the argument, no of threads will be spawned and will the respective no in round robin manner. i.e., If thread count is 3, thread 1 will print 1,4 ...; thread 2 will print 2,5,... and thread 3 will print 3,6...

    public class ThreadSynchronizer
    {
    
        public static void main(String[] args)
        {
        // BASED ON THE ARGUMENT MULTIPLE THREADS WILL BE CREATED AND EACH WILL PRINT ITS RESPECTIVE NO
        // IE, IF THREAD COUNT IS 3, THREAD 1 WILL PRINT 1,4 ..., THREAD2 WILL PRINT 2,5,... AND THREAD3 WILL PRINT 3,6...
        // LIMITED THE DISPLAY TO 1000 NOS
        int threadCnt = Integer.parseInt(args[0]);
    
        ReentrantLock lckArray[] = new ReentrantLock[threadCnt + 1];
    
        for (int i = 0; i < threadCnt + 1; i++)
        {
            ReentrantLock lck = new ReentrantLock();
            lck.lock();
            lckArray[i] = lck;
        }
    
        for (int i = 0; i < threadCnt; i++)
        {
            Thread th = new Thread(new Printer(lckArray, i + 1));
            th.start();
        }
    
        for (int i = 1; i < threadCnt + 1; i++)
        {
            lckArray[i].unlock();
    
            while (!lckArray[i].isLocked())
            {
    
            }
        }
        lckArray[0].unlock();
        }
    }
    
    class Printer implements Runnable
    {
    
        private ReentrantLock[] lckArray;
        private int index;
    
        Printer(ReentrantLock[] lckArray, int startValue)
        {
        this.lckArray = lckArray;
        this.index = startValue;
        }
    
        @Override public void run()
        {
        ReentrantLock prevLock = null;
        int printCounter = index;
    
        for (int counter = 0; printCounter <= 1000; counter++)
        {
            int remCounter = counter % lckArray.length;
            int incCounter = lckArray.length - remCounter;
            int indexPostion = index + incCounter;
            int curElementIndex = indexPostion % lckArray.length;
    
            lckArray[curElementIndex].lock();
            if (prevLock != null)
            prevLock.unlock();
            prevLock = lckArray[curElementIndex];
    
            if (curElementIndex == 0)
            {
            System.out.println("Printed by Thread " + index + " " + printCounter);
            printCounter = printCounter + lckArray.length - 1;
            }
    
        }
    
        if (prevLock != null)
        {
            if (prevLock.isHeldByCurrentThread())
            prevLock.unlock();
        }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-29 09:18

    i Used a shared object to control the order of execution

    class Counter implements Runnable {
      int count;
      static Class cl = Counter.class;
    
      public synchronized void increment() {
            String tname = Thread.currentThread().getName();
            System.out.printf("%s: %d\n", tname, count++);
      }
    
      @Override
      public void run() {
            String tname = Thread.currentThread().getName();
            while (true) {
    
                  increment();
                  synchronized (Counter.class) {
                        try {
                              cl.notify();
                              cl.wait();
                        } catch (Exception e) {
                              e.printStackTrace();
                        }
                  }
    
            }
      }
    }
    
    public class WaitNotify {
    public static void main(String[] args) {
        Counter c = new Counter();
        Thread t1 = new Thread(c, "thread1");
        Thread t2 = new Thread(c, "thread2");
        t1.start();
        t2.start();
    }
    }
    
    0 讨论(0)
  • 2020-11-29 09:19
    public class Number_Thread extends Thread {
    
    String thread;
    int limit;
    
    public Number_Thread(String thread,int limit){
        this.thread=thread;
        this.limit=limit;
                                                 }
    
    Object lock=new Object();
    
    public  void run()
    {
    
        synchronized (lock) 
        {
    
              //------------------- "print even"--------------------//
    
          if(thread.equals("even"))
            {
                for (int i = 2; i <=limit; i+=2) 
                {
    
                     System.out.println(thread+" thread "+i);
                try {
    
                lock.wait(1000);
                     continue;
    
                    }
                  catch (InterruptedException e) {}
                }
                lock.notifyAll();
            }
    
             //------------------- "print odd"--------------------//
    
        if(thread.equals("odd"))
             {
             for (int i = 1; i <=limit; i+=2) 
                 {
                    System.out.println(thread+" thread  "+i);
                try {
    
                    lock.wait(1000);
                        continue;
                    }
                catch (InterruptedException e) {}
                }
                lock.notifyAll();
             }
          }
       }
    }
    
         //------------------thread creater class------------------//
    import java.util.Scanner;
    
    public class Main_Thread {
    private static Scanner s;
        public static void main(String[] args) throws InterruptedException {
            System.out.print("enter limit:\t ");
            s=new Scanner(System.in);
        int n=s.nextInt();
         s.close();
        Thread t1=new Number_Thread("even",n);
        Thread t2=new Number_Thread("odd",n);
        t2.start();
        Thread.sleep(100);
        t1.start();
    
     }
    
    }
    

    output for limit 5:

    enter limit: 5

    odd thread 1

    even thread 2

    odd thread 3

    even thread 4

    odd thread 5

    0 讨论(0)
  • 2020-11-29 09:24

    You're waiting and notifying different objects (monitors).

    The idea is that you can call obj.wait() to wait for someone to do obj.notify(), while you're doing objA.wait() and objB.notify().

    Change your printOdd method to something like

    private void printOdd(int i) {
        synchronized (lock) {                        // <-------
            while (!oddTurn) {
                try {
                    lock.wait();                     // <-------
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(type + i);
            oddTurn = false;
            lock.notifyAll();                        // <-------
        }
    }
    

    and the printEven method similarly.

    Then provide the NumberPrinter with a lock object:

    Object lock = new Object();
    Thread odd = new Thread(new NumberPrinter("odd", lock));
    Thread even = new Thread(new NumberPrinter("even", lock));
    

    Output:

    odd1
    even2
    odd3
    even4
    odd5
    even6
    odd7
    even8
    odd9
    
    0 讨论(0)
提交回复
热议问题