Download the JDK
Search the Tutorials
Hide the TOC
Trail: Essential Classes
Lesson: Concurrency
Concurrency
Processes and Threads
Thread Objects
Defining and Starting a Thread
Pausing Execution with Sleep
Interrupts
Joins
The SimpleThreads Example
Synchronization
Thread Interference
Memory Consistency Errors
Synchronized Methods
Intrinsic Locks and Synchronization
Atomic Access
Liveness
Deadlock
Starvation and Livelock
Guarded Blocks
Immutable Objects
A Synchronized Class Example
A Strategy for Defining Immutable Objects
High Level Concurrency Objects
Lock Objects
Executors
Executor Interfaces
Thread Pools
Concurrent Collections
Atomic Variables
For Further Reading
Questions and Exercises
Home Page > Essential Classes > Concurrency
Questions and Exercises: Concurrency

> ### Questions

> 1. Can you pass a `Thread` object to `Executor.execute`? Would such an invocation make sense?

> ### Exercises

> 1. Compile and run ``BadThreads.java``:

>    >     public class BadThreads {

>    >         static String message;

>    >         private static class CorrectorThread extends Thread {

>    >             public void run() {
>    >                 try {
>    >                     sleep(1000);
>    >                 } catch (InterruptedException e) {}
>    >                 //Key statement 1:
>    >                 message = "Mares do eat oats.";
>    >             }
>    >         }

>    >         public static void main(String args[]) throws InterruptedException {
>    >             (new CorrectorThread()).start();
>    >             message = "Mares do not eat oats.";
>    >             Thread.sleep(2000);
>    >             //Key statement 2:
>    >             System.out.println(message);
>    >         }
>    >     }

>    The application should print out "Mares do eat oats." Is it guaranteed to always do this? If not, why not? Would it help to change the parameters of the two invocations of `Sleep`? Describe two ways to change the program to enforce such a guarantee.

> 2. Modify the producer-consumer example in Guarded Blocks to use a standard library class instead of the `Drop` class.

> Check your answers.

Previous page: For Further Reading
Next page: The Platform Environment