简体   繁体   English

JAVA:如何模拟并发

[英]JAVA: How to simulate concurrency

I have to create an application which simulates concurrent threads. 我必须创建一个模拟并发线程的应用程序。 The "server" creates a number of "threads" and stores them in a queue. “服务器”创建多个“线程”并将它们存储在队列中。 Each "thread" has defined a time to finish his execution. 每个“线程”都定义了完成执行的时间。 The server polls each "thread" from the queue to do his job for 10 ms. 服务器从队列中轮询每个“线程”以完成其工作10毫秒。 If the thread has finished his job, it is removed from the queue. 如果线程已完成其工作,则将其从队列中删除。 If not, it is added at the end of the queue. 如果不是,则将其添加到队列的末尾。 I used for this application PriorityQueue. 我为此应用程序使用了PriorityQueue。 The problem is that the code i wrote is not giving the expected output; 问题是我写的代码没有给出预期的输出; a "thread" is executed until his execution time ends. 执行“线程”直到执行时间结束。 How can I solve this problem? 我怎么解决这个问题?

SimulatedThread class SimulatedThread类

public class SimulatedThread {

    private int executionTime;
    private Integer id;
    private int executedTime;
    private boolean finished;

    public SimulatedThread(){
       executedTime = 0;
       executionTime = 0;
       setFinished(false);
    }
    //getters and setters
}

Server class 服务器类

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Random;

public class Server {

final int TIME = 10;
final int TH_NO = 10;

//priority
final int MIN_P = 1;
final int MAX_P = 100;

//execution time
final int MIN_E = 10;
final int MAX_E = 100;

private PriorityQueue<SimulatedThread> activeThreads;



public Server() {
    Comparator<SimulatedThread> comparator = new SimulatedThreadComparator();
    activeThreads = new PriorityQueue<SimulatedThread>(10,comparator);

}

public void createThreads(){
    for( int i = 0; i < TH_NO; i++){
        SimulatedThread th = new SimulatedThread();
        th.setExecutionTime(generator(MAX_E, MIN_E));
        th.setId(generator(MAX_P,MIN_P));
        System.out.println("New thread has been created");
        System.out.println(th.toString());          
        activeThreads.add(th);
    }
}

public void executeThreads(){

    while(!activeThreads.isEmpty()){
        SimulatedThread th = activeThreads.poll();  
        if(!th.isFinished()){

            try {
                Thread.sleep(TIME);
                th.setExecutedTime(th.getExecutedTime() + TIME);
                System.out.println(th.toString());
                if((th.getExecutionTime() - th.getExecutedTime()) <= 0 ){
                    th.setFinished(true);                       
                } else{
                    activeThreads.add(th);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    activeThreads.removeAll(activeThreads);
}

private int generator(int max, int min){
    Random rand = new Random();
    return rand.nextInt((max - min) + 1) - min;
}


public static void main(String[] args){
    Server s = new Server();
    s.createThreads();
    s.executeThreads();
}

EDIT 编辑

SimulatedThreadComparator class SimulatedThreadComparator类

import java.util.Comparator;

public class SimulatedThreadComparator implements Comparator<SimulatedThread> {

    @Override
    public int compare(SimulatedThread o1, SimulatedThread o2) {

        return o1.getId().compareTo(o2.getId());
    }
}

That explains it: since the priority is the ID, the last thread that was activated will return to the head of the queue, and be the next activated, until it's finished. 这就说明了这一点:因为优先级是ID,所以最后一个被激活的线程将返回队列的开头,并成为下一个被激活的线程,直到完成为止。

Perhaps you should compare the executed time instead of the ID? 也许您应该比较执行时间而不是ID?

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

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