Printing methods not called at startup, called during debugging
I created a class with four methods. I pasted print instructions in them so I could see if they were working correctly. There are no problems in the first three. But when I call the fourth method, nothing is printed. Oddly enough, when I start the debugger and step through the method step by step, the expressions are called (and the output is output). How can it be?
Thanks in advance.
Method used:
public void robin(int counter, int quant, int penalty) {
if(Schedulers.quant==-1) {
Schedulers.quant=quant;
}
while(p!=null && p.getArrival()==counter) {
qrobin.add(p);
if(i.hasNext())
p=i.next();
else {
p=null;
break;
}
}
if(active!=null) {
if(active.getLeftOver()>0 && Schedulers.quant>0) {
active.decreaseLeftOver();
Schedulers.quant--;
System.out.print(active.getPID());
}
else if(active.getLeftOver()>0 && Schedulers.quant==0) {
qrobin.add(active);
active=qrobin.poll();
Schedulers.quant=quant;
Schedulers.quant--;
if(active!=null) {
System.out.print(active.getPID());
active.decreaseLeftOver();
}
else
System.out.print(" ");
}
else {
active=qrobin.poll();
Schedulers.quant=quant;
Schedulers.quant--;
if(active!=null) {
System.out.print(active.getPID());
active.decreaseLeftOver();
}
else
System.out.print(" ");
}
}
else {
active=qrobin.poll();
Schedulers.quant=quant;
Schedulers.quant--;
if(active!=null) {
System.out.print(active.getPID());
active.decreaseLeftOver();
}
else
System.out.print(" ");
}
}
The code that calls it:
while(true){
algorithm(algorithm,s,counter);
counter++;
}
source to share
This does not seem like an obvious answer, and without the rest of the project, we cannot reproduce and analyze the problem.
The way to deal with this unexplained behavior is to shorten the code as much as possible while retaining the specified behavior, that is, take away all the code that does not appear to be related to it, see if the problem still exists; if not, add some code back, etc. Eventually you will have to identify what is causing the problem and infer what is actually happening from there.
source to share
I can't say anything definitive without seeing the rest of the class and possibly some of the other classes it uses, but it looks like it could be a race condition, that is, multiple threads interfering with each other in unpredictable ways, especially reusability Schedulers.quant
, by - the apparently unsynchronized static variable looks extremely suspicious.
If it really is a race condition, you need to read this book and then go back and fix your code; multithreading problems can be very complex and you really need to understand the theory before writing multithreaded code.
source to share
When you "run" the code, how do you do it? Is it "running" on some server or container where stdout can be redirected? This is what I suspect.
Using it System.out
for debugging and troubleshooting like this can be problematic and is generally pretty bad practice - it's best to use a log structure like log4j . This way you don't have to guess where the log output is being sent.
source to share