Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.

Slides:



Advertisements
Similar presentations
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.
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.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Java Threads Representation and Management of Data on the Internet.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
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.
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. Concurrent programming in Java How to make all things run-able?
Concurrent Programming and Threads Threads Blocking a User Interface.
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.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading in JAVA
Object-oriented Programming in Java. © Aptech Ltd. Introduction to Threads/Session 7 2  Introduction to Threads  Creating Threads  Thread States 
Scis.regis.edu ● CS-434: Object-Oriented Programming Using Java Week 8 Dr. Jesús Borrego Adjunct Faculty Regis University 1.
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.
Multi-Threading in Java
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
MultiThreading Sangeetha Parthasarathy 07/10/2001.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
THREAD MODEL.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
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 The objectives of this chapter are:
Chapter 4 – Thread Concepts
Multithreading / Concurrency
Multithreading Lec 23.
Multi Threading.
Java Multithreading.
Lecture 9 Object Oriented Programming Using Java
Multithreading.
Chapter 4 – Thread Concepts
Multithreaded Programming in Java
Lecture 21 Concurrency Introduction
Multithreading in Java
Multithreading Chapter 23.
Multithreading.
Java Based Techhnology
Multithreading.
Multithreaded Programming
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
Multithreading The objectives of this chapter are:
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1

Topik Bahasan Multithreading Synchronized 2

Multithreading in JAVA Java is a multithreaded programming language which means we can develop multi threaded program using JAVA. A multi threaded program contains two or more parts that can run concurrently and each part can handle different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs. 3

Multithreading in JAVA By Definition multitasking is when multiple processes share common processing resources such as a CPU. Multi threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. 4

Multithreading in JAVA The OS divides processing time not only among different applications, but also among each thread within an application. Multi threading enables you to write in a way where multiple activities can proceed concurrently in the same program. 5

Life Cycle of a Thread A Thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. Following diagram shows complete life cycle of a thread. 6

Life Cycle of a Thread 7

Above-mentioned stages are explained here: New : A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also reffered to as a born thread. Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task. 8

Life Cycle of a Thread Above-mentioned stages are explained here: Waiting : Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing. 9

Life Cycle of a Thread Above-mentioned stages are explained here: Timed Waiting : A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs. Terminated (Dead) : A runnable thread enters the terminated state when it completes its task or otherwise terminates. 10

Thread Priorities in JAVA Every Java thread has a priority that helps the OS determine the order in which threads are scheduled. Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5). 11

Thread Priorities in JAVA Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities can not guarantee the order in which execute and very much platform dependent. 12

Create Thread by Implementing Runnable Interface If your class is intended to be executed as a thread then you can achieve this by implementing Runnable interface. You will need to follow three basic steps: 13

Create Thread by Implementing Runnable Interface Step 1: As a first step you need to implement a run() method provided by Runnable interface. This method provides entry point for the thread and you will put you complete business logic inside this method. Following is simple syntax of run() method: public void run () 14

Create Thread by Implementing Runnable Interface Step 2: At second step you will instantiate a Thread object using the following constructor: Thread(Runnable threadObj, String threadName); Where, threadObj is an instance of a class that implements the Runnableinterface and threadName is the name given to the new thread. 15

Create Thread by Implementing Runnable Interface Step 3 Once Thread object is created, you can start it by calling start( ) method, which executes a call to run( ) method. Following is simple syntax of start() method: Void start(); 16

Example (create a new thread and starts it running) 17

Example (create a new thread and starts it running) 18

Example (create a new thread and starts it running) 19

Example (create a new thread and starts it running) 20

Example (create a new thread and starts it running) 21

Example (create Thread by Extending Thread Class) The second way to create is to create a new class that extends Thread class using the following two simple steps. This approach provides more flexibility in handling multiple threads created using available methods in Thread class. 22

Example (create Thread by Extending Thread Class) Step 1 You will need to override run() method in Thread class. This method provides entry point for the thread and you will put you complete business logic inside this method. Following is simple syntax of run() method: Public void run() 23

Example (create Thread by Extending Thread Class) Step 2 Once Thread object created, you can start it by calling start() method which executes a call to run() method. Following is simple syntax of start() method. Void start(); 24

Example (create Thread by Extending Thread Class) 25

Example (create Thread by Extending Thread Class) 26

Example (create Thread by Extending Thread Class) 27

Example (create Thread by Extending Thread Class) 28

Example (create Thread by Extending Thread Class) 29

Thread Methods Following is the list of important methods available in the Thread Class. 30

Thread Methods Following is the list of important methods available in the Thread Class. 31

Thread Methods Following is the list of important methods available in the Thread Class. 32

Thread Methods Following is the list of important methods available in the Thread Class. 33

Thread Methods The previous methods are invoked on a particular Thread object. The following methods in the Thread class are static. Invoking one of the static methods performs the operation on the currently running thread. 34

Thread Methods 35

Thread Methods 36

Example The following ThreadClassDemo program demonstrates some of these methods of the Thread class. Consider a class DisplayMessage which implementsRunnable: 37

Example 38

Example Following is another class which extends Thread class: 39

Example 40

Example 41

Example 42

Example This would produce the following result. You can try this example again and again and you would get different result every time. 43

Example 44

Sumber 45