java模拟多处理器调度

import java.util.LinkedList;
import java.util.Random;

public class ThreadDispatch extends Thread{

//需要暂用几个时间片
private int count;
//是否分配到了处理器标志
private volatile boolean holdCpu;
//暂用的处理器名称
private volatile String cpuName;

public ThreadDispatch(int count,String name) {
super(name);
this.count = count;
}

public void setCount(int count) {
this.count = count;
}

public void setHoldCpu(boolean holdCpu) {
this.holdCpu = holdCpu;
}

public void setCpuName(String cpuName) {
this.cpuName = cpuName;
}
//线程是否执行任务完毕,就是需要预先规定的需要得到时间片是否都得到了。
private volatile boolean finish = false;
//标志线程是否正在运行
public volatile boolean runing ;
public void run() {
for (int i = 0; i < count; i++) {
synchronized (this) {

while(!holdCpu){
//在没有得到处理器,则等待分配到处理器
//因为怕存在虚假唤醒,故在循环中等待。
//一直运行到失去处理器为止
System.out.println(Thread.currentThread().getName()
+ "准备就绪,需要处理器支援。。。。" + cpuName);
try {
//表示线程没有运行
runing=false;
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
//表示线程得到了处理器,设置标志位为true  表示线程在运行
runing=true;
for (;;) {
if (!holdCpu){
break;
}
System.out.println(Thread.currentThread().getName()
+ "正在暂用处理器。。。。" + cpuName);
}
}
//当出了循环,标志着finish
finish = true;
runing=false;
}

public static void main(String[] args) throws InterruptedException {
LinkedList<ThreadDispatch> threads = new LinkedList<ThreadDispatch>();
Random random = new Random();
for (int i = 0; i < 10; i++) {
threads.add(new ThreadDispatch(2,"pdy执行线程: "+i));
}
for (int i = 0; i < 10; i++) {
threads.get(i).start();
}


String[] CPUName = {"超人CPU","模型CPU","intl CPU"};
//10毫秒time片
final int cputime = 5;
IDEL idel = new IDEL(threads);
for (int i = 0; i < CPUName.length; i++) {

new CPU(cputime, idel, CPUName[i]).start();;
}
synchronized (ThreadDispatch.class) {
ThreadDispatch.class.wait();
}
}

public static class CPU extends Thread{

private final int cputime;
private final int ideltime = 10;
private final IDEL idel;
private final String cpuName;
public CPU(int cputime,IDEL idel, String cpuName) {
super();
this.cputime = cputime;
this.idel = idel;
this.cpuName = cpuName;
}

private ThreadDispatch dispatch;
@Override
public void run() {

while(true){
//处理器将会一直运行着。
//转调度处理程序,调出当前执行的task,换入新处理task
dispatch = idel.calloutAndnextThreadDispatch(dispatch);
System.out.println(dispatch);
if(dispatch == null){
//调度的新任务没有,则处理器一直空转着。
try {
System.out.println("处理器。。。。 " + cpuName + "  正在空转。。    没有干的");
Thread.sleep(ideltime);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue ;
}
//有新任务,则开始执行。
dispatch.cpuName=cpuName;
dispatch.holdCpu=true;
synchronized (dispatch) {
dispatch.notify();
}
try {
Thread.sleep(cputime);
} catch (InterruptedException e) {
e.printStackTrace();
}

dispatch.holdCpu=false;
while(dispatch.runing){
}
dispatch.cpuName=null;
}
}
}

/**
* 线程调度程序
* 按照时间片轮转
* @author Administrator
*
*/
public static class IDEL {
LinkedList<ThreadDispatch> threads ;

public IDEL(LinkedList<ThreadDispatch> threads) {
this.threads = threads;
}
public synchronized ThreadDispatch calloutAndnextThreadDispatch(ThreadDispatch dispatch){

if(dispatch != null)
callout(dispatch);

return threads.size() > 0 ? threads.removeFirst():null;
}
private void callout(ThreadDispatch dispatch) {

threads.remove(dispatch);
if(dispatch.isNotFinish()){
threads.addLast(dispatch);
}
}
}

public boolean isNotFinish() {
return !finish;
}

}

猜你喜欢

转载自1064319393.iteye.com/blog/2389567