Deck 20: Multithreading
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/82
Play
Full screen (f)
Deck 20: Multithreading
1
Given a single CPU and four threads, how many of the threads can execute in parallel?
A) Only 1 can execute at a time.
B) 2 can execute in parallel.
C) 3 can execute in parallel.
D) All 4 can execute in parallel.
A) Only 1 can execute at a time.
B) 2 can execute in parallel.
C) 3 can execute in parallel.
D) All 4 can execute in parallel.
A
2
The Runnable interface includes which method(s)?
I public void run(Runnable runnable)
II public void run()
III public void start()
A) I
B) II
C) I and II
D) II and III
I public void run(Runnable runnable)
II public void run()
III public void start()
A) I
B) II
C) I and II
D) II and III
B
3
When a sleeping thread is interrupted, a(n) ____________________ is generated.
A) ThreadInterruptedException
B) ThreadException
C) ThreadSleepException
D) InterruptedException
A) ThreadInterruptedException
B) ThreadException
C) ThreadSleepException
D) InterruptedException
D
4
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 { . . . }
A) II
B) III
C) I and II
D) II and III
I public interface MyRunnable extends Runnable { . . . }
II public class MyRunnable extends Runnable { . . . }
III public class MyRunnable implements Runnable { . . . }
A) II
B) III
C) I and II
D) II and III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
5
To start a thread, you should first construct an object from a class that implements the ____________ interface.
A) Thread
B) Threadable
C) Run
D) Runnable
A) Thread
B) Threadable
C) Run
D) Runnable
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
6
The Runnable interface has a single method called ____________.
A) run
B) start
C) sleep
D) wait
A) run
B) start
C) sleep
D) wait
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
7
Which constructor can be used to create a new thread associated with a Runnable object?
A) public Thread(Runnable r)
B) public Thread()
C) public Runnable(Thread t)
D) public Thread(String s)
A) public Thread(Runnable r)
B) public Thread()
C) public Runnable(Thread t)
D) public Thread(String s)
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
8
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
A) I
B) II
C) III
D) II and III
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
A) I
B) II
C) III
D) II and III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
9
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
A) I
B) II
C) I and III
D) II and III
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
A) I
B) II
C) I and III
D) II and III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
10
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()
A) I
B) II
C) III
D) II and III
I public void run(Runnable runnable)
II public void start(Runnable runnable)
III public void start()
A) I
B) II
C) III
D) II and III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
11
Given a two-CPU machine and four threads, how many of the threads can execute in parallel?
A) Only 1 can execute at a time.
B) 2 can execute in parallel.
C) 3 can execute in parallel.
D) All 4 can execute in parallel.
A) Only 1 can execute at a time.
B) 2 can execute in parallel.
C) 3 can execute in parallel.
D) All 4 can execute in parallel.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
12
When a thread is interrupted, the most common response is to terminate the ____________ method.
A) run
B) thread
C) sleep
D) await
A) run
B) thread
C) sleep
D) await
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following does not create an object that can run in a thread, assuming the following MyRunnable class declaration?
Public class MyRunnable implements Runnable { . . . }
I Runnable runnable = new Runnable();
II Runnable runnable = new MyRunnable();
III MyRunnable runnable = new MyRunnable();
A) I
B) III
C) I and II
D) II and III
Public class MyRunnable implements Runnable { . . . }
I Runnable runnable = new Runnable();
II Runnable runnable = new MyRunnable();
III MyRunnable runnable = new MyRunnable();
A) I
B) III
C) I and II
D) II and III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
14
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.
A) Thread
B) Runnable
C) MyRunnable
D) Threadable
A) Thread
B) Runnable
C) MyRunnable
D) Threadable
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
15
The ________ method stops the current thread for a given number of milliseconds.
A) run
B) await
C) sleep
D) delay
A) run
B) await
C) sleep
D) delay
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
16
Which exception must be caught or declared when calling the sleep method?
A) IOException
B) IllegalStateMonitorException
C) InterruptedException
D) SleepException
A) IOException
B) IllegalStateMonitorException
C) InterruptedException
D) SleepException
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
17
In which method are the tasks that are performed by a thread coded?
A) start
B) main
C) run
D) init
A) start
B) main
C) run
D) init
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
18
When a sleeping thread is interrupted, an InterruptedException is generated. Where do you catch that exception?
A) in the start() method.
B) in the sleep() method.
C) in the run() method.
D) in the interrupt() method.
A) in the start() method.
B) in the sleep() method.
C) in the run() method.
D) in the interrupt() method.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
19
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();
A) run1 and run2 execute as independent threads.
B) Syntax error
C) Only run1 executes as an independent thread.
D) run1 and run2 run sequentially, not as independent threads.
Run1.run();
Run2.run();
A) run1 and run2 execute as independent threads.
B) Syntax error
C) Only run1 executes as an independent thread.
D) run1 and run2 run sequentially, not as independent threads.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
20
Which method do you call to make a thread ineligible to run on the CPU for a set number of milliseconds?
A) start
B) wait
C) await
D) sleep
A) start
B) wait
C) await
D) sleep
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
21
In the initial release of the Java library, the Thread class had a stop method to terminate a thread. However, that method is now _______________
A) forbidden
B) not recommended
C) unwise
D) deprecated
A) forbidden
B) not recommended
C) unwise
D) deprecated
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
22
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
A) I
B) II
C) III
D) II and III
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
A) I
B) II
C) III
D) II and III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
23
Insert the statement that would start the following thread.
Thread firstThread = new Thread(myRunnable);
____________________
A) firstThread.run();
B) firstThread.start();
C) run();
D) start();
Thread firstThread = new Thread(myRunnable);
____________________
A) firstThread.run();
B) firstThread.start();
C) run();
D) start();
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
24
What course of action should be followed when a thread has been interrupted?
A) the thread should terminate
B) the thread should do what a programmer thinks is appropriate
C) the thread should go to sleep
D) the thread should call the Thread.interrupted method
A) the thread should terminate
B) the thread should do what a programmer thinks is appropriate
C) the thread should go to sleep
D) the thread should call the Thread.interrupted method
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
25
A(n) ____ uses a small number of threads to execute a large number of runnables.
A) embedded system
B) condition object
C) thread pool
D) race condition
A) embedded system
B) condition object
C) thread pool
D) race condition
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
26
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 see the first 10KB of the processed file?
A) one
B) two
C) eight
D) ten
A) one
B) two
C) eight
D) ten
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
27
Which of the following definitely indicates that a thread has been interrupted by another thread?
I The run method has completed
II The method Thread.interrupted returns true
III The run method catches an InterruptedException
A) I
B) II
C) I and II
D) II and III
I The run method has completed
II The method Thread.interrupted returns true
III The run method catches an InterruptedException
A) I
B) II
C) I and II
D) II and III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
28
What should be done to get the attention of a thread?
A) call the thread's stop method
B) call the thread's run method
C) call the thread's interrupt method
D) call the thread's sleep method
A) call the thread's stop method
B) call the thread's run method
C) call the thread's interrupt method
D) call the thread's sleep method
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
29
The sleep method is terminated with a(n) __________ whenever a sleeping thread is interrupted.
A) InterruptedException
B) SleepException
C) lock
D) SignalException
A) InterruptedException
B) SleepException
C) lock
D) SignalException
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
30
Each thread runs for a short amount of time, called a ____________________.
A) thread allotment
B) thread time slot
C) time slice
D) run time slice
A) thread allotment
B) thread time slot
C) time slice
D) run time slice
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
31
What is likely to be true when thread one is downloading a 1MB file while thread two is downloading another 1MB file?
A) The files download twice as fast as they would if one thread downloaded both files.
B) The files download half as fast as they would if one thread downloaded both files.
C) When thread one is half done, thread two is about half done.
D) The faster thread will interrupt the other thread.
A) The files download twice as fast as they would if one thread downloaded both files.
B) The files download half as fast as they would if one thread downloaded both files.
C) When thread one is half done, thread two is about half done.
D) The faster thread will interrupt the other thread.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
32
The ___________ method does not actually cause threads to terminate; it merely sets a Boolean field in the thread data structure.
A) stop
B) await
C) sleep
D) interrupt
A) stop
B) await
C) sleep
D) interrupt
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
33
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?
A) thirty
B) forty
C) eighty
D) hundred
A) thirty
B) forty
C) eighty
D) hundred
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
34
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?
A) The threaded way will be twice as fast.
B) The threaded way will be twice as slow.
C) The time depends heavily on the thread scheduler.
D) The ways will take about the same time.
A) The threaded way will be twice as fast.
B) The threaded way will be twice as slow.
C) The time depends heavily on the thread scheduler.
D) The ways will take about the same time.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
35
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?
A) accuracy of the results
B) speed to compete entire job
C) speed of availability of partial results
D) less memory is used
A) accuracy of the results
B) speed to compete entire job
C) speed of availability of partial results
D) less memory is used
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
36
Which argument(s) present(s) the best case(s) for using the Runnable interface rather than extending the Thread class?
I Thread sub-classes are harder to write
II Runnable objects can be passed into thread pools
III Less time is wasted in creating and destroying thread objects
A) I
B) II
C) II and III
D) I and II
I Thread sub-classes are harder to write
II Runnable objects can be passed into thread pools
III Less time is wasted in creating and destroying thread objects
A) I
B) II
C) II and III
D) I and II
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
37
The ____________ occurs when a thread that is not running is interrupted.
A) InterruptException
B) InterruptedException
C) ThreadException
D) ThreadTerminatedException
A) InterruptException
B) InterruptedException
C) ThreadException
D) ThreadTerminatedException
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
38
____ occur(s) if the effect of multiple threads on shared data depends on the order in which the threads are scheduled.
A) Pooling
B) Interrupted exceptions
C) Deadlocks
D) Race conditions
A) Pooling
B) Interrupted exceptions
C) Deadlocks
D) Race conditions
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
39
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
A) I
B) II
C) I and II
D) II and III
I The run method has completed
II The method Thread.interrupted returns true
III The run method catches an InterruptedException
A) I
B) II
C) I and II
D) II and III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
40
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?
A) 1
B) 2
C) 3
D) 4
A) 1
B) 2
C) 3
D) 4
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
41
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;
}
A) $10
B) $20
C) $0
D) a negative amount
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;
}
A) $10
B) $20
C) $0
D) a negative amount
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
42
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;
}
A) The balance could be zero or positive.
B) The balance is zero.
C) The balance could be zero or negative.
D) The balance is positive.
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;
}
A) The balance could be zero or positive.
B) The balance is zero.
C) The balance could be zero or negative.
D) The balance is positive.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
43
Calling the wait method in synchronized code is very similar to what action after locking with a ReentrantLock object?
A) Calling notify.
B) Locking a ReentrantLock object.
C) Calling await on a condition object.
D) Calling signalAll on a condition object.
A) Calling notify.
B) Locking a ReentrantLock object.
C) Calling await on a condition object.
D) Calling signalAll on a condition object.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
44
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.
A) run
B) lock
C) unlock
D) signalAll
A) run
B) lock
C) unlock
D) signalAll
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
45
If a thread sleeps after acquiring a _________, it blocks all other threads that want to acquire it.
A) condition
B) signal
C) lock
D) thread
A) condition
B) signal
C) lock
D) thread
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
46
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;
}
}
A) Thread one produced two 8's.
B) Thread two is getting longer time slices.
C) Thread one is getting longer time slices.
D) The race condition.
Public class SharedData
{
Private int value;
Public void setSharedData(int n)
{
Value = n;
}
Public int getSharedData()
{
Return value;
}
}
A) Thread one produced two 8's.
B) Thread two is getting longer time slices.
C) Thread one is getting longer time slices.
D) The race condition.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
47
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;
}
A) The calls were interleaved: thread one, thread two, thread one, thread two, …
B) The first call was to the deposit method.
C) The last call was to the withdraw method.
D) Each individual call to the deposit and withdraw methods ran to completion.
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;
}
A) The calls were interleaved: thread one, thread two, thread one, thread two, …
B) The first call was to the deposit method.
C) The last call was to the withdraw method.
D) Each individual call to the deposit and withdraw methods ran to completion.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
48
____ allow a thread to temporarily release a lock, so that another thread can proceed, and to regain the lock at a later time.
A) Condition objects
B) Embedded systems
C) Exceptions
D) Race conditions
A) Condition objects
B) Embedded systems
C) Exceptions
D) Race conditions
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
49
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();
}
A) None of them
B) III only
C) II and III only
D) All of them
/**
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();
}
A) None of them
B) III only
C) II and III only
D) All of them
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
50
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?
A) The bank account balances may no longer be correctly updated.
B) The printouts may become intermingled.
C) The printouts may no longer display the correct balances.
D) All of the above.
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?
A) The bank account balances may no longer be correctly updated.
B) The printouts may become intermingled.
C) The printouts may no longer display the correct balances.
D) All of the above.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
51
A waiting thread is blocked until another thread calls ____ on the condition object for which the thread is waiting.
A) await
B) signalAll
C) interrupt
D) lock
A) await
B) signalAll
C) interrupt
D) lock
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
52
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;
}
}
A) 1,2,3,4,5,6,7,8
B) 1,2,2,4,5,6,7,8
C) 1,2,3,4,5,6,8,8
D) 1,2,3,4,5,6,8,7
Public class SharedData
{
Private int value;
Public void setSharedData(int n)
{
Value = n;
}
Public int getSharedData()
{
Return value;
}
}
A) 1,2,3,4,5,6,7,8
B) 1,2,2,4,5,6,7,8
C) 1,2,3,4,5,6,8,8
D) 1,2,3,4,5,6,8,7
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
53
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. Suppose thread two receives values 1, 2, 3, 4, 5, 6, 7, 8 respectively on its calls. Should we expect the same values for each program run?
Public class SharedData
{
Private int value;
Public void setSharedData(int n)
{
Value = n;
}
Public int getSharedData()
{
Return value;
}
}
A) Yes, because the sleep times are equal in between calls.
B) Yes, because that's the same order used by thread one to set the values.
C) No, because thread one may not set the data as 1,2,3,4,5,6,7,8 respectively.
D) No, because a race condition may result in a different order.
Public class SharedData
{
Private int value;
Public void setSharedData(int n)
{
Value = n;
}
Public int getSharedData()
{
Return value;
}
}
A) Yes, because the sleep times are equal in between calls.
B) Yes, because that's the same order used by thread one to set the values.
C) No, because thread one may not set the data as 1,2,3,4,5,6,7,8 respectively.
D) No, because a race condition may result in a different order.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
54
The thread that calls signalAll must own the lock that belongs to the condition object on which signalAll is called. Otherwise, a(n) _______ ________ is thrown.
A) InterruptException
B) IllegalMonitorStateException
C) ThreadException
D) ThreadTerminatedException
A) InterruptException
B) IllegalMonitorStateException
C) ThreadException
D) ThreadTerminatedException
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
55
A(n) ____ object is used to control the threads that want to manipulate a shared resource.
A) condition
B) lock
C) interrupt
D) runnable
A) condition
B) lock
C) interrupt
D) runnable
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
56
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 ten times with values 1...10 respectively, sleeping for a random number of milliseconds between calls. Thread two calls getSharedData eight times, also sleeping for a random number of 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;
}
}
A) 8, 10
B) 10, 9
C) 10, 8
D) 8, 7
Public class SharedData
{
Private int value;
Public void setSharedData(int n)
{
Value = n;
}
Public int getSharedData()
{
Return value;
}
}
A) 8, 10
B) 10, 9
C) 10, 8
D) 8, 7
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
57
Which of the following statements is correct?
A) If a thread sleeps after acquiring a lock, it blocks all other threads that want to use the same lock.
B) When a thread calls await, it is simply deactivated in the same way as a thread that reaches the end of its time slice.
C) A thread pool 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.
D) Under no circumstances should you terminate a running thread.
A) If a thread sleeps after acquiring a lock, it blocks all other threads that want to use the same lock.
B) When a thread calls await, it is simply deactivated in the same way as a thread that reaches the end of its time slice.
C) A thread pool 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.
D) Under no circumstances should you terminate a running thread.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
58
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;
}
}
A) 9, 10
B) 10, 9
C) 1, 2
D) 8, 7
Public class SharedData
{
Private int value;
Public void setSharedData(int n)
{
Value = n;
}
Public int getSharedData()
{
Return value;
}
}
A) 9, 10
B) 10, 9
C) 1, 2
D) 8, 7
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
59
The ____ method is useful only if you know that a waiting thread can actually proceed.
A) run
B) signal
C) await
D) interrupted
A) run
B) signal
C) await
D) interrupted
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
60
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()
}
A) The balance could be zero or positive.
B) The balance is zero.
C) The balance could be zero or negative.
D) The balance is positive.
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()
}
A) The balance could be zero or positive.
B) The balance is zero.
C) The balance could be zero or negative.
D) The balance is positive.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
61
Under what circumstances will a call to signalAll not release a blocked thread that has called await?
A) When the thread is sleeping.
B) When the thread called await on different condition object.
C) When two or more threads are waiting.
D) When the thread called await on the same condition object.
A) When the thread is sleeping.
B) When the thread called await on different condition object.
C) When two or more threads are waiting.
D) When the thread called await on the same condition object.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
62
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?
A) The second thread should not try to acquire the lock.
B) The first thread should not try to acquire the lock.
C) Neither thread should use locks.
D) The first thread should release the lock after changing the data.
A) The second thread should not try to acquire the lock.
B) The first thread should not try to acquire the lock.
C) Neither thread should use locks.
D) The first thread should release the lock after changing the data.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
63
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;
}
A) The balance could be zero or positive.
B) The balance is zero.
C) The balance could be zero or negative.
D) The balance is positive.
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;
}
A) The balance could be zero or positive.
B) The balance is zero.
C) The balance could be zero or negative.
D) The balance is positive.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
64
Which of the following scenarios may not always cause a deadlock among two threads?
I Thread one is in an infinite loop and has acquired a lock
II Both threads are in an infinite loop, and one thread has acquired a lock
III Both threads are in an infinite loop, and both threads have acquired locks
A) I
B) II
C) III
D) I, II or III
I Thread one is in an infinite loop and has acquired a lock
II Both threads are in an infinite loop, and one thread has acquired a lock
III Both threads are in an infinite loop, and both threads have acquired locks
A) I
B) II
C) III
D) I, II or III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
65
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?
A) One method needs to acquire then release the lock after modifying the data.
B) Both methods need to acquire then release the lock after modifying or reading the data.
C) Never change any shared object data in multi-CPU machines.
D) Never use locks for any shared data in multi-CPU machines.
A) One method needs to acquire then release the lock after modifying the data.
B) Both methods need to acquire then release the lock after modifying or reading the data.
C) Never change any shared object data in multi-CPU machines.
D) Never use locks for any shared data in multi-CPU machines.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
66
Under what conditions are locks unnecessary for multi-threaded programs?
A) When the threads are all of the same class.
B) When the threads are different classes.
C) When the threads do not share data.
D) When the shared data only has a single method.
A) When the threads are all of the same class.
B) When the threads are different classes.
C) When the threads do not share data.
D) When the shared data only has a single method.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
67
When is it a good idea to call notifyAll in a synchronized method?
A) Whenever you exit it.
B) Upon exit, but only if threads before have called wait.
C) Upon exit, but only if the thread has replenished a needed resource.
D) Immediately upon entering the method.
A) Whenever you exit it.
B) Upon exit, but only if threads before have called wait.
C) Upon exit, but only if the thread has replenished a needed resource.
D) Immediately upon entering the method.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
68
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
A) I
B) I and II
C) II and III
D) I, II and III
I The two people are the threads
II The shared data is the telephone
III The state of the object is corrupt
A) I
B) I and II
C) II and III
D) I, II and III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
69
What happens when a thread calls the signalAll method of a Condition object connected to a lock, if no other thread had called await on that Condition object?
A) A compiler error.
B) A checked exception is thrown.
C) The unlock method call will block.
D) Nothing, the program executes normally.
A) A compiler error.
B) A checked exception is thrown.
C) The unlock method call will block.
D) Nothing, the program executes normally.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
70
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();
}
A) 0
B) 15 or 25
C) 45
D) -5
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();
}
A) 0
B) 15 or 25
C) 45
D) -5
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
71
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();
}
A) 0
B) 15 or 25
C) 45
D) -5
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();
}
A) 0
B) 15 or 25
C) 45
D) -5
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
72
What happens if we try to start a thread with an instance of a Runnable class that did not override the run method?
A) A compiler error.
B) A checked exception is thrown.
C) The inherited method runs and does nothing.
D) The thread sleeps indefinitely.
A) A compiler error.
B) A checked exception is thrown.
C) The inherited method runs and does nothing.
D) The thread sleeps indefinitely.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
73
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
A) I
B) I and II
C) I and III
D) I, II and III
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
A) I
B) I and II
C) I and III
D) I, II and III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
74
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?
A) As you run the task, break and check for events.
B) Ask the user if she is sure she wants to run the task.
C) Run the task in a separate thread.
D) Disable the GUI during the task.
A) As you run the task, break and check for events.
B) Ask the user if she is sure she wants to run the task.
C) Run the task in a separate thread.
D) Disable the GUI during the task.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
75
Exactly when does a thread finish in Java?
A) When its main method ends.
B) When its run method ends.
C) When it calls the method await.
D) When it is interrupted.
A) When its main method ends.
B) When its run method ends.
C) When it calls the method await.
D) When it is interrupted.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
76
What is the relationship between synchronized code and code that is locked using a ReentrantLock object?
A) synchronized code stops the race condition, locks do not.
B) synchronized is a single lock, but we may have many ReentrantLock objects.
C) The two are exactly the same.
D) The two concepts are not related.
A) synchronized code stops the race condition, locks do not.
B) synchronized is a single lock, but we may have many ReentrantLock objects.
C) The two are exactly the same.
D) The two concepts are not related.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
77
Which phrase best describes the purpose of a lock used in an object in one or more of its methods?
A) increased sharing
B) mutual exclusion
C) speed increase
D) privacy protection
A) increased sharing
B) mutual exclusion
C) speed increase
D) privacy protection
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
78
"Livelock" occurs when one thread runs continuously, while another thread never does. Which of the following is sufficient to cause livelock?
I Thread one is in an infinite loop
II Thread one possesses a lock and does not unlock it, but thread two requires the lock
III Thread one requires a lock, but thread two possesses the lock
A) I
B) II
C) I and II
D) I and III
I Thread one is in an infinite loop
II Thread one possesses a lock and does not unlock it, but thread two requires the lock
III Thread one requires a lock, but thread two possesses the lock
A) I
B) II
C) I and II
D) I and III
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
79
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()?
A) Thread two will wait for the lock.
B) Thread two will acquire the lock.
C) Deadlock will occur.
D) Thread two causes the IllegalStateMonitorException.
A) Thread two will wait for the lock.
B) Thread two will acquire the lock.
C) Deadlock will occur.
D) Thread two causes the IllegalStateMonitorException.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck
80
Class MyClass has two ReentrantLock objects, myLock1 and myLock2. Suppose thread one acquires myLock1 as it enters methodX and immediately completes its CPU time slice. After thread two enters methodY, it acquires myLock2 and tries to acquire myLock1. When thread one resumes, it tries to acquire myLock2. What will happen next?
A) Thread two will acquire myLock1.
B) Thread one will acquire myLock2.
C) Deadlock will occur.
D) Thread two will release myLock2.
A) Thread two will acquire myLock1.
B) Thread one will acquire myLock2.
C) Deadlock will occur.
D) Thread two will release myLock2.
Unlock Deck
Unlock for access to all 82 flashcards in this deck.
Unlock Deck
k this deck