Exam 20: Multithreading

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

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

(Multiple Choice)
4.9/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
(40)

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

(Multiple Choice)
4.9/5
(34)

Assume two threads share a BankAccount object with balance of zero (0), and that the BankAccount class provides synchronized deposit and withdraw methods. 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 synchronized void deposit(int dollars) { Int newBalance = balance + dollars; System.out.println("depositing"); Balance = newBalance; } Public synchronized void withdraw(int dollars) { Int newBalance = balance - dollars; System.out.println("withdrawing"); Balance = newBalance; }

(Multiple Choice)
4.8/5
(37)

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
(39)

Which of the following class declarations could run in a thread? I public interface MyRunnable extends Runnable { . . . } II public class MyRunnable extends Runnable { . . . } III public class MyRunnable implements Runnable { . . . }

(Multiple Choice)
4.9/5
(36)

In which method are the tasks that are performed by a thread coded?

(Multiple Choice)
4.8/5
(37)

Which exception must be caught or declared when calling the sleep method?

(Multiple Choice)
4.8/5
(34)

Suppose thread one is downloading a 800KB file while another thread is processing the same file on a multi-CPU machine. Suppose further that one time slice allows the first thread to download about 10KB and that the second thread can process 10KB of the file in one time slice. What is the minimum number of CPUs that will allow the job to be completed in roughly half the time of a single-CPU machine?

(Multiple Choice)
4.8/5
(37)

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 when access to the shared data occurs in two different methods?

(Multiple Choice)
4.9/5
(33)

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

(Multiple Choice)
4.8/5
(38)

Consider the following change to the deposit method in Section 20.4: Public void deposit(double amount) { BalanceChangeLock.lock(); Try { Double newBalance = balance + amount; Balance = newBalance; } Finally { BalanceChangeLock.unlock(); } System.out.println("Depositing " + amount + ", new balance is " + balance); } What is the consequence of this change?

(Multiple Choice)
4.9/5
(34)

Which of the following definitely indicates that a thread has terminated? I The run method has completed II The method Thread.interrupted returns true III The run method catches an InterruptedException

(Multiple Choice)
5.0/5
(36)

The Runnable interface has a single method called ____________.

(Multiple Choice)
4.7/5
(43)

A(n) ____ object is used to control the threads that want to manipulate a shared resource.

(Multiple Choice)
4.8/5
(40)

What course of action should be followed when a thread has been interrupted?

(Multiple Choice)
4.8/5
(39)

____ allow a thread to temporarily release a lock, so that another thread can proceed, and to regain the lock at a later time.

(Multiple Choice)
4.8/5
(36)

Under what conditions are locks unnecessary for multi-threaded programs?

(Multiple Choice)
4.7/5
(43)

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 100 milliseconds between calls. Thread two calls getSharedData eight times, also sleeping for 100 milliseconds between calls. Which of the following could be the last two values received by thread two? Public class SharedData { Private int value; Public void setSharedData(int n) { Value = n; } Public int getSharedData() { Return value; } }

(Multiple Choice)
4.8/5
(37)

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.7/5
(41)
Showing 21 - 40 of 82
close modal

Filters

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