All java programs comprise at least a single thread

This preview shows page 13 - 17 out of 24 pages.

All Java programs comprise at least a single thread of control. " E.g., the main() method runs as a single thread in the JVM. ! To create Java threads, " First define a class that implements the Runnable interface. # The interface has a function run() that the class must implement. class Summation implements Runnable { private int upper; private Sum sumValue; public Summation(int upper, Sum sumValue) { this.upper = upper; this.sumValue = sumValue; } public void run() { int sum = 0; for (int i = 0; i <= upper; i++) sum += i; sumValue.set(sum); } } class Sum { private int sum; public int get() { return sum; } public void set(int sum) { this.sum = sum; } } Threads
Image of page 13

Subscribe to view the full document.

Threads 14 27 Thread Libraries – (cont’d) " Then , create an object instance of the Thread class and passing the constructor a Runnable object. " Creating a Thread object does not create the new thread; it is the start() method that actually creates the new thread. " The join() method in Java provides similar functionality to the pthread_join() and WaitForSingleObject() . public class Driver { public static void main(String[] args) { ... // do some basic error checking Sum sumObject = new Sum(); int upper = Integer.parseInt(args[0]); Thread worker = new Thread(new Summation(upper, sumObject)) ; worker.start(); worker.join(); System.out.println("The sum of " + upper + " is " + sumObject.get()); } } Threads 28 Thread Libraries – (cont’d) ! Calling the start() method does two things: " It allocates memory and initializes a new (child) thread in the JVM. " The child thread begins execution in the run() method. Threads
Image of page 14
Threads 15 29 Threading Issues –fork()/exec() ! If one thread in a program call fork() " Does the new process duplicate all threads? " Or is the new process single-threaded? ! Some UNIX systems have chosen to have two versions of fork() , one that duplicates all threads and another that duplicates only the thread that invoked the fork() system call. ! If a thread invokes the exec() system call, the program specified in the parameter to exec() will replace the entire process – including all threads. ! For systems with two versions of fork() : " If exec() is called immediately after forking, duplicating only the calling thread is appropriate. " If the separate process does not call exec() after forking, the separate process should duplicate all threads. Threads 30 Threading Issues – Cancellation ! Thread cancellation is the task of terminating a thread before it has completed. ! Examples: " Multithreaded database search: # When one thread returns the result, the remaining threads might be canceled. " A user presses a stop button on a web browser: # A web page is often loaded using several threads. # When stop, all threads loading the page are canceled. ! A thread that is to be canceled is often referred to as the target thread . Threads
Image of page 15

Subscribe to view the full document.

Threads 16 31 Threading Issues – Cancellation (cont’d) ! Two general approaches: " Asynchronous cancellation terminates the target thread immediately .
Image of page 16
Image of page 17
You've reached the end of this preview.
  • Spring '12
  • GwangS.Jung
  • System Call, Native POSIX Thread Library, thread libraries

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