NETWORK PROGRAMMING CNET 441

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Multithreading The objectives of this chapter are:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Software Engineering Oct-01 #11: All About Threads Phil Gross.
Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fundamentals of Python: From First Programs Through Data Structures
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Multithreading.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Java Programming: Advanced Topics
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading in JAVA
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Multi-Threading in Java
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
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.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
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.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Java Thread Programming
Tutorial 2: Homework 1 and Project 1
Multithreading The objectives of this chapter are:
CSCD 330 Network Programming
Multithreading / Concurrency
Threaded Programming in Python
Multi Threading.
Multithreading.
Multithreaded Programming in Java
Lecture 21 Concurrency Introduction
23 Multithreading.
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading Chapter 23.
© 2002, Mike Murach & Associates, Inc.
Multithreading.
Multithreading.
Multithreaded Programming
Threaded Programming in Python
21 Threads.
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Threads and Multithreading
Lecture 19 Threads CSE /6/2019.
“The Little Book on Semaphores” Allen B. Downey
Multithreading The objectives of this chapter are:
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Synchronization and liveness
Presentation transcript:

NETWORK PROGRAMMING CNET 441 CHAPTER - 4 Multithreading

Chapter 4: Objectives After Completing the Chapter 4, the student can understand the following concepts. Thread Basics Using Threads in Java Extending the Thread Class Sample Program Implementing the Runnable Interface Sample Program  Multithreaded Servers Locks and Deadlock Synchronising Threads

Thread Basics A thread is a flow of control through a program. A thread does not have a separate allocation of memory, but shares memory with other threads created by the same application. Threads created by an application can share global variables. Threads can have its own local variables.

Pre-emptive and Cooperative Scheduler A pre-emptive scheduler will determine when a thread has had its fair share of CPU time (possibly via simple time allocation) and will then pause it (temporarily). A cooperative scheduler, on the other hand, will wait for the running thread to pause itself before giving control of the CPU to another thread.

Methods for Creating Thread Class In Java, an object can be run as a thread if it implements the inbuilt interface Runnable, which has just one method: run. Thus, in order to implement the interface, we simply have to provide a definition for method run. Since the inbuilt class Thread implements this interface. There are two fundamental methods for creating a thread class. create a class that extends Thread. create a class that does not extend Thread and specify explicitly that it implements Runnable.

Extending the Thread Class The run method specifies the actions that a thread is to execute and serves the same purpose for the process running on the thread as method main does for a full application program. Like main , run may not be called directly. The containing program calls the start method (inherited from class Thread ), which then automatically calls run . The two most commonly used constructors are: Thread() Thread(String<name>)

Extending Thread Class (cont..)

Thread Class Constructors Thread( ): If this method is used, the system generates a name of the form Thread-n, where n is an integer, starting at zero and increasing in value for further threads. Thus, if three threads are created via the first constructor, they will have names Thread-0 , Thread-1 and Thread-2 respectively. Thread(String<name>): This method provides a name for the thread via its argument. Note: Whichever constructor is used, method getName may be used to retrieve the name. Example: Thread firstThread = new Thread(); Thread secondThread = new Thread("namedThread"); System.out.println(fi rstThread.getName()); System.out.println(secondThread.getName());

Thread Life Cycle

sleep( ) and interrupt( ) Methods sleep( ) method: This method is used to make a thread pause for a specified number of milliseconds. Example: myThread.sleep(1500); //Pause for 1.5 seconds. interrupt( ) method: This method may be used to interrupt an individual thread. In particular this method may be used by other threads to awaken a sleeping thread before that thread’s sleeping time has expired. Note: Please refer the Example in the Text Book.

Explicitly Implementing the Runnable Interface We first create an application class that explicitly implements the Runnable interface. Then, in order to create a thread, we instantiate an object of our Runnable class and wrap it in a Thread object. We do this by creating a Thread object and passing the Runnable object as an argument to the Thread constructor. There are two Thread constructors that allow us to do this. Thread (Runnable<object>) Thread(Runnable<object>, String<name>) Example - Please Refer Text Book

Multithreading

Single-Threaded Server

Multithreaded Server

Multithreaded Servers Why use Multithreaded Server? There is a fundamental and important limitation associated with all the server programs encountered so far. They can handle only one connection at a time. This restriction is simply not feasible for most real-world applications and would render the software useless. There are two possible solutions: use a non-blocking server use a multithreaded server

Advantages of Multithreaded Servers: It offers a clean implementation, by separating the task of allocating connections from that of processing each connection. It is robust, since a problem with one connection will not affect other connections.

Multithreaded Socket Server

Multithreaded Server Process 1. The basic technique involves a two-stage process: The main thread (the one running automatically in method main ) allocates individual threads to incoming clients. The thread allocated to each individual client then handles all subsequent interaction between that client and the server (via the thread’s run method). Note: Please refer the example program in the Text Book.

Locks We can require a thread to obtain a lock on the object that is to be updated. Only the thread that has obtained the lock may then update the object. Any other (updating) thread must wait until the lock has been released. Once the first thread has finished its updating, it should release the lock, making it available to other such threads. (Note that threads requiring read-only access do not need to obtain a lock).

Deadlocks

Deadlocks (cont..) A state of deadlock occurs when threads are waiting for events that will never happen. Consider the example illustrated in Fig. 3.5 . Here, thread1 has a lock on resource res1 , but needs to obtain a lock on res2 in order to complete its processing (so that it can release its lock on res1 ). 3. At the same time, however, thread2 has a lock on res2 , but needs to obtain a lock on res1 in order to complete its processing.

Synchronising Threads Locking is achieved by placing the keyword synchronized in front of the method definition or block of code that does the updating. Example public synchronized void updateSum(int amount) { sum+=amount; }

Synchronising Threads (cont..) In order to improve thread efficiency and to help avoid deadlock, the following methods are used: wait() notify() notifyAll() wait() method: If a thread executing a synchronized method determines that it cannot proceed, then it may put itself into a waiting state by calling method wait . This releases the thread’s lock on the shared object and allows other threads to obtain the lock.

notify() and notifyAll() methods notify() method: When a synchronized method reaches completion, a call may be made to notify, which will wake up a thread that is in the waiting state. notifyAll() method: If all threads waiting for a lock on a given object are to be woken, then we use notifyAll . Note: Please refer the producer-consumer problem example from the Text Book.

wait(), notify(), notifyAll() methods