• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/90

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

90 Cards in this Set

  • Front
  • Back
What are the two different things that a "thread" in Java means?
1. An instance of class java.lang.Thread
2. A thread of execution
What is an "instance" of Thread?
It is just an object. It has variables and methods, and lives and dies on the heap.
What is a thread of execution?
An individual process (a "lightweight" process) that has its own call stack. In Java, there is one thread per call stack and in reverse, one call stack per thread.
What schedules the threads in Java?
The JVM operates like a mini-OS and schedules its own threads regardless of the underlying operating system.
List the 4 methods in the Thread class that you need to know for the exam?
start()
yield()
sleep()
run()
How do you start a thread of execution, a new call stack?
By invoking "run()"
What are the two ways you can define and instantiate a thread?
1. Extend the java.lang.Thread class
2. Implement the Runnable interface
Why isn't extending the Thread class not a good OO practice?
Because subclassing should be reserved for classes that extend an existing class. So you should only do this when you have a more specialized version of a Thread class (i.e you have more specialized thread-specific behavior).
In the real world, which way would you most likely define and instantiate a thread?
By implementing the Runnable interface
If you extend java.lang.Thread how would you define code to run in a separate thread?
1. Extend the Thread class
2. Override the "run()" method
What is the limitation of extending the Thread class?
You can't extend anything else.
In extending the Thread class can you overload the "run()" method and if you do when will it be called?
Yes you can overload it but it won't run unless you call it - it will not be used as the basis of a new call stack.
Every thread of execution begins as an instance of class _____.
Thread
How do you instantiate a Thread if you just extended the Thread class?
MyThread t = new MyThread();
How do you instantiate a Thread if you implement Runnable?
1. First you instantiate your Runnable class:
My Runnable r = new MyRunnable();
2. Next, you get yourself an instance of java.lang.Thread to run your job and give it your job:
Thread t = new Thread(r);
When do you use the no-arg constuctor for Thread and when do you call the one-arg constructor for the Thread?
1. no-arg when you extend Thread - then the thread will run its own "run()" method.
2. one-arg (you pass in your Runnable) when you implement Runnable - then the new thread will run your "run()" method.
Can you pass a single Runnable instance to multiple Threads?
Yes - this means several threads of execution will be running the very same job.
Give the 7 constructors we care about for the Thread class.
1. Thread()
2. Thread(Runnable target)
3. Thread(Runnable target, String name)
4. Thread(String name)
5. Thread(ThreadGroup group, Runnable target)
6. Thread(ThreadGroup group, Runnable target, String name)
7. Thread(ThreadGroup group, String name)
What state is a thread in when it has been instantiated but not started and is it alive?
The "new" state. No, it is not yet alive.
How can you test the "aliveness" of a thread - that it has been started but has not yet completed its "run()" method?
By calling the "isAlive()" method on the Thread instance.
How do you start a new Thread?
You call start "start()" on a Thread instance:
t.start();
What 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
What do you start, a Thread or a Runnable?
a Thread
What call stack does a "run()" method go on if it is called directly?
It goes onto the current call stack rather than at the beginning of a new call stack - this is legal although it does not start a new thread of execution.
Since the Runnable instance doesn't have a reference to the Thread instance, how do we get the thread name?
First invoke the static "Thread.currentThread()" method, which returns a reference to the currently executing thread, and then invoke "getName()" on that returned refernce:
Thread.currentThread().getName()
Does a thread have a name if you don't explicitly give it a name?
Yes, it still has a name
If you don't give a thread a name what is the form of the name and what is the name of the main thread?
The name of the main thread is "main". The form of a thread name is like "Thread-0".
What is the only thing that is guaranteed with Java threads?
Each thread will start, and each thread will run to completion. How this happens is not just JVM dependent but also runtime dependent. Order of threads running, a thread executing until it's done and a loop completing before another thread begins is not guaranteed.
What is in charge of when threads are run?
the scheduler
When is a thread done being a thread?
When its target "run()" method completes.
What happens when a thread completes its "run()" method?
The thread ceases to be a thread of execution, the stack for that thread dissolves, and the thread is considered dead.
Once a thread is dead, is it gone?
No, it's still a Thread object, just not a thread of execution.
What method can't you call on a dead Thread instance?
its "start()" method
What does the thread scheduler do?
It is part of the JVM and decides which thread should run at any given moment, and also takes threads out of the run state.
What does it mean for a thread to be eligible to run?
It is in the runnable state
In a single processor mahcine, how many threads can run at a time?
one
Is the order in which runnable threads are chosen to run guaranteed?
No
Give the signatures of 4 methods from the java.lang.Thread class that influence thread scheduling.
public static void sleep(long millis) throws Interrupted Exception
public static void yield()
public final void join()
public final void setPriority(int newPriority)

Note: sleep() and join() have overloaded versions not shown above
Give the signatures of 3 methods from the java.lang.Object class that influence thread behavior.
public final void wait()
public final void notify()
public final void notifyAll()

Note: the wait() method has 3 overloaded version including the one above
List the 5 states that a thread can be in.
new, runnable, running, waiting/blocked/sleeping, dead
What is the "new" state of a thread?
The state the thread is in after the Thread instance has been instantiated but the "start()" method has not been invoked on the thread. It is a live Thread object but not yet a thread of execution. It is "not alive".
What is the "runnable" state of a thread?
The state of a thread when it is eligible to run, but the scheduler has not selected it to be the running thread. A thread enters this state when the "start()" method is invoked, but it also returns to this state after either running or coming back from a blocked, waiting, or sleeping state. It is "alive".
What is the "running" state of a thread?
This is the state when the scheduler selects it (from a runnable pool) to be the currently executing process.
What is the one thing that "waiting/blocked/sleeping" have in common?
The thread is still alive, but is currently not eligible to run, although it might return to a runnable state later if a particular event occurs.
Can one thread tell another thread to block?
No
What is the "dead" state of a thread?
A thread is considered dead when its "run()" method completes.
What happens when you invoke "start()" on a dead thread?
You'll get a runtime(not compiler) exception because a dead thread can never be brought back to life.
What method would you use to "slow a thread down"?
the static Thread.sleep() method.
What exception can the "sleep()" method throw?
a checked InterruptedException
What does the time specified in "sleep()" mean?
It is the minimum duration in which the thread won't run. There is not a guarantee that the thread will start running again as soon as the time expires and the thread wakes.
Why can't "sleep()" be used for one thread to put another thread to sleep?
Because it is a static method - so it can only put the currently running thread to sleep.
Threads usually run with some ____ usually as a number between 1 and __.
priority
10
What type of scheduling is used in most JVMs?
preemptive, priority-based scheduling
Do all JVMs use time slicing?
No
In most JVMs, how does the scheduler use thread priorities?
The running thread will be of equal or greater priority than the hightest priority threads in the pool.
How should you use thread priorities in your program?
As a way to improve the efficiency but don't depend on thread-scheduling priority for correctness.
What is the default priority of a thread?
the priority of the thread of execution that created it
How can you yourself set a thread's priority?
By calling "setPriority()" on a Thread instance.
Give the three constants (static final variables) in the Thread class that define the range of thread priorities.
Thread.MIN_PRIORITY (1)
Thread.NORM_PRIORITY (5)
Thread.MAX_PRIORITY (10)
What does "yield()" do?
It might - and often does - make a running thread give up its slot to another runnable thread of the same priority, there's no guarantee - the yielding thread might just be chosen again over all the others.
What does a call to "join()" do?
Guaranteed to cause the current thread to stop executing until the thread it joins with (in other words, the thread it calls "join()" on) completes. If the thread it's trying to join with is not alive, however, the current thread won't need to back out.
Besides the calls to "sleep()", "yield()", and "join", give three other ways in which a thread might leave the running state?
1. The thread's "run()" method completes
2. A call to "wait()" on an object
3. A thread can't acquire the lock on the object whose method code it's attempting to run.
What is an "atomic" operation?
The operation, regardless of the number of actual statements (or underlying byte code instructions), is completed before any other thread code that acts on the same data.
What can you guarantee for an "atomic" operation?
Even if the thread running the atomic operation moves in and out of the runnable state, no other running thread will be able to act on the same data.
What are the two things you must do to protect data?
1. Mark the variables "private"
2. Synchronize the code that modifies the variables
How does synchronization work?
With locks. Every object in Java has a built-in lock that only comes into play when the object has synchronized method code. Since there is only one lock per object, if one thread has picked up the lock, no other thread can enter the synchronized code until the lock is released.
Only _____ can be synchronized, not _____.
methods
variables
How many locks does an object have?
just one
Can a class have both synchronized and nonsynchronized methods?
Yes
If two methods are synchronized in a class, can one thread be accessing one of the methods and another by accessing the other method?
No
If a class has both synchronized and nonsynchronized methods, can multiple threads still access the nonsynchronized methods?
Yes
What happens with a thread's locks when it goes to sleep?
It takes them with it.
Can a thread acquire more than one lock? Give examples
Yes
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.
Can a thread acquire a lock and then attempt to call a synchronized method on that same object?
Yes - the JVM know that this thread already has the lock for this object so is free to call other synchronized methods on the same object using the lock the thread already has.
Can you just synchronize a block of code rather than a method?
Yes, we call this a "synchronized block"
When you synchronize a block of code, what lock do you use?
You specify which object's lock you want to use as the lock. This gives you the ability to have more than one lock for code synchronization within a single object.
Can static methods be synchronized and if so what lock is used?
Yes, there is only one copy of the static data you're trying to protect, so you only need one lock per class to synchronize static methods - a lock for the whole class and every class loaded in Java has a corresponding instance of java.lang.Class representing that class and it's this instance whose lock is used to procted the static methods of the class, if they're synchronized.
If a thread tries to enter a synchronized method and the lock is already taken, what happens?
The thread is said to be blocked on an object's lock. The thread goes into a kind of pool for that particular object ahd has to sit there until the lock is released and the thread can again become runnable/running.
How does a deadlock happen?
It occurs when two threads are blocked, with each waiting for the other's lock. Neither can run until it gives up the lock, so they'll sit forever and ever and ever ...
What is one design strategy that can be used to help avoid deadlock?
strategies for always acquiring locks in a predetermined order.
What 3 methods does java.lang.Object have that help threads communicate about the status of an event that the threads care about?
wait(), notify(), and notifyAll()
What kind of context must "wait()
, "notify()", and "notifyall()" be called from?
a synchronized context. A thread can't invoke a wait or notify method on an object unless it owns that object's lock.
What happen when a thread executes "wait()"?
Every object can have a list of threads that are waiting for a signal (notification) from the object. A thread gets on this waiting list by executing the "wait()" method of the target object. From that moment, it doesn't execute any further instructions until the "notify()" method of the target object is called.
What happens to a lock when a thread waits?
It temporarily releases the lock for other threads to use, but it will need it again to continue execution.
What happens when a thread calls "wait()" does not own the lock?
It will throw an IllegalMonitorStateException which is not a checked exception so you don't have to catch it explicitly.
What is the second form of "wait()"?
It accepts a number of milliseconds as a maximum time to wait. If the thread is not interrupted, it will continue normally whenever it is notified or the specified timeout has elapsed.
What happens with the lock when "notify()" is called?
It doesn't mean the thread gives 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.
What happens when you call "notifyAll()"?
All of the threads that are waiting 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.
What method is defined in interface Runnable?
run()
Which 2 methods in the Thread class are static and which are non-static?
sleep() and yield() are static
start() and join are non-static