Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 8: 2D Array
Start Practice TestPractice Test
About Exam
hard

Java Thread Race Condition

Mark
< Prev
Next >

The main method in the MatrixModifier class starts a new thread to execute the run method and concurrently calls the printMatrix method. What is the primary synchronization issue that arises from this design?

public class MatrixModifier implements Runnable {
    private int[][] matrix = { {0,0,0}, {0,0,0}, {0,0,0} };

    public void run() {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                matrix[i][j] = matrix[i][j] + 1;
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {}
            }
        }
    }

    public void printMatrix() {
        for (int[] row : matrix) {
            for (int value : row) {
                System.out.print(value + " ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        MatrixModifier mod = new MatrixModifier();
        Thread modifier = new Thread(mod);
        modifier.start();
        mod.printMatrix();
    }
}
A

An infinite loop resulting from unsynchronized sleep intervals in run().

B

Race condition causing printMatrix to read inconsistent or partially updated values.

C

Deadlock because printMatrix waits indefinitely for run() to finish.

D

Thread starvation due to lower priority of the printMatrix method.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|