views:

144

answers:

5

Hi,

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++;
    }
A: 

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

michelemarcon
A: 

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.

Michael Borgwardt
No multi-threading involved. For now. All of the methods you see are being used in other methods. Only the "quant" variable is unique to this method, and its a simple primitive integer type.
Daniel
What is especially puzzling about it is that it works just fine when I run debugger. This is completely illogical to me.
Daniel
Well, if the code ever runs multi-threaded then that variable WILL become a problem; it doesn't matter if it's "unique" to a method, what matters is that it will be accessed by multiple threads, and without synchronization even "quant--" will not work correctly.
Michael Borgwardt
I'm aware of that. I'm familiar with race conditions and locking mechanisms. That won't be a problem. This is.
Daniel
+1  A: 

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.

Michael Borgwardt
A: 

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.

matt b
A: 

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.

JRL