Biblioteca Java - Blame information for rev 27
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
27 | mihai | 1 | package ex2_balls; |
2 | import java.awt.Color; | ||
3 | import javax.swing.*; | ||
4 | |||
5 | /* | ||
6 | TASK 1 | ||
7 | Your first task is to add still another thread to your program that kills the balls | ||
8 | at random times (i.e. with short random delays inbetween). But the balls should be | ||
9 | killed in random order. Killing a ball means that its run method must terminate. | ||
10 | This should be achieved by making the loop terminate normally and NOT by calling the | ||
11 | deprecated method to stop a thread. Further, the ball must be removed from the world | ||
12 | (in a thread-safe way similar to addBalls). | ||
13 | |||
14 | TASK 2 | ||
15 | Now return to the original version of the program (make a new directory and download the program again). | ||
16 | You must now modify the program to achieve the following behaviour: When a bouncing ball after one of | ||
17 | its moves finds itself in the diagonal area of the world (i.e. where x is very close to y), it will | ||
18 | "freeze", i.e. stop moving. Note that a ball may jump over the diagonal area in one move; this will not | ||
19 | cause it to freeze. When all balls have frozen at the diagonal, they will all wake up and continue bouncing | ||
20 | until they freeze again on the diagonal. This bouncing/freezing continues forever. | ||
21 | |||
22 | You should recognize this as a form of barrier synchronization that can be achieved using N+1 semaphores: one | ||
23 | common barrier semaphore, which ball threads release when they reach the synchronization point, and an array | ||
24 | of "continue" semaphores, indexed by thread, which threads acquire in order to continue beyond the barrier. | ||
25 | |||
26 | A special barrier synchronization process is also needed, which repeatedly acquire the barrier semaphore N times, | ||
27 | followed by releasing all the continue semaphors. | ||
28 | |||
29 | The package java.util.concurrent includes the class CyclicBarrier, which provides more convenient means to | ||
30 | achieve barrier synchronization. As an alternative for resolving this exercise you can use CyclicBarrier class. | ||
31 | */ | ||
32 | public class Balls { | ||
33 | |||
34 | private static void sleep(int ms) { | ||
35 | try { | ||
36 | Thread.sleep(ms); | ||
37 | } catch (InterruptedException e) {} | ||
38 | } | ||
39 | |||
40 | |||
41 | public static void main(String[] a) { | ||
42 | |||
43 | final JFrame win = new JFrame(); | ||
44 | final BallWorld world = new BallWorld(); | ||
45 | win.getContentPane().add(world); | ||
46 | win.pack(); | ||
47 | win.setVisible(true); | ||
48 | |||
49 | new Thread(new Runnable() { | ||
50 | public void run() { | ||
51 | while (true) { | ||
52 | sleep(20); | ||
53 | win.repaint(); | ||
54 | } | ||
55 | } | ||
56 | }).start(); | ||
57 | |||
58 | |||
59 | sleep((int)(5000*Math.random())); | ||
60 | new Thread(new Ball(world, 50, 80, 5, 10,Color.red)).start(); | ||
61 | sleep((int)(5000*Math.random())); | ||
62 | new Thread(new Ball(world, 70, 100, 8, 6,Color.blue)).start(); | ||
63 | sleep((int)(5000*Math.random())); | ||
64 | new Thread(new Ball(world, 150, 100, 9, 7,Color.green)).start(); | ||
65 | sleep((int)(5000*Math.random())); | ||
66 | new Thread(new Ball(world, 200, 130, 3, 8,Color.black)).start(); | ||
67 | sleep((int)(5000*Math.random())); | ||
68 | |||
69 | } | ||
70 | |||
71 | } |