0
votes

I am working in core java. I have one small program which will print numbers 1,2,3 by corresponding threads thread1 , thread2 and thread3.

while(true)
        {
            synchronized (obj)
            {

                System.out.println("Thread got chance : "+Thread.currentThread().getName());


                if (ai.get() ==0 && Thread.currentThread().getName().equalsIgnoreCase("Thread1"))
                {
                    System.out.print(ai.incrementAndGet()+"("+Thread.currentThread().getName()+")"+" ");
                    obj.notify();
                    obj.wait();
                }

                if (ai.get() ==1 && Thread.currentThread().getName().equalsIgnoreCase("Thread2"))
                {
                    System.out.print(ai.incrementAndGet()+"("+Thread.currentThread().getName()+")"+" ");
                    obj.notify();
                    obj.wait();
                }

                if (ai.get() ==2 && Thread.currentThread().getName().equalsIgnoreCase("Thread3"))
                {
                    System.out.print(ai.incrementAndGet()+"("+Thread.currentThread().getName()+")"+" ");
                    System.out.println("");
                    ai.set(0);
                    obj.notify();
                    obj.wait();
                }

            }

        }

in the above program logic is fine and working i.e it is printing in order. But I have printed the thread name with "Thread got change ". i.e I was trying to identify which thread is getting more chance to run and got to know the thread name Ex thread 2.

The question is "How I can make sure that, all the threads are getting same chances. Since this is the small program , thread will do its work and come out within milliseconds and we will not come to know. What if a thread takes little long time to run" ?

Please help.

1

1 Answers

0
votes

synchronized does not support a fair policy. Any waiting thread could be waken up when you call notify().

You can use a fair ReentrantLock:

  • When you call lock(), the longest waiting thread will acquire the lock.
  • When you call signal(), the longest waiting thread will be signalled first.

This is the example code:

// Use the same lock and condition in different threads
ReentrantLock lock = new ReentrantLock(true);  // create a fair lock
Condition condition = lock.newCondition();

while (true) {
    lock.lock();
    try {
        System.out.println("Thread got chance : " + Thread.currentThread().getName());

        if (ai.get() == 0 && Thread.currentThread().getName().equalsIgnoreCase("Thread1")) {
            System.out.print(ai.incrementAndGet() + "(" + Thread.currentThread().getName() + ")" + " ");
            condition.signal();
            condition.await();
        }

        if (ai.get() == 1 && Thread.currentThread().getName().equalsIgnoreCase("Thread2")) {
            System.out.print(ai.incrementAndGet() + "(" + Thread.currentThread().getName() + ")" + " ");
            condition.signal();
            condition.await();
        }

        if (ai.get() == 2 && Thread.currentThread().getName().equalsIgnoreCase("Thread3")) {
            System.out.print(ai.incrementAndGet() + "(" + Thread.currentThread().getName() + ")" + " ");
            System.out.println("");
            ai.set(0);
            condition.signal();
            condition.await();
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        lock.unlock();
    }
}