Docsity

4 Questions on Programming Languages - Assignment 14 | CS 3723, Assignments of Programming Languages

Toggle sidebar
bg1
CS3723, Fall 2006, Homework #14
I. Exercise 14.1
II. Exercise 14.6
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.)
1

Partial preview of the text

Download 4 Questions on Programming Languages - Assignment 14 | CS 3723 and more Assignments Programming Languages in PDF only on Docsity!

CS3723, Fall 2006, Homework

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.)