简体   繁体   中英

integer not incrementing in inner loop

a quick description: In a multithreaded loop an integer is not incrementing in the inner loop while it does in the outer one.

my code:

public void run() {
        if(maxH==actualmaxH)
            actualmaxH--;
        if(maxW==actualmaxW)
            actualmaxW--;
        for(;i<=actualmaxW; i++) {
            for (; j <=actualmaxH; j++) {
                Hit hit=world.hit(cam.rayFor(maxW,maxH,i,j));
                Color col;

                if(hit==null)
                    col = world.backgroundColor;
                else
                    col = hit.geo.material.colorFor(hit, world, 0);

                int rgb = ((int)(col.r*255)<< 16) | ((int)(col.g*255)<< 8) | ((int)(col.b*255));
                raster.setDataElements(i, maxH - j - 1, model.getDataElements(rgb, null));
            }

            if(i%30==0) {
                frame.update(g);
            }
        }
        frame.update(g);
    }

This bit of code is called from the constructor via "new Thread(this)"(this implements Runnable) and the constructor is called from another class with different i,j and actual.. the rest basically stays the same

Again, the problem is that in the outer loop the i gets incremented just fine, while in the inner loop its always 0(or of course the other starting numbers in the other threads)

I hope I was clear enough. Will answer questions to my code with pleasure if anything is unclear and hope for anyone to find a solution. Thanks anyway=)

EDIT: From where it is called:

Thread t1=new Thread(0,0, img.getWidth()/2, img.getHeight()/2, img.getWidth(), img.getHeight(), world, cam , raster, model, frame, g);
            Thread t2=new Thread(img.getWidth()/2+1, 0, img.getWidth(), img.getHeight()/2, img.getWidth(), img.getHeight(), world, cam , raster, model, frame, g);
            Thread t3=new Thread(0, img.getHeight()/2+1, img.getWidth()/2, img.getHeight(), img.getWidth(), img.getHeight(), world, cam , raster, model, frame, g);
            Thread t4=new Thread(img.getWidth()/2+1, img.getHeight()/2+1, img.getWidth(), img.getHeight(), img.getWidth(), img.getHeight(), world, cam , raster, model, frame, g);

And the Constructor to match:

java.lang.Thread t;
    int i;
    int j;
    int maxW;
    int maxH;
    int actualmaxW;
    int actualmaxH;
    World world;
    Camera cam;
    WritableRaster raster;
    ColorModel model;
    JFrame frame;
    Graphics g;

    public Thread(int i, int j, int actualmaxW, int actualmaxH, int maxW, int maxH, World world, Camera cam, WritableRaster raster, ColorModel model, JFrame frame, Graphics g){
        this.i=i;
        this.j=j;
        this.actualmaxW=actualmaxW;
        this.actualmaxH=actualmaxH;
        this.maxW=maxW;
        this.maxH=maxH;
        this.world=world;
        this.cam=cam;
        this.raster=raster;
        this.model=model;
        this.frame=frame;
        this.g=g;
        t=new java.lang.Thread(this);
        t.start();
    }

This seems to be the problem:

for(;i<=actualmaxW; i++) {
    for (; j <=actualmaxH; j++) {

What will happen if j gets larger than actualmaxH in the first iteration of the outer loop? The inner loop will be exited. So, now comes the second iteration of the outer loop where i is now increased by 1 . But since j is still larger than actualmaxH the inner loop won't be "used" anymore.

A possible solution could be this:

for (; i <= actualmaxW; i++) {
    for (int temp = j; temp <= actualmaxH; temp++) {

and then use temp instead of j in the inner loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM