Representation and Management of Data on the Internet

Slides:



Advertisements
Similar presentations
Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
Advertisements

1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.
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.
Object-Oriented Software Engineering Concurrent Programming.
CSE S. Tanimoto Java Threads 1 Java Threads (Outline) Motivation The class Thread Example program: ThreadRace The Runnable interface Example: Clock.
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.
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng 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.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Lecture 4 : JAVA Thread Programming
Threads in Java a tutorial introduction (revision 7) Christian Ratliff Senior Technology Architect Core Libraries.
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
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.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Java Threads Representation and Management of Data on the Internet.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Threads CSCE 190 – Java Instructor: Joel Gompert Wed, Aug 3, 2004.
1 Web Based Programming Section 8 James King 12 August 2003.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 6 (6.7 & 6.9 only) - Threads & Mutex Threads –A Thread is a basic unit of CPU utilization consisting of a program counter, register set and stack.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SurfaceView.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Multi-Threading in Java
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
CMSC 330: Organization of Programming Languages Threads.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
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.
Java Thread Programming
Doing Several Things at Once
Multithreaded applets
Java Multithreading.
Lecture 9 Object Oriented Programming Using Java
Java Programming Language
Java Concurrency.
Threads Chate Patanothai.
Advanced Programming Behnam Hatami Fall 2017.
Java Concurrency.
Advanced Programming in Java
Multithreading.
Java Based Techhnology
Multithreading.
Java Threads (Outline)
Programming with Shared Memory Java Threads and Synchronization
Programming with Shared Memory Java Threads and Synchronization
Java Threads (Outline)
برنامه‌نویسی چندنخی Multi-Thread Programming
Computer Science 2 06A-Java Multithreading
Threads in Java James Brucker.
Multithreading in java.
Gentle Introduction to Threads
Lecture 19 Threads CSE /6/2019.
CMSC 202 Threads.
Polymorphism Sometimes it is necessary to “see” an object of a certain class as being (also) of another: Polymorphism An object can be seen as being of.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Representation and Management of Data on the Internet Java Threads Representation and Management of Data on the Internet

Threads: Objects with an own line of execution Main Thread1 Thread2 Thread3

Multitasking and Multithreading Multitasking refers to a computer's ability to perform multiple jobs concurrently more than one program are running concurrently, e.g., UNIX A thread is a single sequence of execution within a program Multithreading refers to multiple threads of control within a single program each program can run multiple threads of control within it, e.g., Web Browser

Concurrency vs. Parallelism CPU CPU1 CPU2

Threads and Processes CPU Process 1 Process 2 Process 3 Process 4 main run GC

What are Threads Good For? To maintain responsiveness of an application during a long running task. To enable cancellation of separable tasks. Some problems are intrinsically parallel. To monitor status of some resource (DB). Some APIs and systems demand it: Swing.

Application Thread When we execute an application: The JVM creates a Thread object whose task is defined by the main() method It starts the thread The thread executes the statements of the program one by one until the method returns and the thread dies

Multiple Threads in an Application Each thread has its private run-time stack If two threads execute the same method, each will have its own copy of the local variables the methods uses However, all threads see the same dynamic memory (heap) Two different threads can act on the same object and same static fields concurrently

Creating Threads There are two ways to create our own Thread object Subclassing the Thread class and instantiating a new object of that class Implementing the Runnable interface In both cases the run() method should be implemented

Implementing Threads The most simple way: extending the Thread class and overwriting the run() method Thread is an existing class (no need to import it) Has a run () method (originally without instructions) whose instructions can be run in parallel to what is currently running For that the start() method of and instance of the new class should be called Declaration of the new class public class MyThread extends Thread { And somewhere should appear: public void run() { //instructions to be executed in parallel } An this is used … MyThread mt = new MyThread(); Mt.start() NOTAS

Example from Java tutorial public class SimpleThread extends Thread { String nombre; public SimpleThread(String str) { nombre = str; } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i+" "+nombre); try { this.sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) {} System.out.println("DONE! " + getName()); this.sleep(miliseconds) should appear in a try-catch block NOTAS

Using the thread public class TwoThreadsTest { public static void main (String[] args) { SimpleThread t1,t2; t1 = SimpleThread("Jamaica"); t2 = SimpleThread("Fiji"); t1.start(); t2.start() } start() triggers the parallel execution of run. There are other methods: like join() and interrupt(), suspend(), resume(), stop() are deprecated See ActiveClock and compare it with ActiveClock2 NOTAS

Implementing Runnable public class RunnableExample implements Runnable { public void run () { for (int i = 1; i <= 100; i++) { System.out.println (“Runnable: ” + i); }

Example – an applet that is A Runnable Object The Thread object’s run() method calls the Runnable object’s run() method Allows threads to run inside any object, regardless of inheritance Example – an applet that is also a thread

Starting the Threads public class ThreadsStartExample { public static void main (String argv[]) { //this is previous way new SimpleThread().start (); //this is the new way new Thread(new RunnableExample ()).start (); }