⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tailstrategypriority.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
字号:
/**    
  * Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano

  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.

  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
  
package jmt.engine.NetStrategies.QueuePutStrategies;

import jmt.common.exception.NetException;
import jmt.engine.NetStrategies.QueuePutStrategy;
import jmt.engine.QueueNet.*;

import java.util.LinkedList;
import java.util.ListIterator;


/**
 * This class implements a specific queue put strategy: all arriving jobs
 * are ordered according JobClass priorities. Between jobs of the same
 * priority level, the last come is put at the end.
 * @author Stefano Omini
 */
public class TailStrategyPriority extends QueuePutStrategy {

	/**
     * This method should be overridden to implement a specific job strategy.
	 * @param job job to be added to the queue.
     * @param queue queue.
     * @param sourceSection job source section.
     * @param sourceNode job source node.
     * @param callingSection The section which calls this strategy.
     */
	public void put(Job job, JobInfoList queue, byte sourceSection,
	                NetNode sourceNode, NodeSection callingSection)
	        throws NetException {

        //priority of this job
        int priority = job.getJobClass().getPriority();

        //list of jobs in queue
        LinkedList list = (LinkedList) queue.getJobList();

        if (list.size() == 0) {
            //empty list: add first
            queue.addFirst(new JobInfo(job), true);
            return;
        }

        //else creates an iterator and find the correct position
        //according to the job priority

        ListIterator iterator = list.listIterator();
        JobInfo current = null;
        int currentJobPriority = 0;
        int index = -1;

        //iterator starts from the first (i.e. the job with highest priority)
        while (iterator.hasNext()) {
            index++;
			current = (JobInfo) iterator.next();
            currentJobPriority = current.getJob().getJobClass().getPriority();

			if (priority > currentJobPriority) {
                //the job to be added must be inserted before the current job
                //(because has greater priority)

                //index is the position of the current element, which will be shifted together
                //with the following ones
                queue.add(index, new JobInfo(job), true);
                return;
            }
            //else if priority is lower than current, continue iteration
		}

        //exiting from the "while" means that the job to be inserted is the job with
        //lowest priority or has equal priority to the current last job but it is the last come

        //add last
        queue.addLast(new JobInfo(job), true);
        return;

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -