JAVA:如何模拟并发

JAVA:如何模拟并发,java,Java,我必须创建一个模拟并发线程的应用程序。“服务器”创建许多“线程”,并将它们存储在队列中。每个“线程”都定义了完成其执行的时间。服务器轮询队列中的每个“线程”以完成其工作10毫秒。如果该线程已完成其工作,则该线程将从队列中删除。如果不是,则将其添加到队列的末尾。我用于此应用程序的优先级队列。问题是我写的代码没有给出预期的输出;执行一个“线程”,直到其执行时间结束。 我怎样才能解决这个问题 模拟线程类 public class SimulatedThread { private int e

我必须创建一个模拟并发线程的应用程序。“服务器”创建许多“线程”,并将它们存储在队列中。每个“线程”都定义了完成其执行的时间。服务器轮询队列中的每个“线程”以完成其工作10毫秒。如果该线程已完成其工作,则该线程将从队列中删除。如果不是,则将其添加到队列的末尾。我用于此应用程序的优先级队列。问题是我写的代码没有给出预期的输出;执行一个“线程”,直到其执行时间结束。 我怎样才能解决这个问题

模拟线程类

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
}
服务器类

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();
}
import java.util.Comparator;
导入java.util.PriorityQueue;
导入java.util.Random;
公共类服务器{
最终整数时间=10;
最终int THU NO=10;
//优先权
最终整数最小值P=1;
最终整数最大值P=100;
//执行时间
最终整数最小值=10;
最终整数最大值=100;
私有优先队列活动线程;
公共服务器(){
比较器比较器=新的模拟线程比较器();
activeThreads=new PriorityQueue(10,比较器);
}
public void createThreads(){
对于(int i=0;i如果((th.getExecutionTime()-th.getExecutedTime()),说明了这一点:由于优先级是ID,最后一个被激活的线程将返回到队列的头部,并成为下一个被激活的线程,直到它完成


也许您应该比较执行时间而不是ID?

首先,为什么要使用PriorityQueue?您可以使用linkedList,其次,您如何知道“线程”一直执行到其执行时间结束。@Mifmif每次执行“线程”执行时,执行时间将随着执行时间的增加而增加。@mif若要模拟链表中的优先级,请选择SimulatedThreadComparator类代码help@MauricePerry我在问题中添加了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());
    }
}