In fact if you run the program multiple times you

Info icon This preview shows pages 10–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Interestingly, we cannot predict the order for these 40 lines of output. In fact, if you run the program multiple times, you will probably see the output appear in different orders on different runs. After all, each of the 21 separate threads running at the same time (conceptually, since your machine may not have 21 processors available for the program) can run in an unpredictable order. The main thread is the first thread and then it creates 20 others. The main thread always creates the other threads in the same order, but it is up to the Java implementation to let all the threads “take turns” using the available processors. There is no guarantee that threads created earlier run earlier. Therefore, multithreaded programs are often nondeterministic , meaning their output can change even if the input does not. This is a main reason that multithreaded pro- grams are more difficult to test and debug. The following figure shows two possible orders of execution, but there are many, many more. So is any possible ordering of the 40 output lines possible? No. Each thread still runs sequentially. So we will always see Thread 13 says hi before the line Thread 13 says bye even though there may be other lines in-between. We might also wonder if two lines of output would ever be mixed, something like: Thread 13 Thread says 14 says hi hi This is really a question of how the System.out.println method handles concurrency and the answer happens to be that it will always keep a line of output together, so this CPEN 221 – Fall 2016
Image of page 10

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Introduction to Concurrent and Parallel Programming 11 Figure 3: Two possible outputs from running the program that creates 20 threads that each print two lines. CPEN 221 – Fall 2016
Image of page 11
Introduction to Concurrent and Parallel Programming 12 would not occur. In general, concurrency introduces new questions about how code should and does behave. We can also see how the example worked around the rule that run must conform to the Runnable interface and therefore not take any arguments. The standard idiom is to pass any arguments for the new thread to the constructor on the class that implements Runnable , which then stores them in fields so that run method can later access them. In this simple example, this trick is the only use of shared memory since the helper threads (the ones doing the printing) do not need to pass any other data to/from the main thread or each other. It may not look like this is using shared memory, but it is: When the main thread calls new ExampleMethod(i) , this is a normal call to a constructor. A new object gets created and the main thread runs the constructor code, writing to the i field of the new object. Later, after the call to start , the new running thread is running the run method with this bound to the same object that the main thread wrote to. The helper thread then reads i , which just means this.i , and gets the value previously written by the main thread: shared-memory communication.
Image of page 12

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern