THREADS.

Slides:



Advertisements
Similar presentations
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Advertisements

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 threads Threaded class extends thread Synchronized class is a monitor Runnable interface gives thread methods All code is (C) Copyright 1999 by Deitel.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
10. Multithreading 1 public class SumTest { public static void main(String a1[]) public static void main(String a1[]) { int a, b, sum; int a, b, sum; a.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
2. Java and Concurrency Prof. O. Nierstrasz Selected material © Magee and Kramer.
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.
11-Jun-15 Producer-Consumer An example of using Threads.
Object-Oriented Software Engineering Concurrent Programming.
CP — Concurrent Programming 2. Java and Concurrency Prof. O. Nierstrasz Wintersemester 2005 / 2006.
16-Jun-15 Producer-Consumer An example of using Threads.
Java Programming Transparency No. 1 Lecture 7. Java Threads Cheng-Chia Chen.
Multithreading. Introduction Performing operations concurrently (in parallel) –We can walk, talk, breathe, see, hear, smell... all at the same time –Computers.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
 2002 Prentice Hall, Inc. All rights reserved. Week 4 – Multithreading Outline 4.1 Introduction 4.2 Class Thread : An Overview of the Thread Methods 4.3.
28-Jun-15 Producer-Consumer An example of using Threads.
Threads Just Java: C10–pages 251- C11–pages 275-
Java Programming Transparency No. 1 Lecture 7. Java Threads Cheng-Chia Chen.
„Threads”. Threads Threads - basics A thread is a single sequential flow of control within a program. A thread itself is not a program; it cannot run.
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.
© Amir Kirsh Threads Written by Amir Kirsh. 2 Lesson’s Objectives By the end of this lesson you will: Be familiar with the Java threads syntax and API.
תכנות מקבילי - I יישום ב- Java. References קורס "תכנות מקבילי ומבוזר", הפקולטה למדעי המחשב, הטכניון. קורס "מערכות מידע מבוזרות", הפקולטה להנדסת תעשייה.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Threads in Java a tutorial introduction PdD Marco Antonio Ramos Corchado February 2009.
 2002 Prentice Hall. All rights reserved. 1 Chapter 14 – Multithreading Outline 14.1 Introduction 14.2 Thread States: Life Cycle of a Thread 14.3 Thread.
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.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 16 – Multithreading Outline 16.1 Introduction 16.2 Thread States: Life Cycle of a Thread 16.3 Thread.
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.
CS884 (Prasad)java8Threads1 Concurrent Programming Multithreading.
Threads CSCE 190 – Java Instructor: Joel Gompert Wed, Aug 3, 2004.
 2002 Prentice Hall, Inc. All rights reserved. Introduction Concurrency normally available in OS primitives Java provides built-in multithreading –Multithreading.
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.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Advanced Programming, Based on LY Stefanus’s slides slide 7.1 Multithreading : Using the Runnable interface.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 16 – Multithreading Outline 16.1 Introduction 16.2 Thread States: Life Cycle of a Thread 16.3.
 2002 Prentice Hall, Inc. All rights reserved. Outline HoldIntegerUnsyn chronized.java Line 4 Lines 7-13 Lines // Fig. 15.6: HoldIntegerUnsynchronized.java.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
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.
Threads in Java 1 OOutline Introduction Class Thread : An Overview of the Thread Methods Thread States: Life Cycle of a Thread Thread Priorities and Thread.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Concurrent Programming.
MultiThreading Programming
Java Concurrency.
System Programming Practical Session 7
Threads Chate Patanothai.
Java Concurrency.
Chapter 15 - Multithreading
null, true, and false are also reserved.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Java Based Techhnology
Principles of Software Development
13: Concurrency Definition:  A thread is a single sequential flow of control within a program. Some texts use the name lightweight process instead of thread.
Threads in Java (Deitel & Deitel)
Threads in Java James Brucker.
Threads in Java (Deitel & Deitel)
Representation and Management of Data on the Internet
Gentle Introduction to Threads
Multiplexing/Demux.
Lecture 19 Threads CSE /6/2019.
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:

THREADS

Example -1

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference ref1 “Jamaica” ---------------- sleep run setName getName : public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference ref1 “Jamaica” ---------------- sleep run setName getName : public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference ref1 “Jamaica” ---------------- sleep run setName getName : 0 Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference ref1 “Jamaica” ---------------- sleep run setName getName : 0 Jamaica 401 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 DONE! Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 DONE! Jamaica 9 Fiji 373 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 DONE! Jamaica 9 Fiji 373 DONE! Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 DONE! Jamaica 9 Fiji 373 DONE! Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }

0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 DONE! Jamaica 9 Fiji 373 DONE! Fiji

Example -2

class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); reference thread1 “thread1” Sleeptime=12708 ---------------- sleep run setName getName : public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); }

class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); reference reference thread1 thread2 “thread1” Sleeptime=12708 ---------------- sleep run setName getName : “thread2” Sleeptime=18411 ---------------- sleep run setName getName : public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); }

class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); reference reference thread1 thread2 “thread1” Sleeptime=12708 ---------------- sleep run setName getName : “thread2” Sleeptime=18411 ---------------- sleep run setName getName : reference public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); } thread3 “thread3” Sleeptime=33617 ---------------- sleep run setName getName :

class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); reference reference thread1 thread2 “thread1” Sleeptime=12708 ---------------- sleep run setName getName : “thread2” Sleeptime=18411 ---------------- sleep run setName getName : reference reference public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); } thread3 thread4 “thread3” Sleeptime=33617 ---------------- sleep run setName getName : “thread4” Sleeptime=15264 ---------------- sleep run setName getName :

class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); reference reference thread1 thread2 “thread1” Sleeptime=12708 ---------------- sleep run setName getName : “thread2” Sleeptime=18411 ---------------- sleep run setName getName : reference reference public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); } thread3 thread4 “thread3” Sleeptime=33617 ---------------- sleep run setName getName : “thread4” Sleeptime=15264 ---------------- sleep run setName getName :

Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 Name: thread3; sleep: 33617 Name: thread4; sleep: 15264 Starting threads public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); }

Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 Name: thread3; sleep: 33617 Name: thread4; sleep: 15264 Starting threads thread1 going to sleep Threads started thread2 going to sleep thread3 going to sleep thread4 going to sleep public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); }

Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 Name: thread3; sleep: 33617 Name: thread4; sleep: 15264 Starting threads thread1 going to sleep Threads started thread2 going to sleep thread3 going to sleep thread4 going to sleep thread1 done sleeping thread4 done sleeping thread2 done sleeping thread3 done sleeping public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); }

Example -3

-1 2498 1094 Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 Consumer retrieving sharedInt value 9 2902 Producer setting sharedInt to 10 Producer finished producing values Terminating Producer Consumer retrieving sharedInt value 10 Consumer retrieved values totaling: 52 Terminating Consumer sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell -1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) sum

public class SharedCell { public static void main( String args[] ) reference sObject reference public class SharedCell { public static void main( String args[] ) Unsyn sObject = new Unsyn(); Producer p = new Producer( sObject ); Consumer c= new Consumer( sObject ); p.start(); c.start(); } sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName :

p.start(); public class Producer extends Thread { reference public class Producer extends Thread { private Unsync sObject; public Producer( Unsyn s ) super( "Producer" ); sObject = s; } public void run() for ( int count = 1; count <= 10; count++ ) try Thread.sleep( ( int ) ( Math.random() * 3000 ) ); catch( InterruptedException exception ) System.err.println( exception.toString() ); sObject.setSharedInt( count ); System.err.println(getName() + " finished producing values" + "\nTerminating " + getName() ); sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell -1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } p.start();

c.start(); public class Consumer extends Thread { reference public class Consumer extends Thread { private Unsyn sObject; public Consumer( Unsyn s) { super( "Consumer" ); sObject = s; } public void run() int value, sum = 0; do { try { Thread.sleep( (int) ( Math.random() * 3000 ) ); catch( InterruptedException exception ) { System.err.println( exception.toString() ); value = sObject.getSharedInt(); sum += value; } while ( value != 10 ); System.err.println( getName() + " retrieved values totaling: “ + sum + "\nTerminating " + getName() ); sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell -1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) c.start(); sum

public class Unsyn { private int sharedInt = -1; public void setSharedInt( int value ) System.err.println( Thread.currentThread().getName() + " setting sharedInt to " + value ); sharedInt = value; } public int getSharedInt() System.err.println( Thread.currentThread().getName() + " retrieving sharedInt value " + sharedInt ); return sharedInt;

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell -1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) -1 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) -1 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 2 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 2 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 2 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 3 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 2 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 4 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 2 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 4 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 6 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 5 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 6 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 5 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 11 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 6 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 11 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 6 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 17 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 7 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 17 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 8 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 17 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 8 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 25 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 8 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 33 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 9 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 33 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 Consumer retrieving sharedInt value 9 2902 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 9 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 42 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 Consumer retrieving sharedInt value 9 2902 Producer setting sharedInt to 10 Producer finished producing values Terminating Producer sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 10 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 42 sum

Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 Consumer retrieving sharedInt value 9 2902 Producer setting sharedInt to 10 Producer finished producing values Terminating Producer Consumer retrieving sharedInt value 10 Consumer retrieved values totaling: 52 Terminating Consumer sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 10 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 52 sum

2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 Consumer retrieving sharedInt value 9 2902 Producer setting sharedInt to 10 Producer finished producing values Terminating Producer Consumer retrieving sharedInt value 10 Consumer retrieved values totaling: 52 Terminating Consumer

Example -4

-1 true reference sObject reference sharedInt=-1 Writeable=true ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell -1 writeablel true Thread (p) for count=1 to 10 { sleeping random time and each time and setShsreInt=count } Thread (c) do { sleeping random time and each time and value=getShsreInt } While (value!=10) sum

Thread ( c ) first wake-up reference T sleep P : 2784 and T sleep C : 388 sObject reference reference Two threads begin to run and going to sleep deferent times 2784 and 388 sharedInt=-1 Writeable=true ---------------- setShsreInt getShraeInt p “Producer” sObject ---------------- sleep run setName getName : c Thread ( c ) first wake-up “Consumer” sObject ---------------- sleep run setName getName : Calling getSharedInt Synchronized getSharedInt running sharedcell -1 if (Writeable=true) -> watting to become T sleep P=2784 writeablel true Thread (p) for count=1 to 10 { sleeping random time and each time and setShsredInt=count } Thread (c) do { sleeping random time and each time and value=getShsredInt } While (value!=10) sum

Thread ( c ) first wake-up reference T sleep P : 2784 and T sleep C : 388 Producer setting sharedInt to 1 sObject reference reference Two threads begin to run and going to sleep deferent times 2784 and 388 sharedInt=-1 Writeable=true ---------------- setShsreInt getShraeInt p “Producer” sObject ---------------- sleep run setName getName : c Thread ( c ) first wake-up “Consumer” sObject ---------------- sleep run setName getName : Calling getSharedInt Synchronized getSharedInt running sharedcell 1 if (Writeable=true) -> watting to become T sleep P=2784 writeablel false thread ( p) wake-up ( sharedInt=1)(sharedcell=1) Put ( Writeable=false ) Notify thread (c) become ready Thread (p) for count=1 to 10 { sleeping random time and each time and setShsredInt=count } Thread (c) do { sleeping random time and each time and value=getShsredInt } While (value!=10) sum

Synchronized getSharedInt running reference T sleep P : 2784 and T sleep C : 388 Producer setting sharedInt to 1 Consumer retrieving sharedInt value 1 sObject reference reference sharedInt=-1 Writeable=true ---------------- setShsreInt getShraeInt p “Producer” sObject ---------------- sleep run setName getName : c Thread ( c ) running “Consumer” sObject ---------------- sleep run setName getName : Calling getSharedInt Synchronized getSharedInt running sharedcell 1 Writeable=true value = sharedObject.getSharedInt() sum += value writeablel true Thread (p) for count=1 to 10 { sleeping random time and each time and setShsredInt=count } Thread (c) do { sleeping random time and each time and value=getShsredInt } While (value!=10) sum 1

( sharedInt=2)(sharedcell=2) Put ( Writeable=false ) reference T sleep P : 2784 and T sleep C : 388 Producer setting sharedInt to 1 Consumer retrieving sharedInt value 1 T sleep C : 792 T sleep P : 96 and Producer setting sharedInt to 2 sObject reference reference sharedInt=-1 Writeable=true ---------------- setShsreInt getShraeInt p “Producer” sObject ---------------- sleep run setName getName : c Two threads begin to run again and going to sleep deferent times 96 and 792 “Consumer” sObject ---------------- sleep run setName getName : thread ( p) wake-up ( sharedInt=2)(sharedcell=2) Put ( Writeable=false ) Notify thread (c) become ready sharedcell 2 writeablel false Thread (p) for count=1 to 10 { sleeping random time and each time and setShsredInt=count } Thread (c) do { sleeping random time and each time and value=getShsredInt } While (value!=10) sum 1

T sleep P : 2784 and T sleep C : 388 Producer setting sharedInt to 1 Consumer retrieving sharedInt value 1 T sleep C : 792 T sleep P : 96 and Producer setting sharedInt to 2 T sleep P : 559 and Consumer retrieving sharedInt value 2 T sleep C : 1122 Producer setting sharedInt to 3 T sleep P : 192 and Consumer retrieving sharedInt value 3 Producer setting sharedInt to 4 T sleep P : 1545 and T sleep C : 2224 Consumer retrieving sharedInt value 4 Producer setting sharedInt to 5 T sleep P : 2122 and T sleep C : 1349 Consumer retrieving sharedInt value 5 T sleep C : 2886 Producer setting sharedInt to 6 T sleep P : 1869 and Consumer retrieving sharedInt value 6 Producer setting sharedInt to 7 T sleep P : 185 and T sleep C : 665 Consumer retrieving sharedInt value 7 T sleep C : 375 Producer setting sharedInt to 8 T sleep P : 2157 and Consumer retrieving sharedInt value 8 T sleep C : 1061 Producer setting sharedInt to 9 Consumer retrieving sharedInt value 9 T sleep C : 2731 T sleep P : 1365 and Producer setting sharedInt to 10 Producer finished producing values Terminating Producer Consumer retrieving sharedInt value 10 Consumer retrieved values totaling: 55 Terminating Consumer