
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

I. Exercise 14.
II. Exercise 14.
III. Consider the Java class Monitor:
public class Monitor { int x; int y;
public static void main(String argv[]) throws InterruptedException { final Monitor mon = new Monitor(); Thread one = new Thread() {public void run() {mon.thread1();}}; Thread two = new Thread() {public void run() {mon.thread2();}}; one.start(); two.start(); one.join(); two.join(); }
void thread1() { x = 1; synchronized(this) { y = 1; } }
void thread2() { int r1, r2; synchronized(this) { r1 = y; } r2 = x; System.out.println("r1 = " + r1); System.out.println("r2 = " + r2); } }
Is Monitor correctly synchronized under the Java 5 Memory Model? Why or why not?
IV. Consider the prolog predicate truncate2, defined as follows:
truncate2(Xs, Ys) :- append(Xs, [A, B], Ys).
(Assume append is defined as on p. 484 of your text book.)
(a) What is the value of Xs that will be ”returned” for the query truncate2(Xs,[1,2,3,4,5,6,7]).? (b) What intermediate substitutions would be derived for A and B? (c) List the recursive ”calls” to the append and the resulting substitution at each layer of the recusion. (Note each level of recursion may have its own instance of variables that have the same name, make sure you distinguish these in some way.)