TIJ-3rd-edition-html, 13: Concurrency
About this set
Created by:
lghrants on February 8, 2011
Subjects:
programming, java, concurrency
Description:
Title: Thinking in Java, 3rd ed. Revision 4.0
Author: Bruce Eckel
Order by
54 terms
Terms | Definitions |
|---|---|
Objects provide a way to divide a program into independent sections. Often, you also need to turn a program into separate, independently running subtasks. Each of these subtasks is called a _____. | A thread |
A _____ is a self-contained running program with it's own address space. | A process |
A _____ OS is capable of running more than one process (program) at a time, while making it lok like each one is chugging along on its own, by periodically switching the CPU from one task to another. | A multitasking OS |
A _____ is a single, sequential flow of control within a process. | A thread |
A single process (program) can have multiple concurrently executing _____. | Threads |
One of the most compelling reasons for concurrency is to produce a responsive _____ _____. | User interface |
The simplest way to create a thread is to inherit from _____ which has all the wiring necessary to create and run threads. | java.lang.Thread |
The most important method for java.lang.Thread is _____, which you must override to make the thread do your bidding. | java.lang.Thread.run() |
A java.lang.Thread objects run() method is virtually always has some kind of _____ that continues until the thread is no longer necessary. | Loop |
Which method of the java.lang.Thread class performs special initialization for a thread and then automatically calls the run() method? | java.lang.Thread.start() |
Which class "registers" itself (meaning you don't have to save a reference), preventing the garbage collector from cleaning it up until it's run() method exits? | java.lang.Thread |
If you've accomplished what you need to in your java.lang.Thread.run() method, you can give a hint to the thread scheduling mechanism that you've done enough and that some other thread might as well have the CPU. This is done by calling which method? | java.lang.Thread.yield() |
One way to control the behavior of your threads is by calling _____ to cease execution for a given number of milliseconds. | java.lang.Thread.sleep() |
The _____ of a thread tells the scheduler how important the thread is. | priority |
When you call java.lang.Thread.sleep(), why must it be placed inside a try block? | Because it's possible for sleep() to be interrupted before it times out (if, for instance, someone else has a reference to the thread and they call interrupt() on it) |
Usually, if you're going to break out of a suspended thread using java.lang.Thread.interrupt() you will use _____ rather than sleep()? | java.lang.Thread.wait() |
If you must control the order of execution of your java.lang.Threads, your best bet is not to use threads at all, but instead to do what? | Write your own set of cooperative routines that had control to each other in a specified order |
You can read the priority of an existing java.lang.Thread with which of it's methods? | java.lang.Thread.getPriority() |
You can change the priority of a java.lang.Thread at any time by using which of it's methods? | java.lang.Thread.setPriority() (not just for use in the constructor) |
Although the JDK has _____ priority levels, this doesn't map well to many operating systems (Win 2k has 7, Sun Solaris has 2^31). | 10 priority levels |
The only really portable approach to setting priority levels is to use which constants (in lieu of manually setting priorities of 1 - 10)? | java.lang.Thread.MAX_PRIORITY / NORM_PRIORITY / MIN_PRIORITY |
A _____ thread is on that is supposed to provide a general service in the background as long as a program is running, but is not part of the essence of the program. | A daemon thread. |
To declare a java.lang.Thread as a daemon thread, you must call which method before you call start()? | java.lang.Thread.setDaemon() |
You can find out if a java.lang.Thread is a daemon thread by calling which of it's functions? | java.lang.Thread.isDaemon() |
True or False: If a java.lang.Thread is a daemon thread, then any threads that it spawns will also be daemons. | True |
If the only still-running java.lang.Threads in a program are daemon threads, what happens to the program? | It terminates |
One java.lang.Thread may call _____ on another thread to wait for the second thread to complete before proceeding. | java.lang.Thread.join() |
You may call java.lang.Thread.join() with a timeout argument (in either milliseconds or milliseconds and nanoseconds) so that if the target thread doesn't finish in that period of time, what happens? | The call to java.lang.Thread.join() returns anyway |
The call the java.lang.Thread.join() may be aborted by calling _____ on the calling thread, so a try-catch clause is required. | java.lang.Thread.interrupt() |
If your class is already inheriting from another class, and you want it to be able to behave as a java.lang.Thread, you could implement which interface instead? | java.lang.Runnable |
java.lang.Runnable specifies only that there be a _____ method implemented (java.lang.Thread implements Runnable). | java.lang.Runnable.run() |
To produce a thread from a java.lang.Runnable object, what must you do? | Create a new java.lang.Thread and hand your java.lang.Runnable object to it's specialized constructor |
The java.util.Timer class is, by default, a _____ thread. | A non-daemon thread |
True or False: The default behavior of the java.util.Timer class is to prevent a process from terminating, if it is the only code still running. | True; it is NOT a daemon thread by default (you CAN declare it as one though) |
The java.util.Timer.schedule() must be handed which class of objects in order to perform useful work in the future? | java.util.TimerTask |
Using _____ is generally simpler and clearer that writing the code with an explicit sleep(). In addition, it is designed to scale to large numbers of concurrently scheduled tasks (in the thousands), so it can be a very useful tool. | java.util.Timer |
Java has built-in support to prevent collisions over resources in the form of the _____ keyword. | The synchronized keyword |
If a thread is inside one of an objects synchronized methods, all other threads are blocked from entering any of the synchronized methods of the object until what happens? | The first thread (called inside a synchronized method) returns from its call |
Each object contains a single lock (used by the synchronized keyword), referred to as a _____, that is automatically part of the object (you don't have to write any special code). | A monitor |
In addition to a single object's synchronized lock, there's also a single lock per class (as part of the Class object for the class), so that the _____ _____ methods can lock each other out from simultaneous access of static data on a class-wide basis. | synchronized static |
As a general rule of thumb, if you need to synchronize one method in a class, you should do what? | You should synchronize them all; it's often difficult to tell for sure if a method will be negatively affected if you leave it out |
The _____ keyword is not part of the method signature, so you can add this thread-safe keyword and not worry about it limiting method overriding. | The synchronized keyword |
Sometimes, you only want to prevent multiple thread access to part of the code inside a method instead of the entire method; the section of code you want to isolate this way is called a _____ _____. | A critical section |
What is the following:synchronized(snycObject){ //this code can be accessed by only one thread at a time } | A synchronized block (a.k.a a critical section); before it can be entered, the lock must be acquired on syncObject |
java.lang.Threads can be in any one of four states. What are they? | New, Runnable, Dead, or Blocked |
Which java.lang.Thread state can be defined as, "The thread object has been created, but it hasn't been started yet, so it cannot run"? | New |
Which java.lang.Thread state can be defined as, "The thread can be run when the time-slicing mechanism has CPU cycles available for the thread"? | Runnable (it might or might not be running at the moment, but there's nothing to prevent it from being run if the scheduler can arrange it; it's not dead or blocked) |
Which java.lang.Thread state can be defined as, "The state in which a thread is after it's run() method has returned"? | Dead |
Which java.lang.Thread state can be defined as, "The thread could be run, but something is preventing it"? | Blocked |
After understanding that threads can collide with each other, the next step is to learn how to make threads cooperate with each other. The key to doing this is by handshaking between threads, which is safely implemented with the _____ class's wait() and notify() methods. | The Object class |
It is important to understand that sleep() does not release the lock on an object when it is called. On the other hand, the method _____ DOES release the lock, which means that other synchronized methods in the thread object can be called. | java.lang.Object.wait() |
Where do wait(), notify(), and notifyAll() live in Java? | java.lang.Object (because they manipulate the lock that's part of every object) |
Where MUST you place a call to java.lang.Object.wait()/notify()/notifyAll()? | Inside a synchronized method or block (because any thread calling them must "own" (acquire) the lock for the object before these methods can be called) |
If you call java.lang.Object.wait()/notify()/notifyAll() outside a synchronized method/block, your code will compile, but you will receive what type of runtime error? | IllegalMonitorStateException |
First Time Here?
Welcome to Quizlet, a fun, free place to study. Try these flashcards, find others to study, or make your own.