• 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/58

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;

58 Cards in this Set

  • Front
  • Back

Name 3 things in Java that offer strong support for concurrent programming?

Constructs of the language
Java class libraries
High-level concurrency API's since Java 5.0

Which are the two ways to use a Thread?

Instantiate a new instance of class Thread


Pass an application's task to an exector

How do I use a Thread that I instantiate myself?

Provide the application processing code inside a run() method


How do I stop a thread for 5 seconds?

Thread.sleep(5000) (note that this is the static class method)

How do I tell a thread to stop what it is currently doing?

By calling interrupt() on the thread instance that should be stopped, e.g calling t2.interupt

How do I wait on another thread to finish?

Use join() on the thread you want to wait *FOR*, e.g. t1 waits on t2, by calling t2.join()

Name two pitfalls that can threaded programming can introduce?

Thread interference
Memory consistency errors

What is an example of Thread Interference?

One thread increments a value on a class, and, another decrements, but the order is interleaved leaving a counter at a wrong value by the time the threads have finished

What is an example of a memory consistency error?

Two threads both increment a value, but both threads *read* the initial value - what *should* have happened is that the memory updated by thread a would be locked until thread A had finished with it, and thread B would read value that A had updated the area with.

What tools do you have to prevent these scenarios?

Synchronization

What entity does synchronising use?

An intrinsic lock (usually the instance of the class being 'threaded')

What is liveness?

The ability of a concurrent app to perform quickly (and correctly)



METAPHOR: how quickly can our team correctly communicate what needs to be done to eachother so we can work in parallel without messing things up.

What is deadlock?

Two or more threads waiting on eachother permanently to release a resource and so will never progress.

What is starvation?

One thread unable to make progress due to not being able to get access to a shared resource




METAPHOR: One colleague always talking so I can't get my teammate's help and he has the data I need to continue

What is Livelock?

Two threads too busy responding to *eachother* to actually do their work




METAPHOR: A little little like our context switching myth and being interrupted.

What is a guarded block?

A condition that will prevent a thread from proceeding until another thread has set a condition to be true

What are the 4 main high level concurrency objects?

Lock objects



Executors



Concurrent collections


Atomic Variables


What is the difference between a process and a thread?

Processes is a self-contained execution env, has its own memory space and set of run-time resources.




A thread is *within* a process, therefore every program has at least one, the *main* thread.

What is special about the main thread?

It is from this thread that additional threads are created.

What is a thread *object*?

An instance of java.lang.Thread

What are the two ways to create an instance of java.lang.Thread (as opposed to passing a Task to an executor)?

Provide a class that implements Runnable


Extend the class thread

What is a legitimate use of the sleep method?

When I know an operation on another thread will take time to complete, sleeping the current thread gives the other thread time to complete before I need the other value.

What is an example of where an InterruptedException would be thrown?

If another thread calls interrupt on a thread that is currently asleep

How can I ensure that the interrupt mechanism works correctly?

As a progammer, it is down to me to ensure that my threads *support* being interrupted - in other words, I need to consciously decide what to do, whether that be manually checking my own interrupt status or catching the (checked) InterruptedException

What 2 ways should my code be written so as to support interruption?

If I'm invoking methods that throw Interrupted Exception, all I need to do is to catch the exception and decide how to proceed
If the code I'm calling doesn't throw InterruptedException I need to 'manually check' for the interrupt flag being set by another thread

If I want to support my own interruption, what method would I call to check if an interrupt has been received?

I would call Thread.interrupted() , note that this is a *static* method

What is the interrupt status?

An internal flag that is set on a thread

How do the methods isInterrupted() and interrupted() differ?

isInterrupted() is called on an *instance* to check for the interrupt status of that instance, and won't clear the interrupt status




interrupted() is a static method that is called by a thread instance to see (including itself) if it has been interrupted, and will clear the interrupt status by virute of calling this method.

What is a key trick to using threads in JUnit when you want to assert?

Use join() to wait for all threads to complete before you try and assert the value of anything a thread may touch.

What happens when a thread enters a synchronized block of an object (and it is the first thread there)?

That thread gets the lock on the object until it has finished with the object

What happens when a thread enters a synchronized block of an object (and another thread is already there)?

The thread will block until the thread that currently holds the lock has exited

What happens after a thread has exited a syncronized block?

It establishes a 'happens-before' relationship with any other call to that method, so in other words, the changes that the method applied are visible to any thread that will call the synchronized method

What is the intrinsic lock for a static method?

The lock for the *Class* object associated with the object

How are synchronized statements different form synchronized methods?

The synchronization happens *WITHIN* a method body



You have to specify the object that you are locking on (usually the 'this' instance)

What is the use case for a synchronized statement over a synchronized method?

When an operation only needs to prevent concurrent access to *SOME* of an objects fields within a method block

Identify 3 things that are happening re synchronisation in the following code block:



public void addName(String name) {


synchronized(this) {


lastName = name;


nameCount++;


}


nameList.add(name);


}


- The instance of the class is the intrinsic lock


- No other threads can affect name count or last name until the method is existed by the current thread


- The list nameList remains available for concurrent access by other threads

What is happening in the following code block:




public void addName(String name) {


synchronized(this) {


lastName = name;


nameCount++;


}


nameList.add(name);


}



- The instance of the class is the intrinsic lock


- No other threads can affect name count or last name until the method is existed by the current thread


- The list nameList remains available for concurrent access by other threads

What is an example of an action that is *NOT* atomic?

An increment operation - i++, is broken down into multiple steps that can be interleaved

Which actions ARE atomic?

Read and writes of a reference variable


Reads and writes of a primitive (except double and long)

Why would a double and long write not be atomic?

Because they occupy more than word in memory - TODO confirm this as true

How is an atomic action defined?

An action that happens all at once - in effect, it cannot be broken down into sub-actions that may not all complete.

What problems does an atomic operation solve and which will it *NOT solve?

It can stop two threads interfering with the same value within a shared object




It *cannot* stop threads using an OLD value, and ignoring the most recent value applied to a shared object.

How should you write a guarded block statement to wait for a condition to be set?

Method is syncronized because wait needs to lock on the object




loop needs to wait for the specific condition to be true.




synchronized void method bePatient(){


while(someConditionFalse){

try{
wait();
}
catch (InterruptedException e){
}

}


}

What is the (less efficient) alternative to a guarded block?

A while loop that endless wastes cycles checking for the condition to be true






while (!someCondition){ //doing nothing here}

How does one thread tell another thread to re-check the condition?

Calls notifyAll(), and the wait method can release and check the condition once more

Why would I get an IllegalMonitorStateException

If I was trying to call wait() or notifyAll() without using a synchronized block (or hadn't acquired the intrinsic lock some other way)

Why might a method block forever?

If another thread wasn't able (or didn't) set the shared condition to be true and wait() had been invoked by a thread until that condition was true

What does volatile do?

It ensures that threads cannot cache a value - all threads will see the latest state of a variable when it's written if the variable is marked volatile

What is the "high level" Lock object provided by the java.concurrent packages?

A synchronised block "on steroids" - gives you locks acquiring with optional timeouts and interrupts

How does the improved synchronization work to guard against deadlock?

If an attempt to acquire a lock takes too long, the the thread can abort its attempt using the methods tryLock() and unlock() method instead of a synchronised method.

How else can a request to get a lock be aborted?




HINT: before the timeout period has been reached

If another thread sends an interrupt, and we've chosen to acquire a lock using the lockInterruptly method on the shared object

What is the relationship between Thread and Runnable?

Runnable defines the 'how', the task itself.


The thread is the actual running of the task.

How does normal Thread use differ from using Executors?

With creating our own threads we are bundling together the tasks and the creation/management of threads.




With executors, we are hiding the details of thread creation and management, and only need to define the tasks.

How does the Executor interface work, and how do it's implementations differ?

You pass a runnable to it and call execute. Depending on the implementation you might fire off a new thread, use a thread from a pool, put in on a queue...

How does the ExecutorService interface work?

You call submit instead of execute, and can pass Callable objects, which return Future objects

How does the ScheduledExecutor work?

As with the other executor Service but with methods to accept delays and intervals

What are thread pools and worker threads?

A thread pool is a collection of worker threads.




A worker thread is a special kind of thread that lives separately from the actual runnable - it can be passed a new runnable each time it is called, thereby executing different code every time

What is a fixed thread pool and what is it's advantage?

It's a thread pool that won't grow beyond a specified size




Its advantage is that it won't allow a system to be overwhelmed because it will block until threads in the pool are free - if you just specify as many threads as you have requests then you will cause the application to stop responding to *all* requests at some point.