asela_it1
asked on
Why Synchronize in this program??
what is the importance of synchronize key word in this program,what will happen if we remove that from each method(that use it,specially getParticles(),setParticle s())??
Particle {
protected int x;
protected int y;
protected final Random rng = new Random();
public Particle(int initialX, int initialY) {
x = initialX;
y = initialY;
}
public synchronized void move() {
x += rng.nextInt(10) - 5;
y += rng.nextInt(20) - 10;
}
public void draw(Graphics g) {
int lx, ly;
synchronized (this) { lx = x; ly = y; }
g.drawRect(lx, ly, 10, 10);
}
}
class ParticleCanvas extends Canvas {
private Particle[ ] particles = new Particle[0];
ParticleCanvas(int size) {
setSize(new Dimension(size, size));
}
// intended to be called by applet
protected synchronized void setParticles(Particle[ ] ps) {
if (ps == null)
throw new IllegalArgumentException(" Cannot set null");
particles = ps;
}
protected synchronized Particle[ ] getParticles() {
return particles;
}
public void paint(Graphics g) { // override Canvas.paint
Particle[ ] ps = getParticles();
for (int i = 0; i < ps.length; ++i)
ps[i].draw(g);
}
}
public class ParticleApplet extends Applet {
protected Thread[ ] threads = null; // null when not running
protected final ParticleCanvas canvas
= new ParticleCanvas(100);
public void init() { add(canvas); }
protected Thread makeThread(final Particle p) { // utility
Runnable runloop = new Runnable() {
public void run() {
try {
for(;;) {
p.move();
canvas.repaint();
Thread.sleep(100); // 100msec is arbitrary
}
}
catch (InterruptedException e) { return; }
}
};
return new Thread(runloop);
}
public synchronized void start() {
int n = 10; // just for demo
if (threads == null) { // bypass if already started
Particle[ ] particles = new Particle[n];
for (int i = 0; i < n; ++i)
particles[i] = new Particle(50, 50);
canvas.setParticles(partic les);
threads = new Thread[n];
for (int i = 0; i < n; ++i) {
threads[i] = makeThread(particles[i]);
threads[i].start();
}
}
}
public synchronized void stop() {
if (threads != null) { // bypass if already stopped
for (int i = 0; i < threads.length; ++i)
threads[i].interrupt();
threads = null;
}
}
}
Particle {
protected int x;
protected int y;
protected final Random rng = new Random();
public Particle(int initialX, int initialY) {
x = initialX;
y = initialY;
}
public synchronized void move() {
x += rng.nextInt(10) - 5;
y += rng.nextInt(20) - 10;
}
public void draw(Graphics g) {
int lx, ly;
synchronized (this) { lx = x; ly = y; }
g.drawRect(lx, ly, 10, 10);
}
}
class ParticleCanvas extends Canvas {
private Particle[ ] particles = new Particle[0];
ParticleCanvas(int size) {
setSize(new Dimension(size, size));
}
// intended to be called by applet
protected synchronized void setParticles(Particle[ ] ps) {
if (ps == null)
throw new IllegalArgumentException("
particles = ps;
}
protected synchronized Particle[ ] getParticles() {
return particles;
}
public void paint(Graphics g) { // override Canvas.paint
Particle[ ] ps = getParticles();
for (int i = 0; i < ps.length; ++i)
ps[i].draw(g);
}
}
public class ParticleApplet extends Applet {
protected Thread[ ] threads = null; // null when not running
protected final ParticleCanvas canvas
= new ParticleCanvas(100);
public void init() { add(canvas); }
protected Thread makeThread(final Particle p) { // utility
Runnable runloop = new Runnable() {
public void run() {
try {
for(;;) {
p.move();
canvas.repaint();
Thread.sleep(100); // 100msec is arbitrary
}
}
catch (InterruptedException e) { return; }
}
};
return new Thread(runloop);
}
public synchronized void start() {
int n = 10; // just for demo
if (threads == null) { // bypass if already started
Particle[ ] particles = new Particle[n];
for (int i = 0; i < n; ++i)
particles[i] = new Particle(50, 50);
canvas.setParticles(partic
threads = new Thread[n];
for (int i = 0; i < n; ++i) {
threads[i] = makeThread(particles[i]);
threads[i].start();
}
}
}
public synchronized void stop() {
if (threads != null) { // bypass if already stopped
for (int i = 0; i < threads.length; ++i)
threads[i].interrupt();
threads = null;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This is entirely true, there is no need for it in this code given above..
But i am generaly explaining as what synchronized would achieve.
But i am generaly explaining as what synchronized would achieve.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
talk to eachother.