Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. package bakery.algorithm;
  2.  
  3.  
  4. /**
  5. * Overview:
  6. * All n threads (n meaning any number you like)
  7. * will try to access the sharedResource. Each thread will get given
  8. * an id when created. (Check Main)
  9. *
  10. * @author Jairus
  11. */
  12. public class TheThread extends Thread{
  13.  
  14. private int threadID;
  15. public static int sharedResource;
  16.  
  17. // Global variables
  18. public static volatile int[] priorityNumber = new int[5]; // Priority Number for each thread.
  19. public static volatile boolean[] choosing = new boolean[5]; // Keeps true/false values of threads wanting to aquire/release lock
  20. // Why 5? Because there are 5 threads. Each thread will have their corresponding index. That is why they have thread ids
  21. // Their thread ids will correspond to an index within these two arrays
  22.  
  23. public TheThread(int id){
  24. this.threadID = id;
  25. sharedResource = 0;
  26. }
  27.  
  28. @Override
  29. public void run(){
  30. // do something 1000 times
  31. for(int i = 0; i < 300; i++){
  32. // Tries to aquire the lock before getting into
  33. // the critical section. (Before being able to tamper with
  34. // the shared resource)
  35. aquireLock(threadID);
  36.  
  37. /* CRITIAL SECTION START */
  38. sharedResource++;
  39. System.out.println("Thread: " + this.threadID + " | Count: " + sharedResource);
  40.  
  41. // Pauses this thread for given amount of time in order to
  42. // cause a race condition amongst the other threads
  43. try{
  44. Thread.sleep(100);
  45. } catch(InterruptedException e){
  46. // sleeping error
  47. }
  48. /* CRITICAL SECTION END */
  49.  
  50. releaseLock(); // Critical Section is passed, now you can release the lock for other threads
  51. }
  52. }
  53.  
  54. public void aquireLock(int id){
  55. choosing[id] = true; // Flag for the thread : Does the thread want the lock?
  56. priorityNumber[id] = generatePriority();
  57. choosing[id] = false; // Flag has been set to true : Reset Flag
  58.  
  59. System.out.println("Thread: " + id + " Has gained the Lock");
  60.  
  61. // NOTE: I'm still unsure of the absolute purpose of this loop.
  62. for(int i = 0; i < 5; i++){
  63. // Checks if the given thread id matches the loop index
  64. if(i == id)
  65. continue; // Continue just means jump out of the if Statement
  66.  
  67. // I don't know what the point of this is
  68. // Infinite Loop while the current thread in the loop is choosing?
  69. // Maybe it waits for the thread to get past the critical section?
  70. while(choosing[i]){}
  71.  
  72. // Just keep reading the rules lol
  73. while (priorityNumber[i] != 0 && (priorityNumber[id] > priorityNumber[i] ||
  74. (priorityNumber[id] == priorityNumber[i] && id > i))) { /* nothing */ }
  75. }
  76. }
  77.  
  78. /**
  79. * Generates a priority number for the thread when they aquire the lock.
  80. * The priority number is also known as the "Counter" in the algorithm.
  81. *
  82. * Loops through all the current priority numbers and returns the highest + 1
  83. */
  84. public int generatePriority(){
  85. int n = priorityNumber[0];
  86.  
  87. for(int i = 0; i < priorityNumber.length; i++){
  88. if(priorityNumber[i] > n){
  89. n = priorityNumber[i];
  90. }
  91. }
  92.  
  93. return n+1;
  94. }
  95.  
  96. public void releaseLock(){
  97.  
  98. }
  99.  
  100. public int getThreadID() {
  101. return threadID;
  102. }
  103.  
  104. public void setThreadID(int threadID) {
  105. this.threadID = threadID;
  106. }
  107.  
  108. public int getSharedResource() {
  109. return sharedResource;
  110. }
  111.  
  112. public void setSharedResource(int sharedResource) {
  113. this.sharedResource = sharedResource;
  114. }
  115.  
  116. public static void main(String[] args) {
  117.  
  118. // Initializes the global arrays to their default values
  119. for(int i = 0; i < 5; i++){
  120. choosing[i] = false;
  121. priorityNumber[i] = 0;
  122. }
  123.  
  124. // Array of 5 threads
  125. TheThread[] threadArray = new TheThread[5];
  126.  
  127. // Initializes each thread and gives them an ID + start
  128. for(int i = 0; i < 5; i++){
  129. threadArray[i] = new TheThread(i); // Where i is the threadID
  130. threadArray[i].start();
  131. }
  132.  
  133. // Joins for each thread
  134. for(int i = 0; i < 5; i++){
  135. try{
  136. threadArray[i].join();
  137. } catch(InterruptedException e){
  138. // error
  139. }
  140. }
  141.  
  142. System.out.println("Shared Resource Final Value: " + sharedResource);
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement