Chapter 9

¡Supera tus tareas y exámenes ahora con Quizwiz!

What would happen if a thread's notify() method runs before another thread started waiting? This could happen, since we can't guarantee what order the different parts of the thread will execute in.

Almost always, when you want to wait for something, you also need to be able to check if it has already happened. Generally the best way to solve this is to put in some sort of loop that check on some sort of conditional expression, and only waits if the thing you're waiting for has not yet happened.

Whats the difference between waiting/blocked/sleeping?

Blocked - a thread is waiting for a resource, in which case the event that send it back to runnable is the availability of the resource. Sleeping - the thread's run code tells it to sleep for some period of time, in which case the even that send it back to runnable is that it wakes up because its sleep time has expired. Waiting - the thread's run code causes it to wait, in which case the even that send it back to runnable is that another thread sends a notification that it may no longer be necessary for the thread to wait.

What does it mean to mark a method synchronized?

By marking a method as synchronized we've guaranteed that once a thread starts its operations, other threads cannot enter that method until the current thread completes the process by exiting the method.

What does Deadlock mean?

Deadlock occurs when two threads are blocked, with each waiting for the other's lock. Neither can run until the other give up its lock, so they'll sit there forever. Regardless of how little chance there is for your code to deadlock, the bottom line is, if you deadlock, you're dead.

(T or F) A thread will resume Execution as soon as its sleep duration expires.

False

(T or F) Dead lock will not occur if wiat()/notify is used.

False

(T or F) Synchronization can prevent two objects from being accessed by the same thread

False

(T or F) The notify() method is overloaded to accept a duration

False

sleep() does NOT throw a checked exception

False

(T or F) A thread can invoke a wait or notify method on an object w/o owning that objects lock.

False, it has to own the objects lock

(T or F) If a threads goes to sleep, it releases any lock it has

False, it holds any lock it has

(T or F) When a thread's sleep() expires, and it wakes up, it will return to running?

False, it just goes back to the runnable state/ So sleep is the minimum duration in which the thread wont run, not not exact

(T or F) A thread is still considered alive after the run() method finishes.

False, there is also a isAlive() method to determine if a thread has been started but not yet completed

(T or F) when using Runnable, a thread of execution is generated

False, you still need a Thread Object to do the work

What is meant by the target when dealing with threads

If you create a thread using the no-arg constructor, the thread will call its own run() method when it's time to start working. The Runnable you pass to the Thread constructor is called the target or the target Runnable.

Explain a thread of execution

It is an individual process that has its own call stack. There is One Thread per call stack(one call stack per thread)

Which of the following methods are defined in the Thread class? start(), wait(), notify(), run(), terminate()

Just start() and run(), wait and notify are methods of the object class

Can Static synchronized methods and non-static synchronized methods block each other?

NO

Do Threads run in the order in which they are started?

NOOOOOO

Will a run(String) //(or some other param) execute in a separate call stack

Not unless you call it yourself, the Thread wont call the method for you, and even if you do call it yourself, execution wont happen in a new thread of execution with a separate call stack.

When calling start(), it is on the Runnable instance or Thread Instance()?

Thread instance

List a few overloaded constructors you can use from class Thread.

Thread(), Thread(Runnable target), Thread(Runnable target, String name), Thread(String name)

(T or F) If a class has both synchronized and non-synchronized methods, multiple threads can still access the class's non-synchronized methods.

True

(T or F) If a thread enters the runnable state, and it has a higher priority than any of the threads in the pool and a higher priority than the currently running thread, the lower-priority running thread usually will be bumped back to runnable and the highest-priority thread will be chosen to run.

True

(T or F) If two threads are about to execute a synchronized method in a class, and both threads are using the same instance of the class to invoke the method, only one thread at a time will be able to execute the method.

True

(T or F) Sleep() is a static method and can only put the currently running threads to sleep and not any other thread?

True

(T or F) The wait() method is overloaded to accept a duration

True

(T or F) Threads calling non-static synchronized methods in the same class will only block each other if they're invoked using the same instance.

True

(T or F) When the wait() method is invoked on an object, the thread executing that code give up its lock in the object immediately. However, when notify() is called, that doesn't mean the thread give up its lock at that moment. If the thread is still completing synchronized code, the lock is not released until the thread moves out of synchronized code. So just because notify() is called doesn't mean the lock becomes available at that moment.

True

Both wait() and notify() must be called from a synchronized context.

True

(T or F) You can synchronize a block of code rather than a method.

True, (Try writing this)

(T or F) Giving the same target to multiple threads means that several threads of execution will be running the very same job

True, (note: that same job will be done multiple times)

(T or F) You can pass a single Runnable instance to multiple Thread objects

True, The same Runnable becomes the target of multiple threads.

(T or F) Since there is only one lock per object, if one thread has picked up the lock, no other thread can pick up the lock until the first thread releases (or returns) the lock.

True, no other thread can enter the synchronized code until the lock has been released

Does wait(), notify(), and notifyAll() have to be called from within a synchronized context?

Yes

Should Access to static fields be done from static synchronized methods, and Access to non-static fields be done from non static fields?

Yes

Can you pass a Thread to another Threads Constructor?

Yes (Thread t = new Thread(new MyThread);

Can Static Methods be Synchronized?

Yes, There is only one copy of the static data you're trying to protect, so you only need one lock per class - a lock for the whole class. This can be done like a normal synchronized method: public static synchronized int getCount() { ... }. Or can also be done using a class literal:

What does notifyAll() do?

You can use notifyAll() on the object to let all the threads rush out of the waiting area and back to runnable. All of the threads will be notified and start competing to get the lock. As the lock is used and released by each thread, all of them will get into action without a need for further notification.

sleep()

causes the thread to definitely stop executing for a given amount of time; if no other thread or process needs to be run

join()

non-static join() method of class Thread lets one thread "join onto the end" of another thread.

What does the getId() method return?

positive, unique, long number (that number will be that thread's only ID number for the thread's entire life)

What method do you write your code in if needed in a separate thread

run() method

yield()

supposed to make the currently running thread head back to runnable to allow other threads of the same priority to get their turn. In reality, though, the yield() method isn't guaranteed to do what it claims, and even if yield() does cause a thread to step out of running and back to runnable, there's no guarantee the yielding thread won't just be chosen again over all the others. A yield() won't ever cause a thread to go to the waiting/blocked/sleeping state. At most, a yield() will cause a thread to go from running to runnable.

(T or F) An instance of Thread is just an object with its own variables and methods

true

(T or F) Very little is guaranteed when it comes to threads

true

How do you start a thread but tell it not to run until some other thread has finished.

use the Join() method.

How to you get the actual thread/new call stack(make it a thread of execution)

use the start() method

Besides sleep/yield/join what other ways can a thread leave the running state?

• The thread's run() method completes. • A call to wait() on an object (we don't wait() on a thread) • A thread can't acquire the lock on the object whose method code it's attempting to run. • The thread scheduler can decide to move the current thread from running to runnable in order to give another thread a chance to run.

What is a class literal?

A special feature in the Java language that tells the compiler (who tells the JVM): go and find me the instance of Class that represents the class called MyClass.

wait() does NOT throw a checked exception

False

(T or F) A thread cannot acquire more than one lock.

False, EX: 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.

(T or F) For synchronized blocks, Threads that synchronize on the same object will not block each other. Threads that synchronize on different objects will?

False, Threads that synchronize on the same object will block each other. Threads that synchronize on different objects will not.

(T or F) You can call start() on a thread after its no longer a thread of execution.

False, a thread can never be started a second time. (This calls an RuntimeException/IllegalThreadStateException

(T or F) The thread of execution(the new call stack) begins by invoking start()

False, invoke run()

(T or F) Threads live and die on the stack

False, the do so on the heap

How do you get the name of the current running thread.

First invoke the static Thread.currentThread() method, which returns a reference to the currently executing thread, and then invoke getName() on that return reference.

Write code to instantiate a thread.

For extending Thread class: MyThread t = new MyThread(); For implement Runnable: MyRunnable r = new MyRunnable(); next.......Thread t = new Thread(r);

What three methods makes a running thread leave the running state?

1. A call to sleep() - Guaranteed to cause the current thread to stop executing for at least the specified duration. 2. A call to yield() - Not guaranteed to do much of anything, although typically it will cause the currently running thread to move back to runnable so that a thread of the same priority can have a chance. 3. A call to join() - Guaranteed to cause the current thread to stop executing until the thread it joins with completes, or if the thread it's try to join with is not alive, however, the current thread won't need to back out.

What 3 things happens when you call start()

1. A new thread of execution starts(with a new call stack) 2. The thread moves from the new state to the runnable state 3. When the thread gets a chance to execute, its target run() method will run

How do you define and instantiate a thread

1. Extend the java.lang.Thread 2. Implement the Runnable interface

List the 5 Thread states and explain them

1. New - the state the thread is in after the Thread instance has been created, but the start() method has not been invoked on the thread. 2. Runnable - The state a thread is in when it's eligible to run, but the scheduler has not selected it to be the running thread. 3. Running - This is where the action happens. This is the state a thread is in when the thread scheduler selects it (from the runnable pool) to be the currently executing process. 4. Waiting/blocked/sleeping - The state a thread is in when it's not eligible to run. 5. Dead - A thread is considered dead when its run() method completes. It may still be a viable Thread object, but it is no longer a separate thread of execution.

How might a scheduler handle threads of equal priority?

1. Pick a thread to run, and run it there until it blocks or completes. 2. Time slice the threads in the pool to give everyone an equal opportunity to run.

How many static constants (final variables) does the Thread class have that define the range of thread priorities?

1. Thread.MIN_PRIORITY(1) 2. Thread.NORM_PRIORITY(5) 3. Thread.MAX_PRIORITY(10)

Thread-related methods from the java.lang.Object:

1. public final void wait() throws InterruptedException //this has 3 overloaded versions 2. public final void notify() 3. public final void notifyAll()

Methods from the java.lang.Thread class that can help influence thread scheduling:

1. public static void sleep(Long milliseconds) throws InterruptedException 2. public static void yield() 3. public final void join() throws InterruptedException 4. public final void setPriority(int newPriority)

How do you set a threads priority?

A thread gets a default priority that is the priority of the thread of execution that creates it. You can also set a thread's priority directly by calling the setPriority() method on a Thread instance. Ex : FooRunnable r = new FooRunnable(); Thread t = new Thread(r); t.setPriority(8); t.start();

What does it mean when code is said to be executing in synchronized context?

A thread is executing code from within a synchronized block, including any method code invoked from that synchronized block


Conjuntos de estudio relacionados

Ch 18 Management of Patients with Upper Respiratory Tract Disorders

View Set

3.2: Constitutional Monarchy in England

View Set