Shared Flashcard Set

Details

SCJP 6 Chapter 9
Flashcards for the Sierra/Bates SCJP 6 exam prep book. Chapter 9 - Threads
27
Software
Professional
04/17/2011

Additional Software Flashcards

 


 

Cards

Term
Answer the following pertaining to the yield() method: a) In what fully qualified class is it defined? b) Is it static or non-static? c) What, if any, exceptions does it throw? d) What does it return?
Definition
a) java.lang.Thread b) static c) none d) none - void method. (p. 726 and API)
Term
What does the yield() method do?
Definition
It may cause the currently running thread to go back to Runnable, to allow another thread a turn. This behavior is not guaranteed.
(p. 726)
Term
What three constants are declared in the Thread class to define thread priorities?
Definition
Thread.MIN_PRIORITY (1)
Thread.NORM_PRIORITY (5)
Thread.MAX_PRIORITY (10)
(p. 726)
Term
Answer the following pertaining to the join() method:
a) In what package is it defined?
b) Is it static or non-static?
c) What, if any, exceptions does it throw?
d) What does it return?
Definition
a) java.lang.Thread
b) non-static
c) InterruptedException
d) none - void method.
(p. 726 and API)
Term
What does the join() method do?
Definition
Takes the currently running thread and joins it to the end on which the join was invoked. The currently running thread is then blocked from becoming runnable until after the other thread is no longer alive.

For example, assuming that the following code was running in main:
Thread t = new Thread();
t.start();
t.join();
main would not complete until t had completed running and was no longer alive.
(p. 726)
Term
What does a call to a join method that takes a number of milliseconds as a parameter do?
Definition
The calling thread would wait until the thread it joined was no longer alive, or the number of milliseconds passed as a parameter had expired, whichever is sooner, before becoming runnable.
(p. 726)
Term
What are three methods one might invoke on a thread to cause the currently running thread to leave the Running state?
Definition
yield(), sleep(), and join().
(p. 727)
Term

Which of the following are possible thread states?

 

a. New

b. Ready

c. Running

d. Waiting/blocked/sleeping

e. Dead

Definition

a c d e

 

b. Ready should be "Runnable"

Term

Sleep is a ____ method of class Threads.

 

Definition
static
Term

If a threads sleep() expires it will immediately start running.

True or False?

Definition
False.
Term
Thread priorities are represented as a number between ___-___.
Definition
1-10
Term

thread priority is an ____ of Thread.

 

a. instance method

b. static method

c. instance and static method

Definition
a. instance method
Term

Which of the following are valid priorities?

 

a. MIN_PRIORITY

b. FLOOR_PRIORITY

c. AVG_PRIORITY

d. NORM_PRIORITY

e. CEILING_PRIORITY

f. TOP_PRIORITY

g. MAX_PRIORITY

Definition
a d g
Term
Fill in the blanks:
A ____ ____ can occur when multiple threads access the same resource in an operation that should be atomic, and one thread begins it work before another thread has completed.
Definition
race condition
(p. 733)
Term
T/F: With synchronization, you can guarantee that a single thread will stay running throughout an entire atomic operation
Definition
F: But it does guarantee that no other running thread can act on the same data
(p. 733)
Term
What are the two steps necessary to prevent data from being corrupted by concurrent access via multiple threads?
Definition
1) Mark the variables private
2) Synchronize the code that modifies the variables
(p. 733)
Term

T/F

 

When the wait() method is called on a thread the executing thread gives up it's lock immeadiatly.

 

When notify() is called on an object the executing thread gives up it's lock immeadiatly.

 

 

Definition

False - the wait() method is called on a synchronized object not on a thread

 

False - the notify method is called on a synchronized onject, but the executing thread will not give up it's lock until the thread moves out of the synchronized code.

 

 

Term
In what way should the wait() mechanism generally be called?  What would you code before calling wait()?
Definition

Wait should idealy be called from within a mechanism such as a a while loop, this way we can ensure that whatever reason the thread woke up will be valid, otherwise we will call wait() again if the condition has not been meet. 

 

This avoids spontaneous wakeup.

 

pg 755 bottom. 

Term

Match the following methods to the Class/ Interface.

 

wait ()

start()

yeild()

run()

sleep()

join()

notifyAll()

notify()

 

 

 

 

Class Object

Class Thread

Interface Runnable

Definition

Object - wait(), notify(), notifyAll()

Thread - start(), yeild(), sleep(), join()

Runnable - run()

 

pg 757

Term
T/F - Methods and classes can be synchronized.  Variables cannot be synchronized.
Definition
False.  Only methods (or blocks) can be synchronized, not variables or classes.(pg735)
Term
T/F - A thread can acquire more than one lock
Definition
True. A thread can acquire more than one lock. For example, a thread can enter a synchronized method, thus acquiring a lock, and then immediately invoke a synchronized method on a different object, thus acquiring that lock as well. (pg736)
Term
In regards to locking, how is declaring a synchronized block more flexible than a synchonized method?
Definition
When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired. But when you synchronize a block of code, you can specify which object's lock you want to use as the lock.  (pg 737)
Term
T/F - Threads calling static synchonized methods in the same class will always block each other.
Definition
True - They all lock on the same Class instance.(pg739)
Term
Which is thread-safe? StringBuffer or StringBuilder?
Definition
StringBuffer.

(p. 742)
Term
How can a deadlock be averted?

public class DeadlockRisk { 
   private static class Resource { 
     public int value; 
   } 
   private Resource resourceA = new Resource(); 
   private Resource resourceB = new Resource(); 
   public int read() { 
     synchronized(resourceA) { // May deadlock here 
       synchronized(resourceB) { 
         return resourceB.value + resourceA.value; 
       } 
     } 
   } 
 
   public void write(int a, int b) { 
     synchronized(resourceB) { // May deadlock here 
       synchronized(resourceA) { 
         resourceA.value = a; 
         resourceB.value = b; 
       } 
     } 
   } 
 } 
Definition
Synchronize resources in the same order.

(p. 745)
Term
When will this code continue running?

synchronized(anotherObject) {
   try {
      anotherObject.wait(); 
   } catch(InterruptedException e){}
}
Definition
When notify is called on anotherObject.

(p. 749)
Term
Does this code start a new Thread?

Thread t = new Thread();
t.run();
Definition
no creates a thread but does not start it.
Supporting users have an ad free experience!