Using Threads SWE 344 Internet Protocols & Client Server Programming.

Slides:



Advertisements
Similar presentations
How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Lecture 8 Restrictions on Resource Allocation Dining Philosophers - Havender's Linear Ordering.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Practice Session 7 Synchronization Liveness Guarded Methods Model
Chapter 14 Multithreading Yingcai Xiao. Multithreading is a mechanism for performing two or more tasks concurrently.  In the managed world of the common.
Threads in C# Threads in C#.
System Programming Practical Session 5 Liveness, Guarded Methods, and Thread Timing.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
28.2 Functionality Application Software Provides Applications supply the high-level services that user access, and determine how users perceive the capabilities.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Concurrency…leading up to writing a web crawler. Web crawlers.
Thread Examples. Runnable Interface Runnable defines only one abstract method; Public void run(); Thread also implements Runnable interface. Why does.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Threads CS Introduction to Operating Systems.
Fundamentals of Python: From First Programs Through Data Structures
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 19 Microsoft’s Approach 1 –.NET Mobile Framework part 2 Rob.
NAVY Research Group Department of Computer Science Faculty of Electrical Engineering and Computer Science VŠB-TUO 17. listopadu Ostrava-Poruba.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
1 Web Based Programming Section 8 James King 12 August 2003.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
1.NETConcurrent programmingNOEA / PQC 2007 Concurrent programming Threads –Introduction –Synchronization.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
Concurrent Programming and Threads Threads Blocking a User Interface.
Laboratory - 4.  Threading Concept  Threading in.NET  Multi-Threaded Socket  Example.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 18 Microsoft’s Approach 1 –.NET Mobile Framework part 2 Rob.
SurfaceView.
© Wang Bin 2004 Java Threads. © Wang Bin 2004 In this lesson, you will learn to: u Define the concepts of threads and multithreading  Identify the functions.
Multi-Threading in Java
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Chapter11 Concurrent. 集美大学 计算机工程学院 Java 程序设计 年 第二版 Concurrent ●Computer users take it for granted that their systems can do more than one thing.
1 OS Review Processes and Threads Chi Zhang
Lecture 20 Threads Richard Gesick. Threads Makes use of multiple processors/cores if available Share memory within the program (as opposed to processes.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Demonstrating Multithreading Concepts. Overvie w Multithreading or free-threading is the ability of an operating system to concurrently run programs that.
Asynchronous Programming Writing Concurrent Code in C# SoftUni Team Technical Trainers Software University
pThread synchronization
Lecture 6 Threads Erick Pranata
Multi Threading.
Multithreading.
Processes and Threads.
Threading using C# and .Net
Multithreaded Programming
Java Thread.
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Representation and Management of Data on the Internet
Foundations and Definitions
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Using Threads SWE 344 Internet Protocols & Client Server Programming

What is a thread? A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations then it is often helpful to set different execution paths or threads, with each thread performing a particular job. Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application. To make a program execute more than one task at a time, it could be divided into smaller threads. The life cycle of a thread starts when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution. 2

The Thread Class The C# language provides the System.Threading namespace, which includes classes for creating and controlling threads within a program. Use the Thread class to create a new Thread object, which produces a new thread within the current process. The format of the Thread constructor is as follows, where start is a ThreadStart delegate: Thread(ThreadStart start) The ThreadStart delegate points to the method that will be performed within the new thread. Here's an abbreviated example of creating a new thread:. Thread newThread = new Thread(new ThreadStart(newMethod));.. } void newMethod() {.. } 3

Thread Class Methods MethodDescription Abort()Terminates the thread Equals()Determines whether two Thread objects are the same GetHashCode()Gets a unique representation for the thread GetType()Gets the type of the current thread Interrupt()Interrupts a thread that is in the Wait thread state Join()Blocks the calling thread until the thread terminates Resume()Resumes a thread that has been suspended Start()Causes the operating system to change the thread state to Running Suspend()Suspends the execution of the thread ToString()Gets a string representation of the Thread object 4

The given example program creates two separate threads from the main program thread, using the Thread constructor (along with the ThreadStart constructor). The main program performs a for loop, displaying a counter value every second and using the Sleep() static method of the Thread class. The second thread does the same thing but uses a two-second delay; and the third thread uses a three-second delay. Again, remember that the threads start running only when the Start() method is used, not when the constructor finishes. using System; using System.Threading; class ThreadSample { public static void Main() { ThreadSample ts = new ThreadSample(); } public ThreadSample() { int i; Thread newCounter = new Thread(new ThreadStart(Counter)); Thread newCounter2 = new Thread(newThreadStart(Counter2)); newCounter.Start(); newCounter2.Start(); 5

for(i = 0; i < 10; i++) { Console.WriteLine("main: {0}", i); Thread.Sleep(1000); } void Counter() { int i; for (i = 0; i < 10; i++) { Console.WriteLine(" thread: {0}", i); Thread.Sleep(2000); } void Counter2() { int i; for (i = 0; i < 10; i++) { Console.WriteLine(" thread2: {0}", i); Thread.Sleep(3000); } 6

Discuss: -program that displays thread information for all the threads running on the system. Threading in – TCP Server program UDP Server program 7

END 8