Daar
Daar

Reputation: 3555

Print methods not called when running, called when debugging

I created a class with four methods. I inserted print statements into them, so I could see if they work properly. There is no problem with the first three. But when I call the fourth method nothing prints out. Strangely when I initiate debugger and move through the method step by step, the statements are called(and output printed). How can this be?

Thanks in advance.

Method in question:

   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(" ");
   }

}

Code calling it:

while(true){

        algorithm(algorithm,s,counter);

        counter++;
    }

Upvotes: 0

Views: 234

Answers (5)

JRL
JRL

Reputation: 77995

Well if your active variable is null all the time you'll only ever print " " so you might not notice it in your Console output. It could be a timing issue that makes active not be null when you run in debug.

Upvotes: 0

matt b
matt b

Reputation: 139921

When you "run" the code, how are you doing so? Is it "run" within some sort of server or container in which the standard output might be re-directed? This is what I would suspect.

Using System.out for debugging and troubleshooting like this can be troublesome and is (in general) a pretty bad practice - it's best to use a log framework, such as log4j. This way you don't have to guess at where the log output is being sent.

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346270

It doesn't look like there's an obvious answer, and without the rest of the project we can't reproduce and analyze the problem.

The way to solve such unexplainable behaviour is to reduce the code as far as possible while preserving said behaviour, i.e. take away all code that doesn't seem to be related to it, see if the problem is still there; if it's not, add some of the code back, etc. Eventually you should be able to pinpoint a change that causes the problem and deduce what actually happens from there.

Upvotes: 1

Michael Borgwardt
Michael Borgwardt

Reputation: 346270

I can't say anything definitive without seeing the rest of the class, and maybe some of the other classes it uses, but it looks like it might be a race condition, i.e. multiple threads interfering with each other in an unpredictable fashion. Especially the multiple use of Schedulers.quant, apparently an unsynchronized static variable, looks extremely suspicious.

If it is indeed a race condition, you need to read this book and then come back and fix your code; multithreading issues can be very complex, and you really, really need to understand the theory before writing multithreaded code.

Upvotes: 0

michelemarcon
michelemarcon

Reputation: 24767

There are a lot of reasons for this to happen. However, without further information, is almost impossible to help you.

Upvotes: 0

Related Questions