Deck 22: Multithreadingetext Only

Full screen (f)
exit full mode
Question
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
Use Space or
up arrow
down arrow
to flip the card.
Question
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.Only I
B.Only III
C.I and II
D.II and III
Question
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.
Question
Which exception must be caught or declared when calling the sleep method?
A.IOException
B.IllegalStateMonitorException
C.InterruptedException
D.SleepException
Question
When a thread is interrupted, the most common response is to terminate the ____________ method.
A.run
B.thread
C.sleep
D.await
Question
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.
Question
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.Only II
B.Only III
C.I and II
D.II and III
Question
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.
Question
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)
Question
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.Only I
B.Only II
C.I and III
D.II and III
Question
In which method are the tasks that are performed by a thread coded?
A.start
B.main
C.run
D.init
Question
When a sleeping thread is interrupted, a(n) ____________________ is generated.
A.ThreadInterruptedException
B.ThreadException
C.ThreadSleepException
D.InterruptedException
Question
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
Question
The ________ method stops the current thread for a given number of milliseconds.
A.run
B.await
C.sleep
D.delay
Question
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
Question
The Runnable interface includes which method(s)?
I public void run(Runnable runnable)
II public void run()
III public void start()
A.Only I
B.Only II
C.I and II
D.II and III
Question
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
Question
If you do not use the Runnable interface, 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.Only I
B.Only II
C.Only III
D.I and III
Question
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.Only I
B.Only II
C.Only III
D.II and III
Question
Insert the statement that would start the following thread.
Thread firstThread = new Thread(myRunnable);
____________________
A.firstThread.run();
B.firstThread.start();
C.run();
D.start();
Question
Suppose thread one is downloading a large file while another thread is processing the same file on a single CPU machine.The time required to download and process a portion of the file is the same.The second thread can process the file while it is incomplete.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
Question
The sleep method is terminated with a(n) __________ whenever a sleeping thread is interrupted.
A.InterruptedException
B.SleepException
C.lock
D.SignalException
Question
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.The second thread can process the file while it is incomplete.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
Question
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.Only I
B.Only II
C.I and II
D.II and III
Question
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
Question
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.
Question
Which of the following is a characteristic of threads?
A.The order of the execution of parallel threads is somewhat random
B.When running multiple parallel threads, the threads are exactly interleaved
C.The thread scheduler executes the threads in the same order as its call
D.Parallel threads run exactly at the same time and in the same order
Question
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? 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?  <div style=padding-top: 35px>
Question
Given that a new thread named t has been constructed, how can you make multiple threads run parallel to each other?
A.t.run()
B.t.start()
C.t.parallel()
D.t.thread()
Question
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.The second thread can process the file while it is incomplete.How many total time slices will it take for the first 10 KB to be downloaded and processed?
A.one
B.two
C.eight
D.ten
Question
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.
Question
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.Only I
B.Only II
C.Only III
D.II and III
Question
What is the
Question
The ____________ occurs when a thread that is not running is interrupted.
A.InterruptException
B.InterruptedException
C.ThreadException
D.ThreadTerminatedException
Question
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
Question
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.Only I
B.Only II
C.I and II
D.II and III
Question
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
Question
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
Question
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.The second thread can process the file while it is incomplete.How many total time slices will it take to process half the file?
A.thirty
B.forty
C.eighty
D.hundred
Question
A program that creates a large number of short-lived threads can be inefficient in memory and time.Which of the following can reduce the cost of using threads?
A.Thread pool
B.runnable object
C.await
D.Long-lived threads
Question
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
Question
The ____ method is useful only if you know that a waiting thread can actually proceed.
A.run
B.signal
C.await
D.interrupted
Question
A(n) ____ object is used to control the threads that want to manipulate a shared resource.
A.condition
B.lock
C.interrupt
D.runnable
Question
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? 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?   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.<div style=padding-top: 35px>
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.
Question
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? 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?   A.8, 10 B.10, 9 C.10, 8 D.8, 7<div style=padding-top: 35px>
A.8, 10
B.10, 9
C.10, 8
D.8, 7
Question
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? 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?   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<div style=padding-top: 35px>
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
Question
Consider the following change to the deposit method in Section 22.4: Consider the following change to the deposit method in Section 22.4:   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.<div style=padding-top: 35px> 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.
Question
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? 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?   A.$10 B.$20 C.$0 D.a negative amount<div style=padding-top: 35px>
A.$10
B.$20
C.$0
D.a negative amount
Question
____ 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
Question
What is the condition called where the effect of multiple threads on shared data depends on the order in which the threads are scheduled?
A.Lock condition
B.Race condition
C.Run condition
D.Synchronizing access condition
Question
____ 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
Question
The term "starvation" means a situation where when one thread runs continuously, while another thread never does.Consider the following three conditions (list I,II,III).Which set of these conditions is sufficient to cause starvation?
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.Only I
B.Only II
C.I and II
D.I and III
Question
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? 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?   A.9, 10 B.10, 9 C.1, 2 D.8, 7<div style=padding-top: 35px>
A.9, 10
B.10, 9
C.1, 2
D.8, 7
Question
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? 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?   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.<div style=padding-top: 35px>
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.
Question
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
Question
Consider the addFirst method of the LinkedList class in Chapter 16: Consider the addFirst method of the LinkedList class in Chapter 16:   A.Only I B.I and II C.Only III D.I and III<div style=padding-top: 35px>
A.Only I
B.I and II
C.Only III
D.I and III
Question
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.
Question
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
Question
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? 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?   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<div style=padding-top: 35px>
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
Question
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
Question
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.Only I
B.I and II
C.II and III
D.I, II, and III
Question
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.
Question
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.
Question
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
A.Only I
B.I and II
C.I and III
D.I, II, and III
Question
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.
Question
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 a
Question
Assume three threads share a BankAccount object with a balance initially zero (0), a ReentrantLock named myLock, and a condition object on myLock named insufficientFunds, as shown below.Thread one calls withdraw(30), then thread two calls withdraw(20) and thread three calls deposit(45).What is the balance after the three calls and after the waiting threads have had a chance to run? Assume three threads share a BankAccount object with a balance initially zero (0), a ReentrantLock named myLock, and a condition object on myLock named insufficientFunds, as shown below.Thread one calls withdraw(30), then thread two calls withdraw(20) and thread three calls deposit(45).What is the balance after the three calls and after the waiting threads have had a chance to run?   A.0 B.15 or 25 C.45 D.-5<div style=padding-top: 35px>
A.0
B.15 or 25
C.45
D.-5
Question
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.
Question
lock objects ensure that shared data are in a consistent state when several threads access them.What is a deadlock?
A.When multiple threads try to use the same shared data at the same time, and the threads become undone
B.When multiple threads copy the same shared data in order to proceed, consequently leaving the original lock object useless
C.When multiple threads intertwine multiple times and the lock object is lost, or unreachable
D.When multiple threads are not proceeding because they are currently waiting to acquire the same lock
Question
Why does the textbook recommend signallAll over the signal method?
A.The signalAll method is always more efficient
B.signal can lead to deadlocks when not every waiting thread is able to proceed
C.signalAll notifies the waiting threads that sufficient funds is absolutely available, while signal only notifies one at a time
D.None of the above
Question
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.
Question
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.Only I
B.I and II
C.I and III
D.I, II, and III
Question
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
Question
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? 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?   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.<div style=padding-top: 35px>
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.
Question
Assume three threads share a BankAccount object with a balance initially zero (0), a ReentrantLock named myLock, and a condition object on myLock named insufficientFunds, as shown below.Thread one calls withdraw(30), then thread two calls withdraw(20) and thread three calls deposit(45).What is the balance after the three calls? Assume three threads share a BankAccount object with a balance initially zero (0), a ReentrantLock named myLock, and a condition object on myLock named insufficientFunds, as shown below.Thread one calls withdraw(30), then thread two calls withdraw(20) and thread three calls deposit(45).What is the balance after the three calls?   A.0 B.15 or 25 C.45 D.-5<div style=padding-top: 35px>
A.0
B.15 or 25
C.45
D.-5
Question
The term "stale data" refers to a situation 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.
Question
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.
Question
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.Only I
B.Only II
C.Only III
D.I, II, or III
Question
The term "stale data" refers to a situation 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
Question
Calling signalAll without locking the object is a common error.What kind of exception would be thrown?
A.IllegalArgumentException
B.IllegalStateException
C.IllegalMonitorStateException
D.IllegalFormatException
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/81
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 22: Multithreadingetext Only
1
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
sleep
2
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.Only I
B.Only III
C.I and II
D.II and III
Only I
3
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 and run2 run sequentially, not as independent threads.
4
Which exception must be caught or declared when calling the sleep method?
A.IOException
B.IllegalStateMonitorException
C.InterruptedException
D.SleepException
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
5
When a thread is interrupted, the most common response is to terminate the ____________ method.
A.run
B.thread
C.sleep
D.await
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
6
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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
7
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.Only II
B.Only III
C.I and II
D.II and III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
8
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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
9
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)
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
10
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.Only I
B.Only II
C.I and III
D.II and III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
11
In which method are the tasks that are performed by a thread coded?
A.start
B.main
C.run
D.init
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
12
When a sleeping thread is interrupted, a(n) ____________________ is generated.
A.ThreadInterruptedException
B.ThreadException
C.ThreadSleepException
D.InterruptedException
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
13
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
14
The ________ method stops the current thread for a given number of milliseconds.
A.run
B.await
C.sleep
D.delay
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
15
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
16
The Runnable interface includes which method(s)?
I public void run(Runnable runnable)
II public void run()
III public void start()
A.Only I
B.Only II
C.I and II
D.II and III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
17
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
18
If you do not use the Runnable interface, 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.Only I
B.Only II
C.Only III
D.I and III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
19
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.Only I
B.Only II
C.Only III
D.II and III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
20
Insert the statement that would start the following thread.
Thread firstThread = new Thread(myRunnable);
____________________
A.firstThread.run();
B.firstThread.start();
C.run();
D.start();
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
21
Suppose thread one is downloading a large file while another thread is processing the same file on a single CPU machine.The time required to download and process a portion of the file is the same.The second thread can process the file while it is incomplete.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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
22
The sleep method is terminated with a(n) __________ whenever a sleeping thread is interrupted.
A.InterruptedException
B.SleepException
C.lock
D.SignalException
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
23
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.The second thread can process the file while it is incomplete.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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
24
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.Only I
B.Only II
C.I and II
D.II and III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
25
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
26
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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
27
Which of the following is a characteristic of threads?
A.The order of the execution of parallel threads is somewhat random
B.When running multiple parallel threads, the threads are exactly interleaved
C.The thread scheduler executes the threads in the same order as its call
D.Parallel threads run exactly at the same time and in the same order
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
28
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? 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?
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
29
Given that a new thread named t has been constructed, how can you make multiple threads run parallel to each other?
A.t.run()
B.t.start()
C.t.parallel()
D.t.thread()
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
30
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.The second thread can process the file while it is incomplete.How many total time slices will it take for the first 10 KB to be downloaded and processed?
A.one
B.two
C.eight
D.ten
Unlock Deck
Unlock for access to all 81 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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
32
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.Only I
B.Only II
C.Only III
D.II and III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
33
What is the
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
34
The ____________ occurs when a thread that is not running is interrupted.
A.InterruptException
B.InterruptedException
C.ThreadException
D.ThreadTerminatedException
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
35
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
36
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.Only I
B.Only II
C.I and II
D.II and III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
37
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
38
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
39
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.The second thread can process the file while it is incomplete.How many total time slices will it take to process half the file?
A.thirty
B.forty
C.eighty
D.hundred
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
40
A program that creates a large number of short-lived threads can be inefficient in memory and time.Which of the following can reduce the cost of using threads?
A.Thread pool
B.runnable object
C.await
D.Long-lived threads
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
41
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
42
The ____ method is useful only if you know that a waiting thread can actually proceed.
A.run
B.signal
C.await
D.interrupted
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
43
A(n) ____ object is used to control the threads that want to manipulate a shared resource.
A.condition
B.lock
C.interrupt
D.runnable
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
44
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? 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?   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.
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 81 flashcards in this deck.
Unlock Deck
k this deck
45
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? 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?   A.8, 10 B.10, 9 C.10, 8 D.8, 7
A.8, 10
B.10, 9
C.10, 8
D.8, 7
Unlock Deck
Unlock for access to all 81 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.Which of the following orders of values is not possible for thread two to receive? 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?   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
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 81 flashcards in this deck.
Unlock Deck
k this deck
47
Consider the following change to the deposit method in Section 22.4: Consider the following change to the deposit method in Section 22.4:   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. 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 81 flashcards in this deck.
Unlock Deck
k this deck
48
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? 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?   A.$10 B.$20 C.$0 D.a negative amount
A.$10
B.$20
C.$0
D.a negative amount
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
49
____ 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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
50
What is the condition called where the effect of multiple threads on shared data depends on the order in which the threads are scheduled?
A.Lock condition
B.Race condition
C.Run condition
D.Synchronizing access condition
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
51
____ 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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
52
The term "starvation" means a situation where when one thread runs continuously, while another thread never does.Consider the following three conditions (list I,II,III).Which set of these conditions is sufficient to cause starvation?
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.Only I
B.Only II
C.I and II
D.I and III
Unlock Deck
Unlock for access to all 81 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 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? 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?   A.9, 10 B.10, 9 C.1, 2 D.8, 7
A.9, 10
B.10, 9
C.1, 2
D.8, 7
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
54
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? 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?   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.
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 81 flashcards in this deck.
Unlock Deck
k this deck
55
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
56
Consider the addFirst method of the LinkedList class in Chapter 16: Consider the addFirst method of the LinkedList class in Chapter 16:   A.Only I B.I and II C.Only III D.I and III
A.Only I
B.I and II
C.Only III
D.I and III
Unlock Deck
Unlock for access to all 81 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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
58
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
59
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? 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?   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
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
60
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
61
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.Only I
B.I and II
C.II and III
D.I, II, and III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
62
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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
63
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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
64
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
A.Only I
B.I and II
C.I and III
D.I, II, and III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
65
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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
66
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 a
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
67
Assume three threads share a BankAccount object with a balance initially zero (0), a ReentrantLock named myLock, and a condition object on myLock named insufficientFunds, as shown below.Thread one calls withdraw(30), then thread two calls withdraw(20) and thread three calls deposit(45).What is the balance after the three calls and after the waiting threads have had a chance to run? Assume three threads share a BankAccount object with a balance initially zero (0), a ReentrantLock named myLock, and a condition object on myLock named insufficientFunds, as shown below.Thread one calls withdraw(30), then thread two calls withdraw(20) and thread three calls deposit(45).What is the balance after the three calls and after the waiting threads have had a chance to run?   A.0 B.15 or 25 C.45 D.-5
A.0
B.15 or 25
C.45
D.-5
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
68
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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
69
lock objects ensure that shared data are in a consistent state when several threads access them.What is a deadlock?
A.When multiple threads try to use the same shared data at the same time, and the threads become undone
B.When multiple threads copy the same shared data in order to proceed, consequently leaving the original lock object useless
C.When multiple threads intertwine multiple times and the lock object is lost, or unreachable
D.When multiple threads are not proceeding because they are currently waiting to acquire the same lock
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
70
Why does the textbook recommend signallAll over the signal method?
A.The signalAll method is always more efficient
B.signal can lead to deadlocks when not every waiting thread is able to proceed
C.signalAll notifies the waiting threads that sufficient funds is absolutely available, while signal only notifies one at a time
D.None of the above
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
71
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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
72
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.Only I
B.I and II
C.I and III
D.I, II, and III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
73
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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
74
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? 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?   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.
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 81 flashcards in this deck.
Unlock Deck
k this deck
75
Assume three threads share a BankAccount object with a balance initially zero (0), a ReentrantLock named myLock, and a condition object on myLock named insufficientFunds, as shown below.Thread one calls withdraw(30), then thread two calls withdraw(20) and thread three calls deposit(45).What is the balance after the three calls? Assume three threads share a BankAccount object with a balance initially zero (0), a ReentrantLock named myLock, and a condition object on myLock named insufficientFunds, as shown below.Thread one calls withdraw(30), then thread two calls withdraw(20) and thread three calls deposit(45).What is the balance after the three calls?   A.0 B.15 or 25 C.45 D.-5
A.0
B.15 or 25
C.45
D.-5
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
76
The term "stale data" refers to a situation 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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
77
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.
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
78
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.Only I
B.Only II
C.Only III
D.I, II, or III
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
79
The term "stale data" refers to a situation 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
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
80
Calling signalAll without locking the object is a common error.What kind of exception would be thrown?
A.IllegalArgumentException
B.IllegalStateException
C.IllegalMonitorStateException
D.IllegalFormatException
Unlock Deck
Unlock for access to all 81 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 81 flashcards in this deck.