Chapter 17


Chapter 16

1. 

Name and describe the methods declared by Thread that can cause a thread to stop/pause execution.

image from book

2. 

Name and describe the methods declared by Thread that might cause a thread to start/resume execution.

image from book

3. 

Name and describe the methods declared by Object that can cause a thread to stop/pause execution.

 - the only method defined by object that can cause a thread to stop or pause execution is wait().

4. 

Name and describe the methods declared by Object that might cause a thread to start/resume execution.

image from book

5. 

What is a race condition?

image from book

6. 

How can race conditions be prevented?

image from book

7. 

What is deadlock?

image from book

8. 

How can deadlock be prevented?

image from book

9. 

What is a selfish thread?

image from book

10. 

What are the two ways to construct a Thread?

image from book

11. 

What are the purposes of a ThreadGroup?

image from book

12. 

Under what circumstances may a thread be preempted by another thread?

image from book

13. 

On what does a synchronized non-static method synchronize?

 - a synchronized non-static method synchronizes on the object instance itself (`this`).

14. 

On what does a synchronized static method synchronize?

image from book

Answers

1. 

- The methods defined by Thread that can cause a thread to stop or pause execution are sleep() and yield().

2. 

- The only method defined by Thread that can cause a thread to start execution is the start() method. The only method defined by Thread that can cause a thread to resume execution is the interrupt() method.

3. 

- The only method defined by Object that can cause a thread to stop or pause execution is wait().

4. 

- The methods defined by Object that can cause a thread to resume execution are notify() and notifyAll(). No Object-defined method can cause a thread to start execution.

5. 

- A race condition is the situation where more than one thread accesses the same resource at the same time, resulting in errors and/or resource corruption.

6. 

- Race conditions can be prevented through the use of synchronization. This synchronizes access to shared resources.

7. 

- Deadlock is the situation where two or more threads remain permanently blocked by each other because they are waiting for locks that each other already holds. Without some sort of tie-breaking mechanism, the threads block forever, resources are tied up and the application loses whatever functionality the deadlocked threads had provided.

8. 

- Unfortunately, the Java language provides no facility for detecting or freeing deadlocked threads beyond shutting the JVM down. The best way to fix deadlock is to avoid it, so use extreme caution when writing multi-threaded programs where threads can hold multiple locks. Do not synchronize more than necessary and understand the various ways the threads you write might interact with each other. Keep the use of synchronization to a minimum and be especially careful about situations where a thread can hold multiple locks.

9. 

- A selfish thread is a thread with equal or higher priority than its competitors that never blocks or voluntarily releases the CPU. Such a thread may actually prevent its competitors from running until it finishes.

10. 

- One way to construct a Thread is to extend Thread and override its run method to do whatever the thread is supposed to do. The other way is to implement Runnable in another class, defining its run method to do whatever the thread is supposed to do, and pass an instance of that class into the Thread’s constructor.

11. 

- ThreadGroups organize threads into a tree-like structure primarily as a means of supporting security policies that might restrict access from one ThreadGroup to another. ThreadGroups also simplify the management of threads by providing methods that affect all the threads within a group.

12. 

- A thread may be preempted by another thread if the other thread has a higher priority or if the operating implements some sort of time-slicing method.

13. 

- A synchronized non-static method synchronizes on the object instance itself ("this").

14. 

- A synchronized static method synchronizes on the Class object associated with the class that defined the method.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net