threads n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Threads PowerPoint Presentation
Download Presentation
Threads

Loading in 2 Seconds...

  share
play fullscreen
1 / 10
isaura

Threads - PowerPoint PPT Presentation

134 Views
Download Presentation
Threads
An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Threads CSC220

  2. What is a thread? • A thread (also known as a thread of execution) is a way to make a single program perform multiple tasks concurrently • Various tasks within a single program can be made to run in parallel CSC220

  3. When are threads used? • Threads are typically used when a program consists of multiple tasks of varying priorities • It is typical to have a UI run in a high priority thread so that the user doesn’t sit around pushing mouse buttons and wondering what is going on • Lengthy computations are typically placed in low priority threads CSC220

  4. Will multi-threaded programs run faster? • It depends on the underlying computer architecture and operating system • A multi-threaded application probably won’t run faster on a single core processor • Some tasks (like I/O) may be pushed off to external processors (video cards, etc.) • A multi-threaded application might run faster on a dual core processor if the operating system is designed to utilize both cores on a single program CSC220

  5. Is it easy to write a multi-threaded application? • It depends on the supported provided by the programming language • Java provides the Thread API making it easy to write threads • Ada has multi-threading primitives built into the language • C/C++ are dependent on external libraries to provide multi-threading functionality CSC220

  6. Is it easy to get a multi-threaded application working properly? • No! • It’s especially difficult when threads must • Share memory or other resources • Communicate with one another CSC220

  7. Thread lifecycle Created Key: Italics are external events Block are Java Thread API calls start Ready scheduled by OS yield notify notifyAll interrupt interrupt resource available Running wait waiting on resource sleep task complete Waiting Blocked interrupt sleep expiration Asleep CSC220

  8. Inherit from java.lang.Thread The run method holds the code Thread.stop() has been deprecated, do something like this instead. Don’t let one thread monopolize the CPU The specifics (Java API) public class ThreadClass extends Thread { private boolean go; public ThreadClass () { go = true; } public void Stop () { go = false; } public void run () { while (go) { // -- do something here try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } } CSC220

  9. Construct thread objects Start threads Stop threads Managing a thread public class TestThread { public static void main(String[] args) { ThreadClass thread1 = new ThreadClass(1); ThreadClass thread2 = new ThreadClass(2); ThreadClass thread3 = new ThreadClass(3); thread1.start(); thread2.start(); thread3.start(); // -- Do some other stuff in here // -- sets the loop control variable // (Thread.stop was declared unsafe) thread1.Stop(); thread2.Stop(); thread3.Stop(); } } CSC220

  10. Sharing memory amongst threads • Exercise CSC220