Philosopher Example class PhilosophersShare {

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java -
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Multithreading Example from Liang textbook.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
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.
Practice Session 7 Synchronization Liveness Guarded Methods Model
Deadlock CS Introduction to Operating Systems.
CS 149: Operating Systems February 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
System Programming Practical Session 5 Liveness, Guarded Methods, and Thread Timing.
Albert Johnson Molly Richardson Andrew Kruth.  Threads Runnable interface sleep()  GUIs repaint() paintComponent()
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Public class MyThread1 extends Thread { public void start() { } public void run() { System.out.print(“Hello \n”) } public class MyThread2 extends Thread.
ThreadThread Thread Basics Thread Synchronization Animations.
Definitions Process – An executing program
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Java Threads CS Introduction to Operating Systems.
Multithreading.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
7a.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts with Java Module 7a: Classic Synchronization Background The Critical-Section Problem.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Synchronization II: CPE Operating Systems
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 7: Process Synchronization Background The Critical-Section Problem.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Public class Edo1 { private static double euler(double y, double h, double t, Derivada d) { return y + h * d.f(y, t); } public static void euler(double.
© 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.
7.1 Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS 149: Operating Systems February 19 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Multithreading and Garbage Collection Session 16.
1 Java Programming Java Programming II Concurrent Programming: Threads ( II)
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Deadlock CS Introduction to Operating Systems.
Synchronization Deadlocks and prevention
Multithreading Lec 23.
Lecture 9 Object Oriented Programming Using Java
using System; namespace Demo01 { class Program
Java Concurrency.
Chapter 6-7: Process Synchronization
Section 5.7 Concurrency, Interference, and Synchronization.
System Programming Practical Session 7
Threads Chate Patanothai.
Java Concurrency.
Multithreaded Programming in Java
Condition Variables and Producer/Consumer
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
Lecture 25 Syed Mansoor Sarwar
Condition Variables and Producer/Consumer
Module 7a: Classic Synchronization
Java Threads אליהו חלסצ'י תכנות מתקדם תרגול מספר 6
Multithreading 2 Lec 24.
Assignment 7 User Defined Classes Part 2
Code Animation Examples
برنامه‌نویسی چندنخی Multi-Thread Programming
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Java Thread.
More IPC B.Ramamurthy 4/15/2019.
Multithreading in java.
Lecture 26 Syed Mansoor Sarwar
Sampath Kumar S Assistant Professor, SECE
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Presentation transcript:

Philosopher Example class PhilosophersShare { int [] state = new int[6]; static final int THINKING = 0; static final int HUNGRY = 1; static final int EATING = 2; public Philosopher() { for(int i=0; i<6; i++) { state[i] = THINKING; } } public synchronized void pickUp(int id) { state[id] = HUNGRY; test(id); while(state[id] != EATING) { try { wait(); } catch(InterruptedException e) { } public synchronized void putDown(int id) { state[id] = THINKING; test((id + 5) % 6); test((id+1) % 6); private test(int id) { if((state[(id + 5) % 6] != EATING) && (state[id] == HUNGRY) && (state[(id + 1) % 6] != EATING) { state[id] = EATING; notifyAll(); public void thinkOrEat() { try { Thread.sleep((int)(2000 * Math.random()));

Philosopher Example class Philosopher implements Runnable { private int id; private PhilosopherShare share; public Philosopher(int id, PhilosopherShare share) { this.id = id; this.share = share; } public void run() { for(;;) { System.out.println(“Philosopher “ + id + “ is thinking”); share.thinkOrEat(); share.pickUp(id); System.out.println(“Philosopher “ + id + “ is eating”); share.putDown(id);

Philosopher Example class DiningPhilosophers { public static void main(String [] args) { PhilosopherShare share = new PhilosopherShare(); Philosopher [] p = new Philosopher[6]; for(int i=0; i<6; i++) { p[i] = new Philosopher(i, share); } Thread [] t = new Thread[6]; t[i] = new Thread(p[i]); t[i].start();