java simulates multiprocessor scheduling

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

public class ThreadDispatch extends Thread{

//A few time slices need to be used temporarily
private int count;
//Whether the processor flag is allocated
private volatile boolean holdCpu;
//Temporary use The name of the processor
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;
}
//Whether the thread has finished executing the task, that is, whether all the time slices that need to be obtained in advance are obtained.
private volatile boolean finish = false;
//sign whether the thread is running
public volatile boolean running ;
public void run() {
for (int i = 0; i < count; i++) {
synchronized (this) {

while(!holdCpu){
//If the processor is not obtained, wait for the processor to be allocated.
//Because of the fear of false wake-up, it waits in a loop.
//Run until the processor is lost
System.out.println(Thread.currentThread().getName()
+ "Ready, needs processor support..." + cpuName);
try {
//Indicates that the thread is not running
running=false;
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
//Indicates that the thread has got the processor, setting the flag to true means the thread is running
running=true;
for (;;) {
if (!holdCpu){
break;
}
System.out.println(Thread.currentThread().getName ()
+ "Temporarily using the processor..." + cpuName);
}
}
//When the loop is out, it marks finish
finish = true;
running=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 executes threads: "+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){
//The processor will keep running.
//Transfer to the scheduling handler, call out the currently executing task, and switch in the new processing task
dispatch = idel.calloutAndnextThreadDispatch(dispatch);
System.out.println(dispatch);
if(dispatch == null){
//Scheduled new If there is no task, the processor has been idling.
try {
System.out.println("Processor..." + cpuName + " is idling.. not doing anything");
Thread.sleep(ideltime);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue ;
}
//A new task starts executing.
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;
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326272477&siteId=291194637