最短的剩余时间优先:Java多线程

aka*_*man 6 java simulation cpu multithreading scheduling

我试图在java中模拟CPU调度算法,并使用多线程.我已经成功实施了FCFS(先到先服务)和SJF(最短工作优先).但问题是当我开始考虑SRTF(最短剩余时间优先)时,这是SJF的先发制人形式.我使用以下模型:

  • CPU的一个线程,它有一个CLOCK变量,每个都保持滴答声(一个简单的时钟增量)100ms.我有一个boolean isAvailable;标志,用于在开始执行之前检查CPU是否可用的进程.
  • 长期调度程序(LTS)的一个线程,它将进程从进程列表推送到就绪队列.
  • 用于短期调度程序(STS)的线程,它从ReadyQueue获取进程并将其分配给CPU.
  • 一旦通过STS从ReadyQueue中删除进程以执行,该进程将检查isAvailableCPU 的标志.如果true,它将标志设置为false并开始执行(为此我只是让线程休眠,(100 * burstTime) ms因为这只是一个模拟).否则,进程只是忙着等待: while(CPU.isAvailable != true);.

我手头上有进程列表以及它们的到达和突发时间.在我模拟非抢占式调度(FCFS和SJF)之前是可以的.但是当我尝试SRTF时,我无法找到一种方法来抢占当前正在运行的进程线程.

对于SRTF,我知道从ReadyQueue中选择下一个流程的方法.我可以尝试将isAvailable标志设置为false从队列中选择一个进程,但是我怎么知道哪个线程最初在执行?由于我没有使用很多同步黑白线程,我将使用该CPU线程有多个进程.它有点搞砸了.请帮忙.谢谢!

这是进程的代码:

enum State {ARRIVED, WAITING, READY, RUNNING, EXECUTED}
public class Process implements Runnable
{
    int pid;
    int arrTime;
int burstTime;
int priority;
long startTime;
long endTime;
State procState = null;

Process(int pid, int arrTime, int burstTime, int priority)
{
    this.pid = pid;
    this.arrTime = arrTime;
    this.burstTime = burstTime;
    this.priority = priority;
    this.procState = State.ARRIVED;
    this.startTime = 0;


    this.endTime = 0;    /* I also considered adding a timeElapsedUnderExecution
 attribute to the process. So I can check after every cycle if the CPU is still available
 and keep incrementing the time elapsed. Once the timeElapsed becomes same as burstTime, i
 stop the process. Or if after a cycle, the CPU is not available, i know from where to
 resume my Process. Is this the way to go ? */

    }

boolean isReady()
{
    if((this.arrTime <= CPU.CLOCK) && (this.procState == State.ARRIVED))
        return true;
    else return false;
}

@Override
public void run() {
    // TODO Auto-generated method stub
    if(this.procState == State.READY)
        this.procState = State.WAITING;

    while(!CPU.isAvailable());

    try 
    {
        this.procState = State.RUNNING;
        System.out.println("Process " + pid + " executing...");
        this.startTime = CPU.CLOCK;
        System.out.println("Process " + this.pid + ": Begins at " + this.startTime);
        Thread.sleep(this.burstTime * 100);
        this.endTime = CPU.CLOCK;
        System.out.println("Process " + this.pid + ": Ends at " + this.endTime);
        this.procState = State.EXECUTED;

    }
    catch (InterruptedException e) 
    {
        // TODO Auto-generated catch block
        System.out.println("Interrupted: " + pid);
        e.printStackTrace();
    }
    }
}
Run Code Online (Sandbox Code Playgroud)

CPU的代码:

    import java.util.LinkedList;
    import java.util.Queue;

    public class CPU implements Runnable

{
    static Long CLOCK = new Long(0);
    static LinkedList<Process> ReadyQ = new LinkedList<Process>();
private static boolean isAvailable = true;
static boolean done = false;

public static boolean isAvailable() {
    return isAvailable;
}

public static void setAvailable(boolean isAvailable) {
    CPU.isAvailable = isAvailable;
}

static void incrementCLOCK()
{
    LTS.checkArrival();
    CPU.CLOCK++;
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Clock Tick: " + CPU.CLOCK);
}

@Override
public void run() {
    // TODO Auto-generated method stub
    System.out.println("CPU starts.!!!");
    while(CPU.done != true)
        synchronized(CPU.CLOCK)
        {
            incrementCLOCK();
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

LTS的代码:

public class LTS implements Runnable 
{
    private static Process[] pList = null;
    private final int NUM;
    static Integer procStarted;
    static Integer procFinished;
    static boolean STSDone = false;


LTS(Process[] pList, int num)
{
    this.NUM = num;
    LTS.pList = pList;
}

static void checkArrival()
{
    if(pList == null) return;
    for(int i = 0; i < pList.length; i++)
        if(pList[i].isReady())
        {
            pList[i].procState = State.READY;
            System.out.println("Process " + pList[i].pid + " is now ready.");
            CPU.ReadyQ.add(pList[i]);
        }
}

@Override
public void run() {
    // TODO Auto-generated method stub
    System.out.println("Long Term Scheduler starts.!!!");
    while(LTS.STSDone != true)
    {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    System.out.println(LTS.STSDone);
    System.out.println("LTS ends.!!!");
        CPU.done = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

jta*_*orn 0

第一个问题是您的共享状态不是线程安全的。即使像布尔值这样简单的东西也需要正确的线程原语来确保跨线程的可见性(也称为“易失性”)。

  • 恐怕这个主题太大,无法在这里详细阐述。很多人推荐阅读这本书:http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601 (3认同)