The way we will do it is to have the helper threads

Info icon This preview shows pages 7–11. Sign up to view the full content.

answers back to the “main” thread. The way we will do it is to have the helper threads write to some object fields that the main thread later reads. CPEN 221 – Fall 2016
Image of page 7

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

Introduction to Concurrent and Parallel Programming 8 Figure 2: The key pieces of an executing multithreaded program: Each thread has its own program counter and call-stack, but objects in the heap may be shared by multiple threads. (Static fields of classes are also shared by all threads.) CPEN 221 – Fall 2016
Image of page 8
Introduction to Concurrent and Parallel Programming 9 We finish this note with some Java specifics for exactly how to create a new thread in Java. The details vary in different languages and in fact the parallelism portion of these notes mostly uses a different Java library with slightly different specifics. In addition to creating threads, we will need other language constructs for coordinating them. For example, for one thread to read the result another thread wrote as its answer, the reader often needs to know the writer is done. We will present such primitives as we need them. To create a new thread in Java requires that you define a new class (step 1) and then perform three actions at run-time (steps 2–4): 1. Define a class that implements the Java interface Runnable . This class should have a public method run , which takes no arguments and has return type void . The run method will act like main for threads created using this class. It must take no arguments, but the example below shows how to work around this incon- venience. 2. Create an instance of the class you defined in step 1. That is, if you defined class ExampleMethod , then new to create an ExampleMethod object. Note this does not yet create a running thread. It just creates an object of class ExampleMethod , which is of type Runnable . 3. Create an instance of a Thread class in Java using the ExampleMethod object created in step 2 as an argument to the constructor. 4. Call the start method of the thread object you created in step 3. This step does the work of creating of a new thread. That new thread will execute the run method of the object. Notice that you do not call run ; that would just be an ordinary method call. You call start , which makes a new thread that runs run . The call to start returns immediately so the caller continues on, in parallel with the newly-created thread running run . The new thread terminates when its run method completes. Here is a complete example of a useless Java program that starts with one thread and then creates 20 more threads: class ExampleMethod implements Runnable { int i; ExampleMethod ( int i) { this . i = i; } public void run () { System. out . println ( ”Thread ” + i + ” says hi” ); System. out . println ( ”Thread ” + i + ” says bye” ); } CPEN 221 – Fall 2016
Image of page 9

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

Introduction to Concurrent and Parallel Programming 10 } class Main { public static void main (String[] args) { for ( int i= 1 ; i <= 20 ; ++i) { ExampleMethod em = new ExampleMethod (i); Thread t = new Thread(em); t. start (); } } } When this program runs, it will print 40 lines of output, one of which is: Thread 13 says hi Interestingly, we cannot predict the order for these 40 lines of output. In fact, if you
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • satish
  • Central processing unit, shared memory, Parallel algorithm

{[ 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