Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java -

Slides:



Advertisements
Similar presentations
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
Advertisements

$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200.
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500.
wwwcsif.cs.ucdavis.edu/~jacksoni
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik virtuelle Maschine Verteilte.
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java -
Ozone Level ppb (parts per billion)
Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Multithreading Example from Liang textbook.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
System Programming Practical Session 5 Liveness, Guarded Methods, and Thread Timing.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Albert Johnson Molly Richardson Andrew Kruth.  Threads Runnable interface sleep()  GUIs repaint() paintComponent()
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Object-Oriented Software Engineering Concurrent Programming.
26-Jun-15 Threads and Turns. Thread review There are two ways to create a Thread object.. Extend Thread and supply a run method: class MyThread extends.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
28-Jun-15 Producer-Consumer An example of using Threads.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
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.
Lecture 4 : JAVA Thread Programming
1 Java Threads and Synchronization Review Modified from slides taken from
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Threading Eriq Muhammad Adams J
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Threading and Concurrency COM379T John Murray –
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
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.
Java Producer-Consumer Monitor From: Concurrent Programming: The Java Programming Language By Steven J. Hartley Oxford University Press, 1998.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
 2002 Prentice Hall, Inc. All rights reserved. Outline HoldIntegerUnsyn chronized.java Line 4 Lines 7-13 Lines // Fig. 15.6: HoldIntegerUnsynchronized.java.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Multithreading and Garbage Collection Session 16.
Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.
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.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
CSC321 §8 Implementing FSP Models in Java 1 Section 8 Implementing FSP Models in Java.
Distributed and Parallel Processing George Wells.
Java Concurrency.
System Programming Practical Session 7
Threads Chate Patanothai.
Java Concurrency.
null, true, and false are also reserved.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Principles of Software Development
Multithreading.
برنامه‌نویسی چندنخی Multi-Thread Programming
Computer Science 2 06A-Java Multithreading
Philosopher Example class PhilosophersShare {
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Threads 1 Thread Object wait() notify() notifyAll() start() run() sleep() join() yield() isAlive() > Runnable run()

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Threads 2 Thread MyThread run() start() run() sleep() join() > Runnable run() Thread Klasse_A Klasse_B run() start() join() start() run() sleep() join() >

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Threads 3 public class Thr_1 { static public void main(String args[]) { int i; MyThread thread[]; thread = new MyThread[5]; for (i = 0; i < 5; i++) { thread[i] = new MyThread(i); thread[i].start(); } try { for (i = 0; i < 5; i++) thread[i].join(); } catch (InterruptedException e) { System.out.println(e); } } class MyThread extends Thread { int id; MyThread (int id) { this.id = id; } public void run() { int z; for (z = 0; z < 3; z++) try { sleep(Math.round( * Math.random())); System.out.println("MyThread " + id + " cycle " + z); } catch (InterruptedException e) { System.out.println(e); } } MyThread 2 cycle 0 MyThread 4 cycle 0 MyThread 4 cycle 1 MyThread 2 cycle 1 MyThread 0 cycle 0 MyThread 4 cycle 2 MyThread 1 cycle 0 MyThread 3 cycle 0 MyThread 0 cycle 1 MyThread 2 cycle 2 MyThread 3 cycle 1 MyThread 0 cycle 2 MyThread 1 cycle 1 MyThread 1 cycle 2 MyThread 3 cycle 2

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Threads 4 public class Thr_2 { static public void main(String args[]) { int i; MyRunThread thread[]; thread = new MyRunThread [4]; for (i = 0; i < 4; i++) { thread[i] = new MyRunThread(i); thread[i].start(); } try { for (i = 0; i < 4; i++) thread[i].join(); } catch (InterruptedException e) { System.out.println(e); } } MyThread 3 cycle 0 MyThread 1 cycle 0 MyThread 0 cycle 0 MyThread 1 cycle 1 MyThread 2 cycle 0 MyThread 3 cycle 1 MyThread 3 cycle 2 MyThread 0 cycle 1 MyThread 0 cycle 2 MyThread 2 cycle 1 MyThread 1 cycle 2 MyThread 2 cycle 2 class A { int a; A(int a) {this.a = a;} } class MyRunThread extends A implements Runnable { Thread thread; MyRunThread (int id) { super(id); thread = new Thread(this); } public void start() { thread.start(); } public void join() throws InterruptedException { thread.join(); } public void run() { int z; for (z = 0; z < 3; z++) try { thread.sleep(Math.round ( * Math.random())); System.out.println("MyThread " + a + " cycle " + z); } catch (InterruptedException e) { System.out.println(e); } }

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Threads 5 public class Thr_3 { static public void main(String args[]) { Monitor behaelter; int i; Thread thread[]; behaelter = new Monitor(100, 200); thread = new Thread [5]; thread[0] = new ZuThread(70, 100, behaelter); thread[1] = new ZuThread(40, 300, behaelter); thread[2] = new ZuThread(30, 600, behaelter); thread[3] = new AbThread(50, 500, behaelter); thread[4] = new AbThread(90, 200, behaelter); for (i = 0; i < thread.length; i++) thread[i].start(); try { for (i = 0; i < thread.length; i++) thread[i].join(); } catch (InterruptedException e) { System.out.println(e); } Zu 70 level 70 Zu 40 level 110 Zu 70 level 180 Ab 50 level 130 Zu 70 level 200 Ab 90 level 110 Zu 30 level 140 Zu 40 level 180 Ab 50 level 130 Zu 30 level 160 Zu 30 level 190 Ab 90 level 100 Zu 40 level 140

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Threads 6 class ZuThread extends Thread { Monitor b; int menge, intervall; ZuThread (int menge, int intervall, Monitor b) { this.menge = menge; this.b = b; this.intervall = intervall; } public void run() { for (int i = 0; i < 3; i++) { try { sleep(intervall); } catch (InterruptedException e) { System.out.println(e); } System.out.println ("Zu " + menge + " level " + b.zu(menge)); } } class AbThread extends Thread { Monitor b; int menge, intervall; AbThread (int menge, int intervall, Monitor b) { this.menge = menge; this.b = b; this.intervall = intervall; } public void run() { for (int i = 0; i < 2; i++) { try { sleep(intervall); } catch (InterruptedException e) { System.out.println(e); } System.out.println ("Ab " + menge + " level " + b.ab(menge)); } } class Monitor { private int min, max, level; Monitor(int min, int max) { this.min = min; this.max = max; level = 0; } synchronized int zu(int x) { while ((level + x) > max) try { wait();} catch (InterruptedException e) { System.out.println(e); } level += x; notify(); return level; } synchronized int ab(int x) { while ((level - x) < min) try { wait(); } catch (InterruptedException e) { System.out.println(e); } level -= x; notify(); return level; }

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Threads 7 public class Thr_5 { static public void main(String args[]) { Manager m = new Manager(); int i; Philosoph phil[] = new Philosoph[Manager.N]; for (i = 0; i < phil.length; i++) phil[i] = new Philosoph(i, m); for (i = 0; i < phil.length; i++) phil[i].start(); try { for (i = 0; i < phil.length; i++) phil[i].join(); } catch (InterruptedException e) { System.out.println(e); } es essen 0 es essen 0 2 es essen 4 es essen 1 es essen 3 es essen 0 es essen 0 2 es essen 4 es essen 1 4 es essen 1 3 es essen 0 es essen 0 2 es essen 4 es essen 1 es essen 1 3 es essen 0 es essen 0 2 es essen 4 es essen 1 es essen 1 3

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Threads 8 class Philosoph extends Thread { Manager m; int num; Philosoph (int num, Manager m) { this.num = num; this.m = m; } void denken() { try { sleep(800); } catch(InterruptedException e) {}; } void essen() { try { sleep(200); } catch InterruptedException e) {}; } public void run() { for (int i = 0; i < 4; i++) { denken(); m.nehmeGabeln(num); essen(); m.ablegenGabeln(num); } class Manager { public final static int N = 5; private int zustand_P[]; int i; Manager() { zustand_P = new int[N]; for (i = 0; i < zustand_P.length; i++) zustand_P[i] = 0; // denken } private int links(int pos){ return (pos + N - 1) % N;} private int rechts(int pos){ return (pos + 1) % N; } synchronized void nehmeGabeln(int pos) { while ( zustand_P[links(pos)] == 1 // essen || zustand_P[rechts(pos)]== 1) // essen try { wait();} catch (InterruptedException e) {} zustand_P[pos] = 1; // essen zeigeZustand(); } synchronized void ablegenGabeln(int pos) { zustand_P[pos] = 0; // denken notify(); } synchronized void zeigeZustand() { System.out.print("es essen "); for (i = 0; i < zustand_P.length; i++) if (zustand_P[i] == 1) System.out.print(" " + i); System.out.println(); }