dig090

Thread Resource Example

May 10th, 2021 (edited)
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package com.j256.resource;
  2.  
  3. /**
  4.  * Little example of thread resource contention.
  5.  */
  6. public class Person implements Runnable {
  7.     private final int id;
  8.     private final String resourceName;
  9.     private final Resource resource;
  10.  
  11.     long enjoyCount;
  12.  
  13.     public static void main(String[] args) {
  14.         Resource resource = new Resource();
  15.  
  16.         Thread t1 = new Thread(new Person(1, "paper", resource));
  17.         Thread t2 = new Thread(new Person(2, "toy", resource));
  18.         Thread t3 = new Thread(new Person(3, "food", resource));
  19.  
  20.         t1.start();
  21.         t2.start();
  22.         t3.start();
  23.     }
  24.  
  25.     public Person(int id, String resourceName, Resource resource) {
  26.         this.id = id;
  27.         this.resourceName = resourceName;
  28.         this.resource = resource;
  29.     }
  30.  
  31.     public void enjoys() {
  32.         try {
  33.             Thread.sleep(1);
  34.             enjoyCount++;
  35.             if (enjoyCount % 1000 == 0) {
  36.                 System.out.println("Person " + id + " enjoyed " + enjoyCount);
  37.             }
  38.         } catch (InterruptedException e) {
  39.             Thread.currentThread().interrupt();
  40.             return;
  41.         }
  42.     }
  43.  
  44.     public void run() {
  45.         while (true) {
  46.             try {
  47.                 // this sleep makes it a little bit far otherwise the thread that called notify tends to reacquire
  48.                 // the lock immediately
  49.                 Thread.sleep(1);
  50.             } catch (InterruptedException e) {
  51.                 Thread.currentThread().interrupt();
  52.                 return;
  53.             }
  54.             resource.takesResource(this);
  55.             enjoys();
  56.             resource.signal(this);
  57.         }
  58.     }
  59.  
  60.     public static class Resource {
  61.         boolean paper;
  62.         boolean toy;
  63.         boolean food;
  64.  
  65.         public synchronized void takesResource(Person person) {
  66.  
  67.             if ("paper".equals(person.resourceName)) {
  68.                 while (toy || food) {
  69.                     try {
  70.                         wait();
  71.                     } catch (InterruptedException e) {
  72.                         Thread.currentThread().interrupt();
  73.                         return;
  74.                     }
  75.                 }
  76.                 toy = true;
  77.                 food = true;
  78.             } else if ("toy".equals(person.resourceName)) {
  79.                 while (food || paper) {
  80.                     try {
  81.                         wait();
  82.                     } catch (InterruptedException e) {
  83.                         Thread.currentThread().interrupt();
  84.                         return;
  85.                     }
  86.                 }
  87.                 food = true;
  88.                 paper = true;
  89.             } else if ("food".equals(person.resourceName)) {
  90.                 while (paper || toy) {
  91.                     try {
  92.                         wait();
  93.                     } catch (InterruptedException e) {
  94.                         Thread.currentThread().interrupt();
  95.                         return;
  96.                     }
  97.                 }
  98.                 paper = true;
  99.                 toy = true;
  100.             } else {
  101.                 System.exit(1);
  102.             }
  103.         }
  104.  
  105.         public synchronized void signal(Person person) {
  106.             if ("paper".equals(person.resourceName)) {
  107.                 toy = false;
  108.                 food = false;
  109.             } else if ("toy".equals(person.resourceName)) {
  110.                 food = false;
  111.                 paper = false;
  112.             } else if ("food".equals(person.resourceName)) {
  113.                 paper = false;
  114.                 toy = false;
  115.             } else {
  116.                 System.exit(1);
  117.             }
  118.             notifyAll();
  119.         }
  120.     }
  121. }
  122.  
Add Comment
Please, Sign In to add comment