Threading and shared array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrhitokiri
    New Member
    • Apr 2008
    • 35

    Threading and shared array

    hi! whew! been a while since i last posted. anyway, I have this code, but I'm trying to figure out a way to access a shared variable within the threads.

    here's the main program:
    Code:
    public class FeedFrenzy{
    	public static void main(String args[]){
    		Frenzy frenzy = new Frenzy();
    		Fish fish1 = new Predator();
    		Prey minnow = new Prey();
    		frenzy.setName("Frenzy");
    		minnow.setName("Golden Minnow");
    		fish1.setName("Small Fishy");
    		Monitor mon;   //this is what I want to access in each thread
      	        (new Thread(fish1)).start();
    		(new Thread(frenzy)).start();
    		(new Thread(minnow)).start();
    	}
    }
    this is a simple monitor. what it does is just keep a 2d array of the location of the fish, much like a cartesian plane.

    Code:
    public class Monitor {
    	Fish fishGrid[][] = new Fish[100][100];
    	public Fish check (int loc_x, int loc_y){
    		return fishGrid[loc_x][loc_y];
    	}
    	public void move(Fish fish, int loc_x, int loc_y){
    		//do something
    	}
    }

    this is where I want to access mon:
    Code:
    public class Fish implements Runnable {
        ...
        public void run(){
        	int direction = 0;
        	if (!(life > 0)) alive = false;  //checks each time if fish is alive
        	while (alive) {
    [B]    		/* access mon here and update the grid
                        something like:
                         mon.fishGrid[location_x][location_y] = this;
                    */[/B]
        		direction = (int)(Math.random() * 4 ) + 1;
        		swim(direction);
        		try {
        			Thread.sleep(1000);
        		}catch (Exception e){
        			System.out.println("cannot set delay");
        		}
        	}
        }
    }
    well, i guess that's it. I hope someone could help. Thanks so much guys!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Pass your monitor around to every thread that has to deal with it so they know where to find it. The code in the monitor object that is stamped to be critical should synchronize on the monitor object itself. That's all there is to it ...

    kind regards,

    Jos

    Comment

    • jrhitokiri
      New Member
      • Apr 2008
      • 35

      #3
      that's exactply what i thought i wanted to do, but the question is how? err, could you spare some coded examples?

      thanks for the reply Jos.. :D

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Something like this:

        Code:
        Monitor monitor= new Monitor(); 
        Frenzy frenzy= new Frenz(monitor); // pass the monitor
        Fish fish= new Fish(monitor); // pass it again
        ...
        public class Frenzy {
           private Monitor monitor; // points to a monitor;
           public Frenzy(Monitor monitor) { this.monitor= monitor; }
           ...
           public Fish peek(int x, int y) {
              return monitor.peek(x, y); // consult the monitor
           }
           ...
        }
        ...
        public class Monitor {
           private Fish[][] fish= new Fish[10][10]; // or whatever
           ...
           public synchronized Fish peek(int x, int y) {
              return fish[x][y]; // no other thread can access this matrix
           }
           public synchronized  void poke(Fish aFish, int x, int y) {
              fish[x][y]= aFish; // no other thread can access this matrix
           }
        }
        I hope you get the picture,

        kind regards,

        Jos

        Comment

        • jrhitokiri
          New Member
          • Apr 2008
          • 35

          #5
          whoa! so that's how!:D I get it now. really appreaciate it.

          Just a quick question:
          so when the monitor is passed, all i have to do is create a constructor for it, and i don't have to modify the part where i wrote this
          Code:
          (new Thread(fish1)).start();
          (new Thread(frenzy)).start();
          (new Thread(minnow)).start();
          ?

          Thanks again Jos

          Kazuo

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by jrhitokiri
            whoa! so that's how!:D I get it now. really appreaciate it.

            Just a quick question:
            so when the monitor is passed, all i have to do is create a constructor for it, and i don't have to modify the part where i wrote this
            Code:
            (new Thread(fish1)).start();
            (new Thread(frenzy)).start();
            (new Thread(minnow)).start();
            ?

            Thanks again Jos

            Kazuo
            Yep, simply pass that monitor to your other objects (a constructor is a fine place for that but a 'setMonitor(... )' method would be fine too if you don't forget to call it (that's why a ctor is better). The monitor itself should take care of its critical sections (synchronize) while the other classes in their own threads know nothing.

            kind regards,

            Jos

            Comment

            • jrhitokiri
              New Member
              • Apr 2008
              • 35

              #7
              got it.:D

              thanks again!:D this was quite helpful.

              KaZuo

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by jrhitokiri
                got it.:D

                thanks again!:D this was quite helpful.
                You're welcome of course.

                kind regards,

                Jos

                Comment

                Working...