1

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();
    }
8
  • So to understand you correctly: On the line Hit hit=world.hit(cam.rayFor(maxW,maxH,i,j)); i keeps his initial value even after the first iteration of the outer loop (where i gets incremented to initial + 1)? Commented Jan 9, 2015 at 21:10
  • Are your variables (maxH, actualmaxH, maxW, …) used outside of this thread? Can you reproduce the problem in a single thread? Multithreaded programming is tricky in some ways, so it is a good idea to distinguish between simple problems and multithreaded-only problem. Commented Jan 9, 2015 at 21:13
  • @Tom thanks for the edit hint=) and yes in the line "Hit hit=world.hit(cam.rayFor(maxW,maxH,i,j));" the i is always 0 even after the first iteration of the outer loop ........... when i create only one thread the problem still exists=) ......... only maxH and maxW are used outside of the threads Commented Jan 9, 2015 at 21:20
  • Well, then I guess we need every part of that class that contains that i (the declaration and the initialization) and how it is initialized (I guess the main method does that?) Commented Jan 9, 2015 at 21:22
  • edited the post to show constructor and from where the constructor is called......i hope its understandable..such a long line =( Commented Jan 9, 2015 at 21:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Dasterr Questions get closed if they are either a duplicate of another question or a "bad" question (because it is unknown what the asker meant or it is to broad or ...). So I guess you won't your questions to get closed :P. But there is a they to show that a question got solved. It is called accepting an answer. You can accept this answer if you like, but I'm not pushing you to do that :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.