Understanding Threads in Computing

Slide Note
Embed
Share

Exploring the concepts of parallel, distributed, and concurrent computing processes and how threads, cores, and CPUs work together. Delve into the terminology, implementation in Java and C#, synchronization, and the importance of leveraging multiple cores for enhanced performance.


Uploaded on May 14, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.



Presentation Transcript


  1. Module 7 - Part 1 Threads

  2. Overview Terminology Parallel, Distributed and Concurrent computing Processes Threads Cores Main ideas and questions Implementation in Java and C# Synchronization

  3. Terminology Parallel computing a task is broken down into subtasks, and solutions to subtasks are then combined Concurrent tasks may not be related, but done at the same time Distributed computing Large task is broken into subtasks (like parallel) Networked computers work on a subtasks Involves message passing between computers Central Processing Units (CPUs) used to have only one core One core can only run one program at at time To run multiple programs, the core must switch between them Now CPUs have multiple (independent) cores for processing

  4. Thoughtful Questions Questions: If we have more than one core on the CPU, can we leverage them? Can we really run things at the same time to speed things up? If we have only one core? Can we still split our program into parts?

  5. More Terminology Processes vs. Threads A Process: This is a complete program Has something called a Process Control Block (PCB) Must have one or more paths of execution called threads A Thread: Exists within a process Is a separate path of execution within a program Is lightweight meaning fast There s always at least one thread in a process (because of main) When you have only one core, but multiple threads: Core must switch between threads This is called time sharing

  6. This lecture only focuses only on threads. Next lecture is on multiprocessing.

  7. Why threads? Imagine you re writing a text messaging app. What would the code look like? friendsMesg = readFromNetwork(); PRINT (friendsMesg); myMesg = READ(); sendOverNetwork(myMesg); friendsMesg = readFromNetwork(); PRINT (friendsMesg); myMesg = READ(); sendOverNetwork(myMesg); WHY DOES THIS NOT WORK?

  8. Why threads? Problem: What if friend sends two messages? We need to have independent reading and writing So, we split the program into two parts: A thread to read messages from the network and post them A thread to read the keyboard and write it to the network

  9. Multithreading Multithreading - running many threads concurrently within a single program/process. main always has a thread, but can create additional threads Take advantage of multiple processors/cores if available

  10. Multi-threaded Program Fig: Block diagram of a Multi-threaded Program Main Method Module (Main Thread) start start start switching Thread A Thread B Thread C switching

  11. How to create a thread In Java, there are two ways: Create a class that extends the Thread class Implement the Runnable interface (much better) Either way, your class needs to have a run( ) method In general (many languages, like C#): Create a method the code the thread should run Create a thread pointing it to that method

  12. Example: A Day at the Races class Dog implements Runnable { static int dogCounter = 0; int ID; Dog() { ID = ++dogCounter; } public void run() { for (int i = 0; i < 1000; i++) { System.out.println ("Dog:"+ID+"\tat:"+i); } } }

  13. Example: A Day at the Races class Main { public static void main(String[] args) { Dog d1 = new Dog(); Dog d2 = new Dog(); Thread t1 = new Thread(d1); Thread t2 = new Thread(d2); t1.start(); // calls run t2.start(); // calls run } }

  14. Core is switching between threads

  15. Example: Racing 100 Dogs class Main { public static void main(String[] args) { Dog[] dogs = new Dog[100]; Thread[] threads = new Thread[100]; for (int i = 0; i < 100; i++) { dogs[i] = new Dog(); threads[i] = new Thread(dogs[i]); } for (int i = 0; i < 100; i++) { threads[i].start(); } } }

  16. Switching between Dogs

  17. Example: A Day at the Races (Note: method does *not* have to be called run) using System; using System.Threading; class Dog { static int dogCounter = 0; int ID; public Dog() { ID = ++dogCounter; } public void run() { for (int i = 0; i < 1000; i++) { Console.WriteLine("Dog:"+ID+"\tat:"+i); } } }

  18. Example: A Day at the Races class Example { public static void Main(String[] args) { Dog d1 = new Dog(); Dog d2 = new Dog(); // ThreadStarts represent the starting point // for the thread ThreadStart ts1 = new ThreadStart(d1.run); ThreadStart ts2 = new ThreadStart(d2.run); // Pass the ThreadStarts when creating Threads Thread t1 = new Thread(ts1); Thread t2 = new Thread(ts2); t1.Start(); t2.Start(); } }

  19. Thread Life Cycle Newborn State Runnable State Running State Blocked State Dead State Newborn New Thread Start Active Thread Stop Stop Dead Runnable Running yield Killed Thread Stop resume notify sleep, wait suspend Blocked Idle Thread (Not Runnable) Figure: A thread s Life Cycle

  20. Thread Control Methods Method Signature Description Retrieves the name of running thread in the current context in String format String getName() This method will start a new thread of execution by calling run() method of Thread/runnable object. void start() This method is the entry point of the thread. Execution of thread starts from this method. void run() This method suspend the thread for mentioned time duration in argument (sleeptime in ms) void sleep(int sleeptime) By invoking this method the current thread pause its execution temporarily and allow other threads to execute. void yield() This method used to queue up a thread in execution. Once called on thread, current thread will wait till calling thread completes its execution void join() This method will check if thread is alive or dead boolean isAlive() Source: https://www.w3resource.com/java-tutorial/java-threadclass-methods-and-threadstates.php

  21. Race Conditions Classic problem: You want to buy a plane ticket on Delta You want to reserve a specific seat Somebody else wants the same seat! Algorithm: bool isAvailable = ReadSeat( ) IF (isAvailable) THEN // Make it unavailable/invisible to others WriteSeat( ) END IF This is called a critical area and MUST be completed in its entirety (i.e. it can t be switched out)

  22. Synchronized Threads On a single-processor machine, threads share the processor. One thread can only execute for a certain time period. On a multiprocessor machine, each thread could have its own processor. On a single-processor machine, a thread's execution period might not last long enough for that thread to finish executing its critical code section before another thread begins executing its own critical code section. On a multiprocessor machine, threads can simultaneously execute code in their critical code sections. However, they might enter their critical code sections at different times.

  23. Synchronized Threads' execution orders are unpredictable. There is no guarantee that a thread can complete its critical code section before some other thread enters that section. Hence, we have a race condition, which causes inconsistencies. To prevent race conditions, each thread must complete its critical code section before another thread enters either the same critical code section or another related critical code section that manipulates the same shared variables or resources. Java's synchronized keyword in the context of the synchronized statement and synchronized methods. synchronized (ft)

  24. Summary Parallel computing is an important concept and there are several implementations of it Threading is a quick way to split your program into multiple independent parts Synchronizing between threads can be tricky In Java, either extend Thread or implement Runnable In C#, you need methods for the threads to run ThreadStarts that point to those methods

Related