如何使用Java在RoundRobin调度中将一个线程更改为另一个线程

时间:2015-10-25 11:38:14

标签: java scheduling round-robin

给出了文件,我应该只编辑RoundRobinScheduler类中的BLOCK#2部分。 我试图使用索引来安排每个线程。它按顺序成功运行,但无法从一个线程切换到另一个线程作为“RoundRobin”调度。 Thread是否有任何可以让线程进入就绪状态的函数,并运行下一个线程?

以下是课程:

package operating_system;

class TestScheduler extends Thread {

    public static void main(String [] args) {
        Scheduler mySched =  new RoundRobinScheduler();
                //new RandomScheduler() ;
        // or new RoundRobinScheduler() - when written
        // or new PriorityScheduler()  - when written
    Thread p1 = new SimProc(mySched, "Alan", 4) ;
    Thread p2 = new SimProc(mySched, "Brian", 4) ;
    Thread p3 = new SimProc(mySched, "Charles", 4) ; 
    Thread p4 = new SimProc(mySched, "Dave", 4) ;
    Thread p5 = new SimProc(mySched, "Edward", 4) ;
    Thread p6 = new SimProc(mySched, "Francis", 4) ;
    mySched.start() ;
    }

}
package operating_system;

import java.util.Random ;

class SimProc extends Thread {

    private Scheduler mySched ; 
    private int count ;
    private static Random threadSeed = new Random() ;

    private static long getSeed() {
        return 1 + threadSeed.nextLong() ; 
    }

    public SimProc(Scheduler sch, String name, int i) {
        super(name) ;
        mySched = sch ;
        count = i ;
        synchronized (this) { 
            start() ;
            try {
                wait() ; // for process to be ready
            } catch (InterruptedException e) {}
        }
    }

    public int getCount() {
       return count ;
    }

    private int collatz(int i) {
        int ans = 0 ;
        while (i != 1) {
            if (i <= 0) return -1 ; // numbers get too big!
            ans++ ;
            if (i % 2 == 0) i /= 2 ;
            else i = i*3 + 1 ;
        }
        return ans ;
    }

    private void addToReadyQueue() {
        mySched.addProcess(this) ;
        synchronized (this) {
            notify() ; // so constructor completes
            try { 
                wait() ; // so process is waiting when scheduler starts
            } catch (InterruptedException e) {}
        }
    }

    private void removeFromReadyQueue() {
        mySched.removeProcess(this) ;
        mySched.endSlice(this) ; // want something else to run!
    }

    public void run() {
        addToReadyQueue() ;
        Random myrand = new Random(getSeed()) ;
        while(count > 0) {
            int k = Math.abs(myrand.nextInt()) ;
            int ans = collatz(k) ;
            if (ans > 0) count-- ;
            System.out.println("This is " + getName() + " - Collatz of "
            + k + " is " + ans) ;
            mySched.endSlice(this) ;

        }
        removeFromReadyQueue() ;
    }
}
package operating_system;

import java.util.ArrayList ;

abstract public class Scheduler extends Thread {

protected ArrayList <SimProc> processList ;
    public Scheduler() {
      processList = new ArrayList <SimProc> () ;
    }

   public synchronized void addProcess(SimProc t) {
      processList.add(t) ;
   }

   public void endSlice(SimProc t) {
        synchronized(t) {
            synchronized(this) { notify() ; }
        try { t.wait() ;}
        catch (Exception e) { 
        System.out.println("Unexpected interrupt in endSlice " + e) ;
        }
    }
   }    

   public synchronized void removeProcess(SimProc t) {
    int i = processList.indexOf(t) ;
    processList.remove(i) ;
   }
}
package operating_system;

import java.util.Random ;

public class RoundRobinScheduler extends Scheduler {

private Random randGen ;

    public RoundRobinScheduler() {
         randGen = new Random() ;
    }

    public void removeProcess( int index ){
         processList.remove(index);
    }

    public synchronized void run() {
    while (true) {
     //BLOCK 1
        int noProcs = processList.size() ;
        if (noProcs == 0) System.exit(0) ;
     //BLOCK 2
        int curProcs = 0;
        int next;
        if ( curProcs == noProcs )
                next = 0;
        else next = curProcs++;
        SimProc nextRunner = processList.get( next );
        synchronized( nextRunner ){ nextRunner.notify();}
     //BLOCK 3
        try { wait() ;}
        catch (Exception e) { 
        System.out.println("Unexpected interrupt in run " + e) ; 
        }
        //removeProcess( curProcs );
    }
}
}

1 个答案:

答案 0 :(得分:0)

已使用Scheduler.nextSlice()synchronized( nextRunner ){ nextRunner.notify();}部分实现了线程切换。 缺少的是你实际上是第一次启动SimProc对象:

SimProc nextRunner = processList.get( next );
if(nextRunner.isAlive()) {
  synchronized( nextRunner ){ nextRunner.notify();}
} else {
  nextRunner.start();
}