Biblioteca Java - Rev 27
Subversion Repositories:
package ex2_balls;
import java.awt.Color;
import javax.swing.*;
/*
TASK 1
Your first task is to add still another thread to your program that kills the balls
at random times (i.e. with short random delays inbetween). But the balls should be
killed in random order. Killing a ball means that its run method must terminate.
This should be achieved by making the loop terminate normally and NOT by calling the
deprecated method to stop a thread. Further, the ball must be removed from the world
(in a thread-safe way similar to addBalls).
TASK 2
Now return to the original version of the program (make a new directory and download the program again).
You must now modify the program to achieve the following behaviour: When a bouncing ball after one of
its moves finds itself in the diagonal area of the world (i.e. where x is very close to y), it will
"freeze", i.e. stop moving. Note that a ball may jump over the diagonal area in one move; this will not
cause it to freeze. When all balls have frozen at the diagonal, they will all wake up and continue bouncing
until they freeze again on the diagonal. This bouncing/freezing continues forever.
You should recognize this as a form of barrier synchronization that can be achieved using N+1 semaphores: one
common barrier semaphore, which ball threads release when they reach the synchronization point, and an array
of "continue" semaphores, indexed by thread, which threads acquire in order to continue beyond the barrier.
A special barrier synchronization process is also needed, which repeatedly acquire the barrier semaphore N times,
followed by releasing all the continue semaphors.
The package java.util.concurrent includes the class CyclicBarrier, which provides more convenient means to
achieve barrier synchronization. As an alternative for resolving this exercise you can use CyclicBarrier class.
*/
public class Balls {
private static void sleep(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {}
}
public static void main(String[] a) {
final JFrame win = new JFrame();
final BallWorld world = new BallWorld();
win.getContentPane().add(world);
win.pack();
win.setVisible(true);
new Thread(new Runnable() {
public void run() {
while (true) {
sleep(20);
win.repaint();
}
}
}).start();
sleep((int)(5000*Math.random()));
new Thread(new Ball(world, 50, 80, 5, 10,Color.red)).start();
sleep((int)(5000*Math.random()));
new Thread(new Ball(world, 70, 100, 8, 6,Color.blue)).start();
sleep((int)(5000*Math.random()));
new Thread(new Ball(world, 150, 100, 9, 7,Color.green)).start();
sleep((int)(5000*Math.random()));
new Thread(new Ball(world, 200, 130, 3, 8,Color.black)).start();
sleep((int)(5000*Math.random()));
}
}
import java.awt.Color;
import javax.swing.*;
/*
TASK 1
Your first task is to add still another thread to your program that kills the balls
at random times (i.e. with short random delays inbetween). But the balls should be
killed in random order. Killing a ball means that its run method must terminate.
This should be achieved by making the loop terminate normally and NOT by calling the
deprecated method to stop a thread. Further, the ball must be removed from the world
(in a thread-safe way similar to addBalls).
TASK 2
Now return to the original version of the program (make a new directory and download the program again).
You must now modify the program to achieve the following behaviour: When a bouncing ball after one of
its moves finds itself in the diagonal area of the world (i.e. where x is very close to y), it will
"freeze", i.e. stop moving. Note that a ball may jump over the diagonal area in one move; this will not
cause it to freeze. When all balls have frozen at the diagonal, they will all wake up and continue bouncing
until they freeze again on the diagonal. This bouncing/freezing continues forever.
You should recognize this as a form of barrier synchronization that can be achieved using N+1 semaphores: one
common barrier semaphore, which ball threads release when they reach the synchronization point, and an array
of "continue" semaphores, indexed by thread, which threads acquire in order to continue beyond the barrier.
A special barrier synchronization process is also needed, which repeatedly acquire the barrier semaphore N times,
followed by releasing all the continue semaphors.
The package java.util.concurrent includes the class CyclicBarrier, which provides more convenient means to
achieve barrier synchronization. As an alternative for resolving this exercise you can use CyclicBarrier class.
*/
public class Balls {
private static void sleep(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {}
}
public static void main(String[] a) {
final JFrame win = new JFrame();
final BallWorld world = new BallWorld();
win.getContentPane().add(world);
win.pack();
win.setVisible(true);
new Thread(new Runnable() {
public void run() {
while (true) {
sleep(20);
win.repaint();
}
}
}).start();
sleep((int)(5000*Math.random()));
new Thread(new Ball(world, 50, 80, 5, 10,Color.red)).start();
sleep((int)(5000*Math.random()));
new Thread(new Ball(world, 70, 100, 8, 6,Color.blue)).start();
sleep((int)(5000*Math.random()));
new Thread(new Ball(world, 150, 100, 9, 7,Color.green)).start();
sleep((int)(5000*Math.random()));
new Thread(new Ball(world, 200, 130, 3, 8,Color.black)).start();
sleep((int)(5000*Math.random()));
}
}