We had an example of this in class on January 30 with the AnimationApplet class.
public void set_draw_centered_strings(String str, Color C); public void set_draw_centered_strings(String str, int fsize, Color C); public void set_draw_centered_strings(String str, int fsize, Color C1, Color C2, int high_start, int high_num); public void set_draw_centered_strings(String str, String fname, int fstyle, int fsize, Color C1, Color C2, int high_start, int high_num);which draw one string per line left justified but with the position giving the center of the group.
public void path_arc_set(int x1, int y1, int rx, int ry, int start_angle, int arc_angle);
public void path_poly_set(int x1[], int y1[], long t[], Color C[]); public void path_poly_set(int x1[], int y1[], long t[], long td[], Color C[]);which allow for changing the color of an object as it moves along a polynomial path.
Each class is potentially a monitor.
Methods are protected using the key word synchronized. The following methods are used:
Recall the classical producer/consumer or bounded buffer problem.
This applet illustrates both Java synchronization and several features of JOTSA. Here are the idea of the implementation:
package bb_reduced; public class Producer extends Thread { private int nextval = 0; private Buffer buffers; public Producer(Buffer buffers) { this.buffers = buffers; start(); } public void run() { nextval++; buffers.insert_object_in_buffer(nextval); } }
package bb_reduced; public class Consumer extends Thread { private Buffer buffers; public Consumer(Buffer buffers) { this.buffers = buffers; start(); } public void run() { int val; val = buffers.remove_object_from_buffer(); } }
package bb_reduced; public class Buffer { private int buffer_size; private int[] buffers; private int counter; private int in; private int out; public Buffer(int n) { buffer_size = n; buffers = new int[buffer_size]; in = 0; out = 0; counter = 0; } public synchronized void insert_object_in_buffer(int val) { while (counter == buffer_size) try {wait();} catch (InterruptedException e) {} buffers[in] = val; in = (in + 1)%buffer_size; counter++; notifyAll(); } public synchronized int remove_object_from_buffer() { int val; while (counter == 0) try {wait();} catch (InterruptedException e) {} val = buffers[out]; out = (out + 1)%buffer_size; counter--; notifyAll(); return val; } }