0

I want to print number in the below format. This should be taken care by two thread t1,t2. Can anyone help to enhance the below code which I have written?

First t1 should print 0-4
Then t2 should print 5-9
Then again t1 should print 10-14
Then t2 should print 15-19

0 1 2 3 4

5 6 7 8 9

10 11 12 13 14

class PrintNumber implements Runnable{
    String name;

    public void run(){
            print();
    }

    synchronized public void print(){
            for(int i=0;i< 5;i++){
                System.out.println(i+" -- "+Thread.currentThread());
            }
    }

    public static void main(String[] args){
        Runnable r = new PrintNumber();
        Thread t1 = new Thread(r,"t1");
        Thread t2 = new Thread(r,"t2");
        t1.start();
        t2.start();
    }
}
JavaUser
  • 25,542
  • 46
  • 113
  • 139

2 Answers2

4

Instead of using the low-level wait() and notify() you can use two Sempaphores. Each Runnable has a Semaphore it waits for and one it uses to notify the next one.

import java.util.concurrent.Semaphore;

class PrintNumber implements Runnable{

    static volatile int nextStartIdx; 

    private Semaphore waitForSemaphore;

    private Semaphore next;


    public PrintNumber(Semaphore waitFor, Semaphore next) {
        this.waitForSemaphore = waitFor; 
        this.next = next;
    }


    public void run(){
        while (true) {
            print();
        }

    }

    public void print() {
        try {
            waitForSemaphore.acquire();
            int start = nextStartIdx;

            for(int i=0;i< 5;i++){
                System.out.println(String.format("%d -- %s", i + start, Thread.currentThread().getName()));
            }           

            nextStartIdx += 5;
            next.release();

        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
    }

    public static void main(String[] args){

        Semaphore a = new Semaphore(1);
        Semaphore b = new Semaphore(0);
        Thread t1 = new Thread(new PrintNumber(a,b),"t1");
        Thread t2 = new Thread(new PrintNumber(b,a),"t2");
        t1.start();
        t2.start();
    }
}
andih
  • 5,570
  • 3
  • 26
  • 36
  • Upvote 0,5 because this is probably the better solution in real world sczenarios. And 0,5 because the resemblance between you and patrick swayze is shocking :-) – Chris Jul 07 '17 at 08:12
3

You can use wait and notify to achieve inter thread communication for your sczenario.

class PrintNumber implements Runnable {
    String name;
    Integer count = 0;

    @Override
    public void run() {
        try {
            print();
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized public void print() throws InterruptedException {
        while (count < 15) {
            for (int i = 0; i < 5; i++) {
                count++;
                System.out.println(count + " -- " + Thread.currentThread());
            }
            notifyAll();
            wait();
        }
    }

    public static void main(final String[] args) {
        final Runnable r = new PrintNumber();
        final Thread t1 = new Thread(r, "t1");
        final Thread t2 = new Thread(r, "t2");
        t1.start();
        t2.start();
    }
}

For more information see:

A simple scenario using wait() and notify() in java

Chris
  • 7,675
  • 8
  • 51
  • 101