Exam 21: Multithreading

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

Consider the addFirst method of the LinkedList class in Chapter 16: /** Adds an element to the front of the linked list. @param element the element to add */ Public void addFirst(Object element) { Node newNode = new Node(); NewNode.data = element; NewNode.next = first; First = newNode; } Three implementations have been proposed to make the addFirst method thread safe where listLock is a variable of type ReentrantLock. Which of them will work? I. ListLock.lock(); Try { Node newNode = new Node(); NewNode.data = element; NewNode.next = first; } Finally { ListLock.unlock(); } First = newNode; II. Node newNode = new Node(); NewNode.data = element; NewNode.next = first; ListLock.lock(); Try { First = newNode; } Finally { ListLock.unlock(); } III. ListLock.lock(); Try { Node newNode = new Node(); NewNode.data = element; NewNode.next = first; First = newNode; } Finally { ListLock.unlock(); }

(Multiple Choice)
4.9/5
(46)

The ____ method is useful only if you know that a waiting thread can actually proceed.

(Multiple Choice)
4.8/5
(23)

The Runnable interface includes which method(s)? I public void run(Runnable runnable) II public void run() III public void start()

(Multiple Choice)
5.0/5
(34)

Examine the SharedData class shown below. Suppose two threads are created so that each has access to the same SharedData object. Thread one calls setSharedData eight times with values 1...8 respectively, sleeping for 30 milliseconds between calls. Thread two calls getSharedData eight times, also sleeping for 30 milliseconds between calls. Which of the following orders of values is not possible for thread two to receive? public class SharedData { Private int value; Public void setSharedData(int n) { Value = n; } Public int getSharedData() { Return value; } }

(Multiple Choice)
4.8/5
(33)

Stale data occurs in multi-CPU machines when one thread modifies shared data and a second thread accesses that data later, but sees the data value before the change took place. What is required to guarantee that the second thread sees the updated data, not stale data?

(Multiple Choice)
4.7/5
(38)

When a sleeping thread is interrupted, an InterruptedException is generated. Where do you catch that exception?

(Multiple Choice)
4.8/5
(39)

For threads of equal priority, which is guaranteed by the thread scheduler that is part of the Java Virtual Machine? I All will get time on the CPU II All will get exactly equal time on the CPU III The order threads run in the CPU will always be the same

(Multiple Choice)
4.9/5
(34)

Suppose that the class XYZ implements the interface Runnable. Which code creates a thread object and makes it available to the scheduler to be executed?

(Multiple Choice)
4.8/5
(37)

A(n) ____ uses a small number of threads to execute a large number of runnables.

(Multiple Choice)
4.9/5
(27)

Which argument(s) present(s) the best case(s) for extending the Thread class rather than using the Runnable interface in conjunction with a thread pool? I Thread sub-classes will all execute independently II Runnable objects will waste more system resources III Thread sub-classes can execute faster on a single CPU than Runnable objects

(Multiple Choice)
4.8/5
(30)

Assume three threads share a BankAccount object with balance of zero (0), a ReentrantLock named myLock, and a condition object on myLock named lowBalanceCondition, as shown below. Thread one calls withdraw(30), then thread two calls withdraw(20)and thread three calls deposit(45). If the starting balance is 0, what is the balance after the three calls? public void deposit(int dollars) { MyLock.lock(); Int newBalance = balance + dollars; System.out.println("depositing"); Balance = newBalance; MyLock.unlock(); } Public void withdraw(int dollars) { MyLock.lock(); While (balance < dollars) { LowBalanceCondition.await(); } Int newBalance = balance - dollars; System.out.println("withdrawing"); Balance = newBalance; MyLock.unlock(); }

(Multiple Choice)
4.7/5
(44)

Which method(s) are part of the Thread class? I public void run(Runnable runnable) II public void start(Runnable runnable) III public void start()

(Multiple Choice)
4.7/5
(36)

When is it a good idea to call notifyAll in a synchronized method?

(Multiple Choice)
4.8/5
(37)

What should be done to get the attention of a thread?

(Multiple Choice)
4.7/5
(29)

Assume two threads share a BankAccount object with balance of zero (0), and that the BankAccount class provides deposit and withdraw methods as shown below. Thread one deposits $10 ten times and, concurrently, thread two withdraws $10 ten times. Suppose a race condition occurs, and the race is finished first by thread one. What would you expect balance to be after all thread calls? public void deposit(int dollars) { Int newBalance = balance + dollars; System.out.println("depositing"); Balance = newBalance; } Public void withdraw(int dollars) { Int newBalance = balance - dollars; System.out.println("withdrawing"); Balance = newBalance; }

(Multiple Choice)
4.8/5
(38)

____ occur(s) if the effect of multiple threads on shared data depends on the order in which the threads are scheduled.

(Multiple Choice)
4.8/5
(32)

A GUI should be responsive to the user. If a GUI interaction starts a time-consuming task, the GUI may not be responsive to the user until the task completes. Which approach would make the GUI responsive under these circumstances?

(Multiple Choice)
4.8/5
(32)

Given a two-CPU machine and four threads, how many of the threads can execute in parallel?

(Multiple Choice)
4.8/5
(42)

Calling the wait method in synchronized code is very similar to what action after locking with a ReentrantLock object?

(Multiple Choice)
4.8/5
(39)

Assume two threads share a BankAccount object with balance of zero (0), and that the BankAccount class provides deposit and withdraw methods and has a ReentrantLock named myLock, as shown below. Thread one deposits $10 ten times and, concurrently, thread two withdraws $10 ten times. Which statement regarding the balance after all thread calls is definitely true? public void deposit(int dollars) { MyLock.lock() Int newBalance = balance + dollars; System.out.println("depositing"); Balance = newBalance; MyLock.unlock() } Public void withdraw(int dollars) { MyLock.lock() Int newBalance = balance - dollars; System.out.println("withdrawing"); Balance = newBalance; MyLock.unlock() }

(Multiple Choice)
4.8/5
(34)
Showing 41 - 60 of 82
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)