10/17/2015Vimal1 Threads By www.PPTSWorld.com. 10/17/2015Vimal2 Why use threads?? It is a powerful programming tool Computer users take it for granted.

Slides:



Advertisements
Similar presentations
1 Multithreaded Programming in Java. 2 Agenda Introduction Thread Applications Defining Threads Java Threads and States Examples.
Advertisements

13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Concurrency…leading up to writing a web crawler. Web crawlers.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Multithreading.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
 Traditionally, a process in an operating system consists of an execution environment and a single thread of execution (single activity).  However,
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
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)
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
Computer Engineering Rabie A. Ramadan Lecture 8. Agenda 2 Introduction Thread Applications Defining Threads Java Threads and States Priorities Accessing.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Synchronizing threads, thread pools, etc.
1 Object Oriented Programming Lecture XII Multithreading in Java, A few words about AWT and Swing, The composite design pattern.
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.
Threading and Concurrency COM379T John Murray –
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SurfaceView.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
© 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.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Chapter11 Concurrent. 集美大学 计算机工程学院 Java 程序设计 年 第二版 Concurrent ●Computer users take it for granted that their systems can do more than one thing.
Threads. Readings r Silberschatz et al : Chapter 4.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Multithreading and Garbage Collection Session 16.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
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.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
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.
Multithreading / Concurrency
Multi Threading.
Lecture 9 Object Oriented Programming Using Java
Multithreading.
Multithreaded Programming in Java
Java Threads אליהו חלסצ'י תכנות מתקדם תרגול מספר 6
Java Based Techhnology
Multithreading.
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Threads and Multithreading
Representation and Management of Data on the Internet
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

10/17/2015Vimal1 Threads By

10/17/2015Vimal2 Why use threads?? It is a powerful programming tool Computer users take it for granted that their systems can do more than one thing at a time. They assume that they can continue to work in a word processor, while other applications download files, manage the print queue, and stream audio. Even a single application is often expected to do more than one thing at a time. Example of a web server: Many clients accessing….impossible to handle without threads!

10/17/2015Vimal3 Use of threads cont..  In Internet: 100 simultaneous connections means 100 processes to handle. Just imagine 100 million internet users!!  Similarly with web servers..performance crawls with more simultaneous connections

10/17/2015Vimal4 Another example  Streaming audio application must simultaneously read the digital audio off the network, decompress it, manage playback, and update its display.  Even the word processor should always be ready to respond to keyboard and mouse events, no matter how busy it is reformatting text or updating the display.  Software that can do such things is known as concurrent software.

10/17/2015Vimal5 Concurrent programming  The Java platform is designed from the ground up to support concurrent programming, with basic concurrency support in the Java programming language and the Java class libraries.  Since version 5.0, the Java platform has also included high-level concurrency APIs in the java.util.concurrent packages.

10/17/2015Vimal6 Concurrency vs. Parallelism CPU1 CPU2 main run CPU main run main run main run main RAM this.count

10/17/2015Vimal7 Process & threads  In concurrent programming, there are two basic units of execution: processes and threads.  In the Java programming language, concurrent programming is mostly concerned with threads. However, processes are also important. Threads are light weight processes!  A computer system normally has many active processes and threads. This is true even in systems that only have a single execution core, and thus only have one thread actually executing at any given moment.  Processing time for a single core is shared among processes and threads through an OS feature called time slicing.

10/17/2015Vimal8 What is a thread?  A thread is like a sequential program.  A single thread has a beginning, a sequence, and an end and at any given time during the runtime of the thread, there is a single point of execution.  However, a thread itself is not a program; it cannot run on its own. Rather, it runs within a program.

10/17/2015Vimal9 Multiple threads

10/17/2015Vimal10 Creating Threads  Two ways of creating threads: Extending a class. Implementing an interface  Extending a class is the way Java inherits methods and variables from a parent class. In this case, one can only extend or inherit from a single parent class.  This limitation within Java can be overcome by implementing interfaces, which is the most common way to create threads.

10/17/2015Vimal11 Implementing thread  start()  run()  To get information from a finished thread is a challenge especially in a multi-threaded system  return - is used

10/17/2015Vimal12 Extending a class 1. import java.util.*; 2. class TimePrinter extends Thread { 3. int pauseTime; 4. String name; 5. public TimePrinter(int x, String n) 6. { pauseTime = x; name = n; } 7. public void run() 8. { 9. while(true) { 10. try { 11. System.out.println(name + ":" + new Date(System.currentTimeMillis())); 12. Thread.sleep(pauseTime); } catch(Exception e) { } 13. } }

10/17/2015Vimal13 1. public static void main(String args[]) { 2. TimePrinter tp1 = new TimePrinter(1000, "Fast Guy"); 3. tp1.start(); 4. TimePrinter tp2 = new TimePrinter(3000, "Slow Guy"); 5. tp2.start(); 6. } }

10/17/2015Vimal14 Implementing Runnable interface 1. import java.util.*; 2. class TimePrinter implements Runnable { 3. int pauseTime; 4. String name; 5. public TimePrinter (int x, String n) 6. { pauseTime = x; name = n; } 7. public void run() { while(true) { 8. try { 9. System.out.println(name + ":" + new Date(System.currentTimeMillis())); Thread.sleep(pauseTime); 10. } catch(Exception e) { } 11. } }

10/17/2015Vimal15 cont 1. public static void main(String args[]) {  Thread t1 = new Thread (new TimePrinter(1000, "Fast Guy")); t1.start();  Thread t2 = new Thread (new TimePrinter(3000, "Slow Guy")); t2.start(); 4. } }

10/17/2015Vimal16

10/17/2015Vimal17 Creating Multiple threads Class newThread implements Runnable { String name;Thread t; NewThread(String threadname) { name=threadname; t=newThread(this,name); System.out.println(“New Thread:”+t); t.start(); } public void run() {

10/17/2015Vimal18 Cont.. try{ For(int i=00;i>0;i--) { System.out.println(name +”:”+i); Thread.sleep(1000); }catch(IntrrruptedException e){} System.out.println(name +’exiting); }

10/17/2015Vimal19 Cont.. Class MultiThreadDemo { public static void main(String args[]) { new NewThread(“one”); new NewThread(“two”); new NewThread(“three”); try{ Thread.sleep(10000); }catch(InterruptedException e){} }}

10/17/2015Vimal20 Synchronization  Two or more threads accessing the same data simultaneously may lead to loss of data integrity.  Java uses the concept of semaphore or monitor to enable this.  Monitor is an object used as a mutually exclusive lock.  Synchronized

10/17/2015Vimal21 C o n s u m e r. j a v a 1.Class Consumer implements Runnable { 2.Stock c; 3.Thread t; 4.Consumer (Stock c) 5. { this.c=c; 6. t=new Thread(this,”consumer thread”); 7. t.start(); } 8.public void run(){ 9. while(true){ 10. try{ 11. t.sleep(750); 12. }catch(InterruptedEcception e){} 13.c.getStock((int)(Math.random()*100)); 14. } } 15.void stop(){ t.stop() } }

10/17/2015Vimal22 P r o d u c e r. j a v a 1.Class Producer implements Runnable { 2.Stock c; 3.Thread t; 4.Producer (Stock c) 5. { this.c=c; 6. t=new Thread(this,”producer thread”); 7. t.start(); } 8.public void run() { 9. while(true) { 10. try{ 11. t.sleep(750); 12. }catch(InterruptedExcception e){} 13. c.getStock((int)(Math.random()*100)); 14.} } 15.void stop(){ t.stop() } }

10/17/2015Vimal23 Stock.java 1.Class Stock { 2. int goods=0; 3.Public synchronized void addStock(int i) 4.{ goods=goods+i; 5. System.out.println(“Stock added:”+i); 6. System.out.println(“Present Stock:”+goods); 7. notify(); 8.} 9.public synchronised int getStock(int j) 10. { while(true) 11. {

10/17/2015Vimal24 if(goods >=j) { goods=goods=goods-j; System.out.println(“Stock taken away:”+i); System.out.println(“Present Stock:”+goods); break; }else { System.out.println(“Stock not enough:”+i); System.out.println(“Waiting for stocks to come:”+goods); try { wait(); }catch(InterruptedException e){} } return goods; }

10/17/2015Vimal25 public static void main(String args[]) { Stock= new Stock(); Producer p= new Producer(j); Consumer c=new Consumer(j); try{ Thread.sleep(10000); p.stop(); c.stop(); }catch(InterrruptedException e){} }