Exam 20: 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 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; }

Free
(Multiple Choice)
4.8/5
(36)
Correct Answer:
Verified

D

The ____________ occurs when a thread that is not running is interrupted.

Free
(Multiple Choice)
4.9/5
(35)
Correct Answer:
Verified

B

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

Free
(Multiple Choice)
5.0/5
(36)
Correct Answer:
Verified

C

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.8/5
(47)

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

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

(Multiple Choice)
4.9/5
(35)

Under what circumstances will a call to signalAll not release a blocked thread that has called await?

(Multiple Choice)
4.8/5
(33)

The ________ method is called by a thread that has just changed the state of some shared data in a way that may benefit waiting threads.

(Multiple Choice)
4.9/5
(31)

The sleep method is terminated with a(n) __________ whenever a sleeping thread is interrupted.

(Multiple Choice)
4.8/5
(37)

Suppose thread one is downloading a large 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. What is the main benefit of using two threads rather than using a single thread to do both parts of the job?

(Multiple Choice)
4.8/5
(45)

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

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.8/5
(35)

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

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

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

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

The ___________ method does not actually cause threads to terminate; it merely sets a Boolean field in the thread data structure.

(Multiple Choice)
4.8/5
(41)

The _____________ interface is designed to encapsulate the concept of a sequence of statements that can run in parallel with other tasks, without equating it with the concept of a thread, a potentially expensive resource that is managed by the operating system.

(Multiple Choice)
4.9/5
(45)

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

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

(Multiple Choice)
4.8/5
(37)
Showing 1 - 20 of 82
close modal

Filters

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