Java Synchronization Review

All java programmers are familiar with java synchronization. But how much do you really know about it? Are you sure that you have solid knowledge on this concept? If yes then continue reading. Otherwise you may if you wish to.
Following questions are only for those who are very much confident about their knowledge on java synchronization. I bet few of us can answer all these questions correctly.

Given the codes,
MyIO.java

public class MyIO {
    public synchronized void read() {
        System.out.println("Reading started....");
        for (int i = 0; i < 10; i++) {
            System.out.println("Read " + i);
            try {
                // sleeps for 0 to 2 milliseconds.
                Thread.sleep(Math.round(Math.random() * 2));
            } catch (InterruptedException e) {
                ;// no operation
            }
        }
        System.out.println("Reading completed.");
    }
    public synchronized void write() {
        System.out.println("Writing started....");
        for (int i = 0; i < 10; i++) {
            System.out.println("Write " + i);
            try {
                // sleeps for 0 to 2 milliseconds.
                Thread.sleep(Math.round(Math.random() * 2));
            } catch (InterruptedException e) {
                ;// no operation
            }
        }
        System.out.println("Writing completed.");
    }
}

Reader.java

public class Reader implements Runnable {
    private final MyIO io;
    public Reader(MyIO io) {
        super();
        this.io = io;
    }
    public void run() {
        io.read();
    }
}

Writer.java

public class Writer implements Runnable {
    private final MyIO io;
    public Writer(MyIO io) {
        super();
        this.io = io;
    }
    public void run() {
        io.write();
    }
}

Q1: Select all possible output of the following program.

public class ThreadRunner {
    public static void main(String[] args) {
        MyIO io1 = new MyIO();
        MyIO io2 = new MyIO();
        Thread readThread = new Thread(new Reader(io1));
        Thread writeThread = new Thread(new Writer(io2));
        writeThread.start();
        readThread.start();
    }
}

Options:
a)
Writing started....
Write 0
Reading started....
Read 0
Read 1
Write 1
...
...
Write 8
Write 9
Reading completed.
Writing completed.

b)
Writing started....
Write 0
Reading started....
Read 0
Write 1
Read 1
Write 2
...
...
Read 8
Read 9
Write 7
Write 8
Reading completed.
Write 9
Writing completed.

c)
Writing started....
Write 0
Write 1
...
...
Write 8
Write 9
Writing completed.
Reading started....
Read 0
Read 1
...
...
Read 8
Read 9
Reading completed.

d) None of the above

Q2: Select all possible output of the following program.

public class ThreadRunner {
    public static void main(String[] args) {
        MyIO io = new MyIO();
        Thread readThread = new Thread(new Reader(io));
        Thread writeThread = new Thread(new Writer(io));
        writeThread.start();
        readThread.start();
    }
}

Options:
a)
Writing started....
Write 0
Write 1
...
...
Write 8
Write 9
Writing completed.
Reading started....
Read 0
Read 1
...
...
Read 8
Read 9
Reading completed.

b)
Reading started....
Read 0
Read 1
...
...
Read 8
Read 9
Reading completed.
Writing started....
Write 0
Write 1
...
...
Write 8
Write 9
Writing completed.

c)
Writing started....
Write 0
Reading started....
Read 0
Write 1
Read 1
Write 2
...
...
Read 8
Read 9
Write 7
Write 8
Reading completed.
Write 9
Writing completed.

d) None of the above.

Q3: What will be the output of the following program if both MyIO.read() and MyIO.write() are made static?

public class ThreadRunner {
    public static void main(String[] args) {
        MyIO io1 = new MyIO();
        MyIO io2 = new MyIO();
        Thread readThread = new Thread(new Reader(io1));
        Thread writeThread = new Thread(new Writer(io2));
        writeThread.start();
        readThread.start();
    }
}

Options:
a)
Writing started....
Write 0
Write 1
...
...
Write 8
Write 9
Writing completed.
Reading started....
Read 0
Read 1
...
...
Read 8
Read 9
Reading completed.

b)
Reading started....
Read 0
Read 1
...
...
Read 8
Read 9
Reading completed.
Writing started....
Write 0
Write 1
...
...
Write 8
Write 9
Writing completed.

c)
Writing started....
Write 0
Reading started....
Read 0
Write 1
Read 1
Write 2
...
...
Read 8
Read 9
Write 7
Write 8
Reading completed.
Write 9
Writing completed.

d) None of the above.

Answers:

1: Correct answers are A, B and C. A and B are correct because both of the threads readThread and writeThread are working on two different instances of MyIO. So in this case object level synchronization has NO effect. Again we all know that it is the JVM who decides which thread will get chance to run. In case of C, JVM may decide to let writeThread to finish its job before it starts readThread. So C is also a valid answer.

2: Correct answers are A and B. In this case note that both of the threads are working on same instance. Object level synchronization will be applied is this scenario. So the thread that gets started first will make the other thread wait until it finishes its job.

3: Correct answers are A and B. Note that both MyIO.read and MyIO.write are declared as static. Even though both threads work in two different instances of class MyIO, object level synchronization has no effect. Class level synchronization will take effect in this case.

Note: The first two paragraph of this post is just to attract your attention. Please don’t take it other ways.

2 Comments »

  1. M M Islam Chisty said

    This surely is a nice post and this DOES talk about Java Sync. Simply ignore comments on JpGroup. There are always critiques behind every good attempt.

    Regards,
    .. Chisty

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: