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:
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.
this is where I want to access mon:
well, i guess that's it. I hope someone could help. Thanks so much guys!
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(); } }
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"); } } } }
Comment