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

Java Thread Race Condition

Mark
< Prev
Next >

Which statement best describes a potential concurrency issue in the following program?

public class Counter {
    private int count = 0;
    public void increment() { count++; }
    public int getCount() { return count; }
}

public class Worker extends Thread {
    private Counter counter;
    public Worker(Counter counter) { this.counter = counter; }
    public void run() {
        for (int i = 0; i < 1000; i++) {
            counter.increment();
        }
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Counter c = new Counter();
        Thread t1 = new Worker(c);
        Thread t2 = new Worker(c);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("Count is: " + c.getCount());
    }
}
A

There is a compile-time error due to incorrect thread instantiation.

B

The join() calls could throw a checked exception leading to unexpected termination.

C

A deadlock occurs because threads wait on each other to complete.

D

The unsynchronized increment() method creates a race condition as threads concurrently modify the shared variable ‘count’.

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|