Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Shortest Seek-Time First Algorithm with java.util.concurrent

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Shortest Seek-Time First Algorithm with java.util.concurrent

    Hello!
    I'm having an assignment to simulate SSTF (shortest seek time first) using Java concurrency. Here is my code.
    Can someone look at the code and tell me how to correct the waitTime for each process. As you'll see all the processes are making a request to the manager for a hdd sector (0 to 999999), which is in an actual cylinder (let's say sector 344555 is on cylinder 345). Each cylinder has 1000 sectors and there are 1000 cylinders in the hdd. The hdd implementation is not an issue here, it's more important to implement the threads in a correct way.
    The manager thread needs to satisfy only one process request at a time. That's why i have put queue.poll() in a protected area. The manager thread's sleepTime should be the movement of the head of the hdd (initially on sector 0) to the nearest requested sector (let's say sector 123445 which is on cylinder 124, so the manager sleeps 124 ms to satisfy this request). I hope you understood me just a little bit.

    You can check out the SLEEPING BARBER PROBLEM, it's something similar regarding the thread implementation (one thread manager/barber, n threads processes/clients).

    MAIN CLASS
    package test;

    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class Main {
    public static void main(String[] args) {
    Manager manager = new Manager();
    Process processes[] = new Process[10];

    for(int i = 0; i < processes.length; i++){
    processes[i] = new Process(manager, i);
    processes[i].start();
    }
    manager.start();

    try {
    for(int i=0; i<processes.length; i++)
    processes[i].join();
    manager.join();
    } catch(InterruptedException e) {
    Logger.getLogger(Main.class.getName()).log(Level.S EVERE, null, e);
    }

    System.out.println(manager.out);
    for(int i = 0; i < processes.length; i++){
    System.out.println("Wtime\t"+processes[i].getName()+"\t"+manager.waitTime);
    }
    System.out.println("...END...");
    System.exit(0);
    }
    }
    MANAGER CLASS
    package test;

    import java.util.concurrent.locks.*;
    import java.util.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class Manager extends Thread {
    private int seekTime = 1;
    private int waitingProcesses = 0;
    private int maxTasks = Process.PROCESSI;
    private Lock lockVar = new ReentrantLock();
    private Lock lockOut = new ReentrantLock();
    private PriorityQueue<Process> queue =
    new PriorityQueue<Process>(Process.PROCESSI,
    new Comparator<Process>() {
    public int compare(Process a, Process b) {
    return a.compareTo(b);
    }
    }
    );
    public String out = "";
    public long waitTime = 0;
    public long reqTime =0;


    public Manager() {
    super("Hard-Disk Manager: ");
    }

    private void printOut(String s){
    this.lockOut.lock();
    this.out = this.out + "\n" + s;
    this.lockOut.unlock();
    }

    public void queueProcess(Process c){
    this.lockVar.lock();
    this.reqTime = System.currentTimeMillis();
    if(this.waitingProcesses < this.maxTasks){

    this.printOut(c.getName() + "requests access to cylinder C_" +
    c.getCylinder() + " ("
    + c.getSector() + " sector)");
    this.waitingProcesses++;
    this.lockVar.unlock();
    // try {
    this.queue.offer(c);
    //Thread.sleep(50);
    // } catch(InterruptedException e){
    // Logger.getLogger(Process.class.getName()).log(Leve l.SEVERE, null, e);
    //}
    }
    }

    public boolean isAvailable(){
    this.lockVar.lock();
    try {
    return this.maxTasks - this.waitingProcesses > 0 ? true : false;
    } finally {
    this.lockVar.unlock();
    }
    }

    public void run(){
    while(this.waitingProcesses > 0) {
    try {


    this.lockVar.lock();
    this.waitTime+=System.currentTimeMillis()-this.reqTime;
    Process c = (Process)this.queue.poll();
    this.printOut(getName() + "processing " + c.getName() + " for C_" + c.getCylinder());

    this.waitingProcesses--;
    this.maxTasks--;
    this.lockVar.unlock();

    Thread.sleep(this.seekTime);

    } catch(InterruptedException e) {
    Logger.getLogger(Manager.class.getName()).log(Leve l.SEVERE, null, e);
    }
    this.printOut("...waiting processes: " + this.waitingProcesses);
    }
    }
    }
    PROCESS CLASS
    package test;

    import java.util.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class Process extends Thread implements Comparable<Process> {
    public final static int PROCESSI = 10;
    private Manager manager;
    private int settore;
    private int index;



    public Process(Manager m, int ind){
    super("P_" + ind);
    this.manager = m;
    this.index = ind;
    }

    public int compareTo(Process c) {
    return this.getSector()-c.getSector();
    }

    public int getIndex() {
    return this.index;
    }

    public int getSector() {
    return this.settore;
    }

    public void setSettore(int newSettore) {
    settore = newSettore;
    }

    public int getCylinder() {
    return (this.settore/1000)+1;
    }

    public void run(){
    Random rnd = new Random();
    int random = rnd.nextInt(999999);

    this.setSettore(random);
    while(this.manager.isAvailable()){

    this.manager.queueProcess(this);
    // try {

    // Thread.sleep(50);
    // } catch (InterruptedException e) {
    // Logger.getLogger(Process.class.getName()).log(Leve l.SEVERE, null, e);
    // }
    }
    }
    }
    Last edited by SAiNT_JiMMiE; May 11th, 2010 at 03:56 AM.


Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 2
    Last Post: May 17th, 2014, 01:16 AM
  2. Replies: 3
    Last Post: April 26th, 2011, 02:51 AM
  3. Java Concurrent Programming
    By vzwsudha in forum Threads
    Replies: 6
    Last Post: May 27th, 2010, 04:26 AM
  4. java.util.* and visual studio 2005
    By natalie in forum Java Theory & Questions
    Replies: 0
    Last Post: February 26th, 2010, 10:34 AM
  5. Replies: 4
    Last Post: April 29th, 2009, 10:53 AM