简体   繁体   English

等待同步块的线程的执行顺序

[英]Order of execution of Threads waiting for a synchronised block

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 我注意到线程以随机顺序运行(当我调用Thread.start()时),那没关系

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 运行者:1个橙色

synchronized accupied by: 1 orange 同步据点:1个橙色

run by: 2 yellow 运行方式:2黄色

run by: 4 blue 运行方式:4蓝色

run by: 0 red 运行方式:0红色

run by: 3 green 运行:3绿色

synchronized released by: 1 orange 同步发布者:1个橙色

synchronized accupied by: 3 green 同步据称:3绿色

synchronized released by: 3 green 同步发布者:3绿色

synchronized accupied by: 0 red 同步计算者:0红色

synchronized released by: 0 red 同步发布者:0红色

synchronized accupied by: 4 blue 同步据称:4蓝色

synchronized released by: 4 blue 同步发布者:4蓝色

synchronized accupied by: 2 yellow 同步据称:2黄色

synchronized released by: 2 yellow 同步发布者:2黄色

as we see the order of thread execution is random.. thats ok.. 1 2 4 0 3 如我们所见,线程执行的顺序是随机的..那没关系.. 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.... 所以1得到同步块,并且在它结束3(最后一个)之后得到0,然后是4和2。

How to make that order reverse,,, that after 1 it must be 2 then 4 then 0 then 3 如何使该顺序反转,即在1之后必须为2,然后为4,然后为0,然后为3

如果要公平访问同步块,则应使用ReentrantLock

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. 然后再做一次,直到时间/顺序正确为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM