Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. // Author: Matheus Eduardo Garbelini
  2. // Code: Philosophers dinner
  3.  
  4. import java.util.Random;
  5.  
  6. public class Deadlock {
  7.  
  8.     static CrazyThread[] Threads; // Stores the thread classes
  9.     static Object[] Locks; // Stores the locks
  10.     static String[] LocksFlags;
  11.  
  12.  
  13.     public static void main(String[] args) {
  14.  
  15.         // Initialize n threads, where n is the number of cores on current machine
  16.         int ncores = Runtime.getRuntime().availableProcessors();
  17.         System.out.println("Number of available cores: " + ncores);
  18.  
  19.         Locks = new Object[ncores];
  20.         Threads = new CrazyThread[ncores];
  21.         LocksFlags = new String[ncores];
  22.  
  23.         // Initialize threads
  24.         for (int i = 0; i < ncores; i++) {
  25.             LocksFlags[i] = "_";
  26.             Locks[i] = new Object();
  27.             Threads[i] = new CrazyThread();
  28.             Threads[i].ncore = i;
  29.             Threads[i].maxcores = ncores;
  30.             System.out.println("Core number: " + i);
  31.         }
  32.         // Start threads
  33.         for (int i = 0; i < ncores; i++) {
  34.             Threads[i].start();
  35.         }
  36.  
  37.     }
  38.  
  39.  
  40.     public static class CrazyThread extends Thread {
  41.         public int ncore;
  42.         public int maxcores;
  43.         public Random random = new Random(); // Each seed is different at class allocation (nanoTime used)
  44.  
  45.         public void run() {
  46.  
  47.             while (true) {
  48.                 int start_delay = random.nextInt(10);
  49.                 int busy_delay = random.nextInt(100);
  50.                 threadSleep(start_delay);
  51.  
  52.                 synchronized (Locks[ncore]) {
  53.                     LocksFlags[ncore] = String.valueOf(ncore); // Set flag for first lock
  54.                     printThreads(); // print locks status
  55.                     int next_lock = (ncore + 1) % maxcores;
  56.                     synchronized (Locks[next_lock]) {
  57.                         LocksFlags[next_lock] = String.valueOf(ncore); // Set flag for second lock
  58.                         printThreads(); // print locks status
  59.                         threadSleep(busy_delay);
  60.                     }
  61.                     LocksFlags[next_lock] = "_";
  62.                 }
  63.                 LocksFlags[ncore] = "_";
  64.                 printThreads(); // Print freed locks
  65.  
  66.             }
  67.  
  68.         }
  69.  
  70.         void threadSleep(int ms) {
  71.             try { // Yes, java requires to use an exception even to sleep a thread, oh my god!!!
  72.                 Thread.sleep(ms);
  73.             } catch (Exception e) {
  74.             }
  75.         }
  76.  
  77.         void printThreads() {
  78.             String threadsText = "";
  79.             for (int i = 0; i < maxcores; i++) {
  80.                 if (i == 0)
  81.                     threadsText += "["; // first lock flag
  82.  
  83.                 threadsText += LocksFlags[i];
  84.  
  85.                 if (i == maxcores - 1)
  86.                     threadsText += "]";
  87.                 else
  88.                     threadsText += ",";
  89.             }
  90.  
  91.             System.out.println(threadsText);
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement