Java e例外(e){ } //我们的环境是最好的 试一试{ if((this.packet=this.lowPrioQueue.nextPacket())!=null){ this.writeBytes(this.packet+'\n'); log0.write(“\nScheduler:Paquet[“+packet+”]enviat desde la cua Low\n”); log1.写入(“\n调度程序:Paquet[“+packet+”]enviat desde la cua Low\n”); } }捕获(IOE异常){ } } } } } }
我不确定的是,当run方法当前正在运行时,是否可以调用scheduler中的方法。如果发生这种情况,就会出现错误Java e例外(e){ } //我们的环境是最好的 试一试{ if((this.packet=this.lowPrioQueue.nextPacket())!=null){ this.writeBytes(this.packet+'\n'); log0.write(“\nScheduler:Paquet[“+packet+”]enviat desde la cua Low\n”); log1.写入(“\n调度程序:Paquet[“+packet+”]enviat desde la cua Low\n”); } }捕获(IOE异常){ } } } } } },java,multithreading,Java,Multithreading,我不确定的是,当run方法当前正在运行时,是否可以调用scheduler中的方法。如果发生这种情况,就会出现错误 谢谢大家! 如何执行计划终止?如果它测试一个布尔值,这个布尔值是易失的还是有某种形式的同步?如果你不能使用通常的等待/通知技术,那么你可能需要一个易失变量。不,它不是易失的,这可能是错误。但是有没有办法用wait()/notify()语句来实现呢?我的意思是使用while(true)持续检查变量是没有效率的。必须使用监视器,但我不知道调度程序如何通知缓冲容器。有什么想法吗?有很多事情
谢谢大家! 如何执行
计划终止
?如果它测试一个布尔值,这个布尔值是易失的还是有某种形式的同步?如果你不能使用通常的等待/通知技术,那么你可能需要一个易失变量。不,它不是易失的,这可能是错误。但是有没有办法用wait()/notify()语句来实现呢?我的意思是使用while(true)持续检查变量是没有效率的。必须使用监视器,但我不知道调度程序如何通知缓冲容器。有什么想法吗?有很多事情可能会出错。您确定两个队列都调用了setBufferTerminated(n)
?如果是,能否在Scheduler#run()
方法的末尾添加一个println
,以检查它是否真的结束了?你能对缓冲器做同样的处理并告诉我们你观察到了什么吗?可能其中一个线程被困在等待从队列中获取某些内容,或者它只是在执行这个.th.sleep()
太长时间?谢谢大家的帮助。我做了rodion告诉我的事情,发现从未从缓冲区调用setBufferTerminated方法。这是由于一段时间(正确)没有正确终止。我已经纠正了它,它的工作!
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferContainer {
private Buffer buffer0;
private Buffer buffer1;
private Scheduler scheduler;
private Queue[] queue;
private Queue q1;
public BufferContainer (int inPort0, int outPort, long queueSize0, double serviceRate0,
int inPort1, long queueSize1, double serviceRate1){
try {
/*Some code here*/
//Scheduler separated thread
this.scheduler = new Scheduler(this.queue, serviceRate0,
serviceRate1, outPort, outLog0, outLog1);
//start the scheduler
this.scheduler.start();
//Buffers separated threads
this.buffer0 = new Buffer(inPort0, queueSize0, this.queue[0], outLog0);
this.buffer1 = new Buffer(inPort1, queueSize1, this.queue[1], outLog1);
//start the buffers
this.buffer0.start();
this.buffer1.start();
/*Some code here*/
while (true){
if(this.scheduler.schedulerTerminated()){
//Some code here
this.scheduler.stop();
break;
}
/*Some code here*/
}
}
//stop Buffers
this.buffer0.stop();
this.buffer1.stop();
/*Some code here*/
} catch (IOException ex) {
}
}
public static void main(String[] args){
BufferContainer buf = new BufferContainer(Integer.parseInt(args[0]), Integer.parseInt(args[1]),
Long.parseLong(args[2]), Double.parseDouble(args[3]), Integer.parseInt(args[4]),
Long.parseLong(args[5]), Double.parseDouble(args[6]));
}
}
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Random;
public class Scheduler implements Runnable{
Thread th;
private double serviceRateHigh, serviceRateLow;
private DataOutputStream wr;
private BufferedWriter log0, log1;
private Socket socket;
private String packet;
private Queue highPrioQueue, lowPrioQueue;
//Es possible que es mdifiqui per diferents threads
private volatile boolean shouldStop, schedulerTerminated, buffer0Terminated, buffer1Terminated;
public Scheduler(Queue[] queue, double serviceRateHigh, double serviceRateLow,
int port, BufferedWriter log0, BufferedWriter log1){
//Creem el thread
this.th = new Thread(this);
//Guardem les dues cues
this.setQueues(queue);
this.log0 = log0;
this.log1 = log1;
//Guardem els dos service rate
this.serviceRateHigh = serviceRateHigh;
this.serviceRateLow = serviceRateLow;
this.shouldStop = false;
this.schedulerTerminated = false;
this.buffer0Terminated = false;
this.buffer1Terminated = false;
//Socket y buffer de escriptura
try {
this.socket = new Socket(InetAddress.getByName("127.0.0.1"), port);
this.wr = new DataOutputStream(this.socket.getOutputStream());
} catch (IOException e) {
}
}
//Iniciar el thread
public void start(){
this.th.start();
}
//Parar el thread
public void stop(){
this.shouldStop = true;
}
//When both buffer have notified we can terminate the scheduler
public synchronized boolean schedulerTerminated(){
if (this.buffer0Terminated && this.buffer1Terminated)
this.schedulerTerminated = true;
return this.schedulerTerminated;
}
public synchronized void setBufferTerminated(int n){
if(n == 0) this.buffer0Terminated = true;
if(n == 1) this.buffer1Terminated = true;
}
//Assigna les cues de alta i baixa prioritat
private void setQueues(Queue[] queue){
char prio;
for (int i = 0; i < queue.length; i++){
prio = queue[i].getPrio();
if (prio == 'H')
this.highPrioQueue = queue[i];
else{
if (prio == 'L')
this.lowPrioQueue = queue[i];
}
}
}
//Notifica al thread per a que es desperti
synchronized public void resume(){
notify();
}
protected void sendLastPacketNumberToSink(int pl0, int pl1) {
String datagram = "BYE" + " " + pl0 + " " + pl1;
try {
wr.writeBytes(datagram + '\n');
}catch (IOException e) {}
}
//Proces principal del thread
@Override
public void run(){
while(!shouldStop){
//Creem un generador de nombres aleatoris
Random ran = new Random();
//generem un nombre aleatori entre 0 i 1
double uniform = ran.nextDouble();
if(!this.highPrioQueue.isEmpty()){ //Cua de alta prioritat NO buida
//convertim a exponencial
double sleeptime = Math.log(1-uniform)/(-this.serviceRateHigh);
try{
this.th.sleep(Math.round(sleeptime*1000));
}catch(InterruptedException e){
}
//Enviem el paquet de la cua de alta prioritat
try {
if ((this.packet = this.highPrioQueue.nextPacket()) != null){
this.wr.writeBytes(this.packet + '\n');
log0.write("\nScheduler: Paquet [" + packet + "] enviat desde la cua high\n");
log1.write("\nScheduler: Paquet [" + packet + "] enviat desde la cua high\n");
}
}catch (IOException e) {
}
}
else {
if (!this.lowPrioQueue.isEmpty()){//Cua de alta prioritat buida
//convertim a exponencial
double sleeptime = Math.log(1-uniform)/(-this.serviceRateLow);
try{
this.th.sleep(Math.round(sleeptime*1000));
}catch(InterruptedException e){
}
//Enviem el paquet de la cua de baixa prioritat
try {
if ((this.packet = this.lowPrioQueue.nextPacket()) != null){
this.wr.writeBytes(this.packet + '\n');
log0.write("\nScheduler: Paquet [" + packet + "] enviat desde la cua Low\n");
log1.write("\nScheduler: Paquet [" + packet + "] enviat desde la cua Low\n");
}
}catch (IOException e) {
}
}
}
}
}
}