[java] Printing Even and Odd using two Threads in Java

Working solution using single class

package com.fursa.threads;

   public class PrintNumbers extends Thread {

     Object lock;

    PrintNumbers(Object lock) {
         this.lock = lock;
    }

    public static void main(String ar[]) {
        Object obj = new Object();
        // This constructor is required for the identification of wait/notify
        // communication
        PrintNumbers odd = new PrintNumbers(obj);
        PrintNumbers even = new PrintNumbers(obj);
        odd.setName("Odd");
        even.setName("Even");
        even.start();
        odd.start();

    }

    @Override
    public void run() {
        for(int i=0;i<=100;i++) {

            synchronized (lock) {

                if (Thread.currentThread().getName().equals("Even")) {

                    if(i % 2 == 0 ){
                        System.out.println(Thread.currentThread().getName() + " - "+ i);
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }                   
                    else if (i % 2 != 0 ) {
                        lock.notify();
                    }
                }

                if (Thread.currentThread().getName().equals("Odd")) {

                    if(i % 2 == 1 ){
                        System.out.println(Thread.currentThread().getName() + " - "+ i);
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }                   
                    else if (i % 2 != 1 ) {
                        lock.notify();
                    }
                }

            }
        }
    }
}

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to multithreading

How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Waiting until the task finishes What is the difference between Task.Run() and Task.Factory.StartNew() Why is setState in reactjs Async instead of Sync? What exactly is std::atomic? Calling async method on button click WAITING at sun.misc.Unsafe.park(Native Method) How to use background thread in swift? What is the use of static synchronized method in java? Locking pattern for proper use of .NET MemoryCache

Examples related to synchronization

fs.writeFile in a promise, asynchronous-synchronous stuff Use Robocopy to copy only changed files? Wait until flag=true Why is synchronized block better than synchronized method? Printing Even and Odd using two Threads in Java How to use the CancellationToken property? Sharing a variable between multiple different threads How to keep two folders automatically synchronized? Asynchronous Process inside a javascript for loop Java Singleton and Synchronization