Exam 21: Multithreading

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

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. Note that only the deposit method uses the lock. 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) { Int newBalance = balance - dollars; System.out.println("withdrawing"); Balance = newBalance; }

(Multiple Choice)
5.0/5
(39)

A waiting thread is blocked until another thread calls ____ on the condition object for which the thread is waiting.

(Multiple Choice)
4.7/5
(40)

Which constructor can be used to create a new thread associated with a Runnable object?

(Multiple Choice)
4.9/5
(27)

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. What is one reason why thread two may not receive 7 and 8 on its two final calls to getSharedData? public class SharedData { Private int value; Public void setSharedData(int n) { Value = n; } Public int getSharedData() { Return value; } }

(Multiple Choice)
4.8/5
(39)

Which phrase best describes the purpose of a lock used in an object in one or more of its methods?

(Multiple Choice)
4.8/5
(27)

Consider an old fashioned telephone booth that can be occupied by one person at a time. Suppose one person went in and dialed a part of her number, and had to leave the booth. A second person went in and dialed a part of his number, and before the number was fully dialed, a connection to some other phone was made. What Java threads analogy fits this scenario? I The two people are the threads II The shared data is the telephone III The state of the object is corrupt

(Multiple Choice)
4.8/5
(42)

The Runnable interface has a single method called ____________.

(Multiple Choice)
4.8/5
(41)

Assume three threads share a BankAccount object with balance of zero (0), a ReentrantLock named myLock, and has 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 and after the waiting threads have had a chance to run? public void deposit(int dollars) { MyLock.lock(); Int newBalance = balance + dollars; System.out.println("depositing"); Balance = newBalance; LowBalanceCondition.signalAll(); 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.8/5
(41)

Which of the following statements is correct?

(Multiple Choice)
4.7/5
(41)

If you do not use the Runnable interface, then what is necessary to create a new thread? I Implement the Threadable interface II Extend the Thread class and add a run() method to it III Add a run method to any class

(Multiple Choice)
4.9/5
(32)

Which are ways that a thread can be blocked? I when it is sleeping II waiting for a lock to be available III waiting after calling the await method

(Multiple Choice)
4.8/5
(43)

What happens if we try to start a thread with an instance of a Runnable class that did not override the run method?

(Multiple Choice)
4.9/5
(36)

Consider an old fashioned telephone booth that can be occupied by one person at a time. Suppose one person went in and dialed a part of her number, and had to leave the booth. A second person went in and dialed a part of his number, and before the number was fully dialed, a connection to some other phone was made. What Java threads analogy would prevent this undesirable scenario? I Acquire the lock prior to entering the booth II Dial a complete number when inside the booth III Hang up the phone and release the lock upon exiting the booth

(Multiple Choice)
4.8/5
(25)

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. If the balance after all thread calls is 0, which statement is definitely true? 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.9/5
(42)

Suppose thread one is downloading a 800KB file while another thread is processing the same file on a single 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. How many total time slices will it take to process half the file?

(Multiple Choice)
4.8/5
(47)

Class MyClass has a single ReentrantLock object, myLock. Suppose thread one calls myLock.lock() as it enters methodX and immediately completes its CPU time slice. What will happen as thread two calls methodY and attempts to call myLock.lock()?

(Multiple Choice)
4.8/5
(38)

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

(Multiple Choice)
4.9/5
(33)

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.9/5
(38)

Suppose run1 and run2 are objects of the class MyRunnable, which implements the Runnable interface. What is the result of the following calls? run1.run(); Run2.run();

(Multiple Choice)
4.8/5
(40)

Suppose thread one is downloading a 800KB file while another thread is processing the same file on a single 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. Approximately how does the time to complete the entire job compare to having a single thread do the work?

(Multiple Choice)
4.8/5
(34)
Showing 61 - 80 of 82
close modal

Filters

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