Q.1

  • (a) Can constructors be synchronized in Java. Give a brief explanation.
  • (b) Briefly describe the two techniques available to programmers to define a Java thread.
  • (c) List three examples of deadlocks that are not related to a computer system environment.
  • (d) signal() and signalAll() are methods of which Java class?
  • (e) Briefly describe the differences between daemon and non-daemon threads in Java.
  • (f) Briefly describe how a Java programmer can check whether a thread is daemon or non-daemon.
  • (g) What is the output (i.e. printed) of the following code?
public class CS313Thread {
private static class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
System.out.println(Thread.currentThread().getState().name());
}
}
public static void main(String[] args) {
MyThread myThread = new MyThread("Thread-1");
myThread.start();
}
}

Q.2

  • (a) Briefly describe all the Java Thread states.
  • (b) Specify one method that can change the state of a Java thread from NEW to RUNNABLE.
  • (c) For the following MyCounter implementation, identify:
    • the data that can be involved in a race condition
    • the location (or locations) in the code where a race condition can occur
public class MyCounter
{
private int i;
public MyCounter()
{
i = 0;
}
public void inc()
{
i = i + 1;
}
public void dec()
{
i = i - 1;
}
}
  • (d) Modify the MyCounter class above using a ReentrantLock object in order to prevent interference, when a MyCounter object is referenced from multiple threads.

Q.3

The class Driver declares one MyObject object and two threads, one of type ThreadA and one of type ThreadB. The ThreadA objects run() method calls the MyObjects method foo_a(), and the ThreadB objects run() method calls the MyObjects method foo_b(). The MyObject class is partly implemented, but its methods foo_a() and foo_b() are missing.

The output that the program could produce depends on these methods. Each question below has different versions of these methods. For each question, give all the possible outputs that the program could produce during different executions, if the versions of the methods in that question were included in the MyObject class (see location in comments). If any output can be produced in more than one way, write it only once.

Note that a call to MyObject's print() method prints the values of both MyObject fields var1 and var2. Any output which would be produced during one execution would appear on the same output line. Dont miss the call to myObject.print() at the end of the Driver class main() method.

public class MyObject {
private int var1 = 1, var2 = 2;
// foo_a() and foo_b() SHOULD APPEAR HERE!!!
public synchronized void print() {
System.out.print("" + var1 + " " + var2 + " ");
}
}

public class Driver {
public static void main(String args[]) {
MyObject myObject = new MyObject();
ThreadA ta = new ThreadA(myObject);
ThreadB tb = new ThreadB(myObject);
ta.start();
tb.start();
myObject.print();
}
}
public class ThreadA extends Thread {
private MyObject my_object;
public ThreadA(MyObject m) {
my_object = m;
}
public void run() {
my_object.foo_a();
}
}

public class ThreadB extends Thread {
private MyObject my_object;
public ThreadB(MyObject m) {
my_object = m;
}
public void run() {
my_object.foo_b();
}
}
  • (a) Give all possible outputs which could be produced using these methods in the MyObject class.
public void foo_a() {
var1 = var2;
}
public void foo_b() {
var2 = var1;
}
  • (b) Give all possible outputs which could be produced using these methods in the MyObject class.
public synchronized void foo_a() {
var1 = var2;
}
public synchronized void foo_b() {
var2 = var1;
}
  • (c) Give all possible outputs which could be produced using these methods in the MyObject class.
public synchronized void foo_a() {
var1 = 3;
}
public synchronized void foo_b() {
var2 = 4;
}
  • (d) Give all possible outputs which could be produced using these methods in the MyObject class.
public synchronized void foo_a() {
var1 = 3;
}
public void foo_b() {
var2 = 4;
}

Q.4

  • (a) Given the definition of the Synchronization-Pair (SP) coverage metric presented in class, identify the set of all possible pairs in the following code fragment. Express each pair in the format: < st1, st2>, where st1 and st2 are statement labels as they appear below.
1a thread1() {
2a synchronized(m) {
...
3a }
...
4a synchronized(m) {
...
5a }
...
6a synchronized(m) {
...
7a }
...
8a }
1b thread2() {
2b synchronized(m) {
...
3b }
...
4b synchronized(m) {
...
5b }
...
6b synchronized(m) {
...
7b }
...
8b }
  • (b) Briefly describe the following bug patterns:
    • (i) Nonatomic Operations Assumed to Be Atomic
    • (ii) Losing a Notify Bug Pattern
    • (iii) The Orphaned Thread Bug Pattern
    • (iv) The sleep() Bug Pattern
    • (v) Wrong Lock or No Lock
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.