1 Web Based Programming Section 8 James King 12 August 2003.

Slides:



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

Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
CSE S. Tanimoto Java Threads 1 Java Threads (Outline) Motivation The class Thread Example program: ThreadRace The Runnable interface Example: Clock.
Concurrency…leading up to writing a web crawler. Web crawlers.
Multithreading A thread is the flow of execution, from beginning to end, of a task in a program. With Java, you can launch multiple threads from a program.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Programming in Java; Instructor:Alok Mehta Threads1 Programming in Java Threads.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
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.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Java Threads CS Introduction to Operating Systems.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
50.003: Elements of Software Construction Week 5 Basics of Threads.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
EE2E1. JAVA Programming Lecture 8 Multi-threading.
Java Programming: Advanced Topics
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
1 Java Threads Instructor: Mainak Chaudhuri
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
10/17/2015Vimal1 Threads By 10/17/2015Vimal2 Why use threads?? It is a powerful programming tool Computer users take it for granted.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
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.
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. Concurrent programming in Java How to make all things run-able?
Threading Eriq Muhammad Adams J
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
1 Introduction to Threads Computers can perform many tasks concurrently – download a file, print a file, receive , etc. Sequential languages such.
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.
Multithreading in JAVA
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Java Thread and Memory Model
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.
© 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
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
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.
Java Thread Programming
Multi Threading.
Java Multithreading.
Lecture 9 Object Oriented Programming Using Java
Multithreading.
Chapter 19 Java Never Ends
Java Based Techhnology
Multithreading.
Multithreaded Programming
Java Threads (Outline)
Java Threads (Outline)
Java Thread.
Multithreading in java.
Threads and Multithreading
Representation and Management of Data on the Internet
Lecture 19 Threads CSE /6/2019.
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 Web Based Programming Section 8 James King 12 August 2003

2 Topics Object oriented concepts How Java programs work First steps Importing classes Graphical classes Applets Input and Output Streams Multiple threads of execution Network programming

3 Multi-Threading Most network servers (and other types of programs) handle several clients at the same time. This requires doing several things concurrently. In the past this meant running multiple copies of the program with the processor quickly swapping between them Modern Systems allow a single program to perform multiple tasks concurrently with the processor switching between tasks. Each of these separately executable parts of the program is called a Thread.

4 Multiple threads of execution – Terminology A process is the code and data of a program (in our case the program is split into objects) A thread runs inside a process executing the code and read and writing the data (it has its own program counter and register contents) All the threads inside a process see the same code and share the same data A Java program can have many threads in side the same program running at the same time all performing different (or the same) tasks

5 Multiple threads of execution Thread Problems Multiple threads sharing the same data can cause inconsistency problems because each thread can overwrite the changes written by another thread. Imagine two threads both updating a shared (static) variable they both simple want to increase it by one. The variable starts at 0

6 Thread Problems shared variable value 0 1.Reads shared variable (reads the value 0) 3.adds one to the value it read (result 1) 4.writes the result (1) back to the shared variable 5.adds one to the value it read (result 1) 6.writes the result (1) back to the shared variable 2.Reads shared variable (reads the value 0) Thread 2Thread 1 shared variable value 1

7 How to make a Thread - inheritance Your class can extend the class Thread and provide a run method Create an instance of the class Call start() to start the run method in a independent thread

8 How to make a thread using inheritance public class monster extends Thread { public void run() { for (int i=0;i<10;i++) { System.out.println("growl"); } } public static void main (String args[]) { monster snotty=new monster(); monster grotty=new monster(); snotty.start(); grotty.start(); }

9 Making a thread using Interfaces If your class already extends another class you can implement the Runnable interface instead You still provide a run method to start the thread first you have to create a thread around your class and then you can start() it

10 Making a thread using Interfaces public class monster2 implements Runnable { public void run() { for (int i=0;i<10;i++) { System.out.println("growl"); } } public static void main (String args[]) { monster2 snotty=new monster2(); monster2 grotty=new monster2(); Thread m1=new Thread(snotty); Thread m2=new Thread(grotty); m1.start(); m2.start(); }

11 Stopping a Thread Once a thread has performed its task it should be stopped However Java has removed the methods to suspend and stop a thread The only safe way to stop a thread is for the run method to exit

12 Stopping a Thread boolean exit=false; public void finish() { exit=true; } public void run() { while(exit==false) { System.out.println(myname+": growl"); } }

13 Stopping a Thread public static void main (String args[]) { stop snotty=new stop(); stop grotty=new stop(); snotty.start(); grotty.start(); try { Thread.sleep(10000);} catch(Exception e) {} snotty.finish(); grotty.finish(); }

14 Waiting for a Thread – Busy Waiting You can wait for a thread to finish by using isAlive() while snotty.isAlive() { } However this is called busy waiting because the loop is always executing as fast as possible and using up a lot processing power

15 Waiting for a thread - Polling You could add a sleep (which does not take up processing power). However this is still inefficient because the loop will still execute many times This is called polling – checking every so often – in this case every 100 milli seconds While snotty.isAlive() { Thread.sleep(100); }

16 Waiting for a Thread - join Join makes your thread block until the other thread finishes. Join does not busy wait or poll and does not use up processing power It is the best solution to the problem snotty.join();

17 Thread Priorities In some cases some threads are more important than others and you may want these to receive a larger share of the available processing power The Java reference documentation is a little sketchy about thread priorities