Thread 1 prints even and Thread 2 prints odd number in java




Print in sequence with Threads in java



Program 1= Thread 1 prints even and Thread 2 prints odd number in java - using if else


class MyRunable implements Runnable {

    int i = 1;

    public void run() {

          while (i < 10) {
                 synchronized (this) {
                       if (Thread.currentThread().getName().equals("Thread1")) {
                              if (i % 2 == 1) { // odd
                                    System.out.println(i++ + " ,p1.1, " + Thread.currentThread().getName());
                                    try {
                                           Thread.sleep(10);
                                    catch (InterruptedException e) {
                                           // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    notify();
                              else {
                                    try {
                                           wait();
                                        System.out.println(i++ + " ,p1.2, " + Thread.currentThread().getName());
                                           notify();
                                    catch (InterruptedException e) {
                                           // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                              }
                       else if (Thread.currentThread().getName().equals("Thread2")) {
                              if (i % 2 == 0) { // even
                                    System.out.println(i++ + " ,p2.1, " + Thread.currentThread().getName());
                                    notify();
                              else {
                                    try {
                                           wait();
                                        System.out.println(i++ + " ,p2.2, " + Thread.currentThread().getName());
                                           notify();
                                    catch (InterruptedException e) {
                                           // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                              }
                       }
                 }
          }
    }

}

public class TwoTheadEvenOdd_singleMethod_usingIfElse {
    public static void main(String[] args) {
          Runnable rnew MyRunable();
          new Thread(r"Thread1").start();
          new Thread(r"Thread2").start();
    }
}

//output
/*
1 ,p1.1, Thread1
2 ,p2.2, Thread2
3 ,p1.1, Thread1
4 ,p2.2, Thread2
5 ,p1.1, Thread1
6 ,p2.2, Thread2
7 ,p1.2, Thread1
8 ,p2.2, Thread2
9 ,p1.2, Thread1
10 ,p2.2, Thread2
*/

Program 2= Thread 1 prints even and Thread 2 prints odd number in java - using linked blocking queue


import java.util.concurrent.LinkedBlockingQueue;

class MyRunnable1 implements Runnable {
    MyRunnable1 next;

    LinkedBlockingQueue<Integer> lbq;

    MyRunnable1() {
          lbqnew LinkedBlockingQueue<>();
    }

    void accept(int i) {
          try {
                 lbq.put(i);
          catch (InterruptedException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
          }
    }

    public void run() {
          int i = 0;
          while (true) {
                 try {
                       Thread.sleep(1000);
                       ilbq.take()//It will wait till lbq.put(i); is called ,
                                           //so only one thread will call it a time
                 catch (Exception e) {
                       e.printStackTrace();
                 }
              System.out.println(Thread.currentThread().getName() + " = "i);
                 next.accept(++i); //Now, current thread is executed , calling accept will call ,
                                    //lbq.put(i), which will end blocking state of thread on onext object
          }
    }

}

public class ThreadsSequence_blockingQueue_EvenOddBlog2 {

    public static void main(String[] args) {
          MyRunnable1 m1new MyRunnable1();
          MyRunnable1 m2new MyRunnable1();

          m1.nextm2//Chain all the objects
          m2.nextm1;

          new Thread(m1"Thread1").start();
          new Thread(m2"Thread2").start();

          m1.accept(1); //Wait make first thread to run
    }
}

/*output

Thread1 = 1
Thread2 = 2
Thread1 = 3
Thread2 = 4
Thread1 = 5
Thread2 = 6


*/



RELATED LINKS>

Thread Pool, Thread local, Busy Spin, ThreadGroup >

Implement Thread pool in java

Implementing ThreadPool using custom LinkedBlockingQueue

ThreadLocal in multithreading in java - methods and usage with program

Busy Spin - What is busy spin? Consumer Producer problem with busy spin and solution to busy spin.

ThreadGroup in java

Demonstrate ThreadGroup's important methods in DETAIL


eEdit
Must read for you :