Trail: Essential Classes
Lesson: Concurrency
Questions and Exercises
Home Page > Essential Classes > Concurrency

Questions and Exercises: Concurrency


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


  1. Compile and run
    public class BadThreads {
        static String message;
        private static class CorrectorThread
            extends Thread {
            public void run() {
                try {
                } 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.";
            // Key statement 2:

    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? How would you guarantee that all changes to message will be visible in the main thread?

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

Check your answers.

Problems with the examples? Try Compiling and Running the Examples: FAQs.
Complaints? Compliments? Suggestions? Give us your feedback.

Previous page: For Further Reading
Next page: The Platform Environment - all specs in one place