100_lecture34

100_lecture34 - Introduction to Computation and Problem...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computation and Problem Solving Prof. Steven R. Lerman and Dr. V. Judson Harward Class 34: Class 34: Introduction to Threads Introduction to Threads 2 What is a Thread? Imagine a Java program that is reading large files over the Internet from several different servers. Some of these servers might be under heavy load or on slow connections to the Internet. Others might return the data quickly. Most of the time in our program will be spent waiting for input over the network. One programming approach is straightforward: read file 1 from server A read file 2 from server B read file 3 from server C . . . . 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
3 What is a Thread?, 2 Doing these reads sequentially is inefficient since until all of file 1 is loaded. A much faster approach is to start reading from assigned to a separate independent processor). the loading of file 2 from server B wouldn't start each file concurrently, and handle the partial files as they arrive. This requires the ability to have several tasks proceeding in parallel (as though they were each 4 What is a Thread?, 3 sources. and the branches proceed in parallel. small time increments. threads . Most computers still only have a single processor, so what we really want is an easy way for the program to switch among arriving data More generally, we would like to be able to write programs where the "flow of control" branches, The processor can achieve this by switching between the different branches of the program in This is the strategy behind 2
Background image of page 2
5 Threads vs. Processes processes to proceed in parallel. complex and often expensive to communicate Threads each other. Most operating systems allow concurrent Processes are expensive but safe. Processes are so well insulated from each other that it is both between them. are cheap, but different threads running in the same process are not well-insulated from 6 Java Support for Threads language. languages such as C and C++, there are libraries standardized. Java is one of the few programming languages where the support for threads is a part of the Ada, a language developed by the Department of Defense, also has built-in support for threads, but Ada is little used outside DoD contexts. C# provides support similar to Java. In other to implement threads which are more or less 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
7 Java is Inherently Multithreaded In Java, the garbage collection of unreferenced objects is performed by the Java runtime system in a separate thread. Java also uses a separate thread to deliver user interface events. This allows a program to remain responsive even while it is involved in a long running calculation or I/O operation. Think how you would implement a "Cancel" function if you
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

100_lecture34 - Introduction to Computation and Problem...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online