Concurrency QuizS2C Home « Concurrency Quiz

The questions in this Java quiz are on the topics covered in the Java - Concurrency section of the site. The table below lists the lessons, a description of the lesson content and the the quiz question number range.

Lesson Summary

Click on a lesson in the table to go to that lesson for a refresher on the topics for that lesson.

API Contents Lessons Description Question Range
Lesson 1 - Thread BasicsIn our first lesson of this section we look at multithreading and how to run parts of a program concurrently by subclassing the Thread class.1 - 6
Lesson 2 - The Runnable InterfaceThe second way of running parts of a program concurrently is by declaring a class that implements the Runnable interface and is the subject of this lesson.7 - 12
Lesson 3 - SynchronizationIn this lesson we look at synchronization and how we can control access to our code at the method or block level.13 - 17
Lesson 4 - Thread PrioritiesFor this lesson we investigate thread priorities and how we can have the priority set automatically by the invoking thread or assign thread priorities ourselves using methods of the Thread class.18 - 22
Lesson 5 - Thread CommunicationIn our final lesson of the section we explore how threads can communicate with each other when they are within a synchronized method or code block.23 - 28

Java Quiz

The quiz below tests your knowledge of the material learnt in the Java - Concurrency section of the site.

Question 1 : When we start up the JVM what kind of thread is created?
- When we start up the <em>JVM</em> a <em>non-daemon thread</em> also known as a <em>user thread</em> is created.
Question 2 : thread based multitasking allows us to run different programs concurrently?
- <em>thread based</em> multitasking allows us to run different parts of the same program concurrently. Running different programs at the same time is known as <em>process based</em> multitasking.
Question 3 : What should subclasses of the Thread class do with that classes run() method?
- Subclasses of the <code>Thread</code> class should <em>override</em> the <code>run()</code> method to do bespoke processing.
Question 4 : When creating multiple threads in a program, they execute in the same order as they were started in, using the start() method of the Thread class?
- We have no control over the actual order that threads will execute in. The <code>start()</code> method just readies a <em>thread</em> for execution and makes no difference to when it runs.
Question 5 : The interrupt() method of the Thread class will not stop a thread executing?
- The <code>interrupt()</code> method doesn't actually stop a thread from running, but sets a flag in the <em>thread</em> that indicates that an <em>interrupt</em> has been requested. The flag must be checked in the <code>run()</code> method for its effect to have any impact.
Question 6 : The sleep() method of the Thread class will stop a thread executing?
- The <code>sleep()</code> method moves a <em>thread</em> to a <em>blocked</em> state for the amount of time specified in its parameters. The <em>thread</em> will then pass back into a <em>runnable</em> state and be available to the <em>thread scheduler</em> again.
Question 7 : Which method of the Runnable interface do we use to turn Thread instances into threads of execution?
- We use the <code>start()</code> method of the <code>Thread</code> class to turn a <code>Thread</code> instance into a <em>thread of execution</em>.
Question 8 : What is an advantage of using the Runnable interface?
- Using the <code>Runnable</code> interface rather than <em>subclassing</em> the <code>Thread</code> class, leaves us the option of <em>extending</em> another class.
Question 9 : What should we do with the run() method of the Runnable interface?
- Classes that implement the <code>Runnable</code> interface should <em>override</em> the <code>run()</code> method to do bespoke processing.
Question 10 : To delay a thread until another thread has died we use the sleep() method of the Thread class?
- The <code>sleep()</code> method stops a <em>thread</em> executing for the duration specified, which may or may not be till another <em>thread</em> dies. To delay a <em>thread</em> until another thread has died we use the <code>join()</code> method.
Question 11 : How can we find out which thread is active in the run() method at any given time?
- We can use the static <code>currentThread()</code> method to get a reference to the currently executing <em>thread</em>, which can then be used to get information such as the <em>thread</em> name.
Question 12 : How does a thread know which run() method to call?
- If the <em>thread</em> was constructed using a separate <code>Runnable</code> object, then that <code>Runnable</code> object's <code>run()</code> method is called; otherwise, the <code>run()</code> method from the <code>Thread</code> class is executed which does does nothing and returns, unless <em>overridden</em> by a <em>sublass</em> of the <code>Thread</code> class.
Question 13 : Only one thread can enter a synchronized method at a time?
- Many threads can enter the same synchronized method concurrently if the threads are associated with different objects.
Question 14 : Threads for the same object can enter different synchronized methods of a class concurrently?
- When a thread enters a synchronized non-static method the object gets locked. This means that all synchronized non-static methods are now locked out for this object until the method ends and releases the lock or other synchronized methods are called from this synchronized method as this thread already has the lock.
Question 15 : We can run a synchronized static method and a synchronized non-static method at the same time?
- Yes this is possible. Synchronized static methods have a class lock and synchronized non-static methods have an object lock that are totally independent and therefore these methods can run concurrently.
Question 16 : We must make local variables thread safe through synchronization?
- Every thread gets its own set of <em>local variables</em> which work independently of each other, so are inherently thread safe.
Question 17 : When a lock is released from an object it dies and this is known as a deadlock?
- A <em>deadlock</em> occurs when the object locks of two or more objects are locking each other out forever.
Question 18 :Setting a threads priority to 0 effectively stops the thread from running?
- Whatever valid priority (range 1-10) you set a <em>thread</em> to, has no effect on blocking the <em>thread</em>.
Question 19 : We can set a thread's priority to a number within which range?
- A thread can be set in the range <code>1-10</code>.
Question 20 : A new thread will have the default priority of 5 if not given a priority?
- New <em>threads</em> are given the <em>threads</em> priority of their parent <em>thread</em> which can be any value in the range <code>1-10</code>.
Question 21 : We can use the yield() method to block a thread to give other threads a chance to run?
- The <code>yield()</code> method sets a <em>thread</em> back to a <em>runnable</em> state not a <em>blocked</em> state.
Question 22 : A yielded thread may go straight back to executing after yielding?
- A <em>yielded thread</em> goes back to a <em>runnable</em> state and so can indeed be the next <em>thread</em> picked by the scheduler to execute.
Question 23 : Which of the following methods releases an object's lock?
- The <code>wait()</code> method releases an object's lock.
Question 24 : The notify() method returns all waiting threads for an object back to a runnable state?
- The <code>notify()</code> method will alert and return a single thread for an object back to a <em>runnable</em> state. The <code>notifyAll()</code> method will alert and return all threads for an object back to a <em>runnable</em> state.
Question 25 : We can only use notify(), notifyAll() and wait() in synchronized methods?
- We can use the <code>notify()</code>, <code>notifyAll()</code> and <code>wait()</code> methods in synchronized methods and synchronized blocks of code.
Question 26 : When using the wait() and notify() methods the object type makes no difference?
- The <code>Thread</code> class uses <code>wait()</code> and <code>notifyAll()</code> on threads to internally implement <code>join()</code>. So an instance of this class will do a <code>notifyAll()</code> when it dies unlike other object types.
Question 27 : The notify() and notifyAll() methods will only awaken threads that are currently waiting on an object's lock?
- This is true as any <em>threads</em> that enter a <em>wait</em> state after the call to <code>notify()</code> or <code>notifyAll()</code> will be in a <em>wait</em> state until another call to <code>notify()</code> or <code>notifyAll()</code> has been made or indefinitely.
Question 28 : Why do we need to put calls to wait() inside a loop statement?
- We put calls to <code>wait()</code> inside a loop statement to avoid complication with spuriously woken or hanging threads.
Quiz Progress Bar Please select an answer

What's Next?

The next quiz is all about generics/collections.