Order of execution of Threads waiting for a synchronised block

278 Views Asked by At

I have some threads running and all trying to enter the synchronised block.

I have noticed that the thread runs in a random order(when i calls Thread.start()), thats ok

when first Thread executes and enters synchronised method it go to sleep..

during that period others threads comes and start waiting for the synchronised block to get released.

My Question is the last thread came in waiting gets the synchronised block first..

Following is the code.

 import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.sql.rowset.Joinable;


public class ThreadOrdering{  public static void main(String... args) {
    Results results = new Results();
    Thread a=   new Thread(new Task(0, "red", results));
    a.start();

    Thread b=  new Thread(new Task(1, "orange", results));
    b.start();

    Thread c=    new Thread(new Task(2, "yellow", results));
    c.start();

    Thread d= new Thread(new Task(3, "green", results));
    d.start();

    Thread e= new Thread(new Task(4, "blue", results));
    e.start();



}
}

class Results {
private List<String> results = new ArrayList<String>();
private int i = 0;

public synchronized void submit(int order, String result) {
     System.out.println("synchronized accupied by: " + order + " " + result);
       try {
        Thread.sleep((long)(Math.random() *1000));
         System.out.println("synchronized released by: " + order + " " + result);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}
}


class Task implements Runnable {
private final int order;
private final String result;
private final Results results;

public Task(int order, String result, Results results) {
    this.order = order;
    this.result = result;
    this.results = results;
}

public void run() {

     System.out.println("run  by: " + order + " " + result);

    results.submit(order, result);

}
}

The sample output is:

run by: 1 orange

synchronized accupied by: 1 orange

run by: 2 yellow

run by: 4 blue

run by: 0 red

run by: 3 green

synchronized released by: 1 orange

synchronized accupied by: 3 green

synchronized released by: 3 green

synchronized accupied by: 0 red

synchronized released by: 0 red

synchronized accupied by: 4 blue

synchronized released by: 4 blue

synchronized accupied by: 2 yellow

synchronized released by: 2 yellow

as we see the order of thread execution is random.. thats ok.. 1 2 4 0 3

so 1 gets the synchronised block and after it ends 3(the last one) gets it then 0 and 4 and 2....

How to make that order reverse,,, that after 1 it must be 2 then 4 then 0 then 3

2

There are 2 best solutions below

0
On

You should use ReentrantLock if you want fair access to synchronized block.

0
On

My Question is the last thread came in waiting gets the synchronised block first?

Short answer is No.

Multi-thread mechanism doesn't provide such ability o manage wake up order. It depends on many factors which thread is awaken and in what order. You have to manage it manually. Create some shared variable or stack and if thread is not suppose to work yet - yeld it and wait till next time it has control. And then do it again till time/order is right.