Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bakery.algorithm;
- /**
- * Overview:
- * All n threads (n meaning any number you like)
- * will try to access the sharedResource. Each thread will get given
- * an id when created. (Check Main)
- *
- * @author Jairus
- */
- public class TheThread extends Thread{
- private int threadID;
- public static int sharedResource;
- // Global variables
- public static volatile int[] priorityNumber = new int[5]; // Priority Number for each thread.
- public static volatile boolean[] choosing = new boolean[5]; // Keeps true/false values of threads wanting to aquire/release lock
- // Why 5? Because there are 5 threads. Each thread will have their corresponding index. That is why they have thread ids
- // Their thread ids will correspond to an index within these two arrays
- public TheThread(int id){
- this.threadID = id;
- sharedResource = 0;
- }
- @Override
- public void run(){
- // do something 1000 times
- for(int i = 0; i < 300; i++){
- // Tries to aquire the lock before getting into
- // the critical section. (Before being able to tamper with
- // the shared resource)
- aquireLock(threadID);
- /* CRITIAL SECTION START */
- sharedResource++;
- System.out.println("Thread: " + this.threadID + " | Count: " + sharedResource);
- // Pauses this thread for given amount of time in order to
- // cause a race condition amongst the other threads
- try{
- Thread.sleep(100);
- } catch(InterruptedException e){
- // sleeping error
- }
- /* CRITICAL SECTION END */
- releaseLock(); // Critical Section is passed, now you can release the lock for other threads
- }
- }
- public void aquireLock(int id){
- choosing[id] = true; // Flag for the thread : Does the thread want the lock?
- priorityNumber[id] = generatePriority();
- choosing[id] = false; // Flag has been set to true : Reset Flag
- System.out.println("Thread: " + id + " Has gained the Lock");
- // NOTE: I'm still unsure of the absolute purpose of this loop.
- for(int i = 0; i < 5; i++){
- // Checks if the given thread id matches the loop index
- if(i == id)
- continue; // Continue just means jump out of the if Statement
- // I don't know what the point of this is
- // Infinite Loop while the current thread in the loop is choosing?
- // Maybe it waits for the thread to get past the critical section?
- while(choosing[i]){}
- // Just keep reading the rules lol
- while (priorityNumber[i] != 0 && (priorityNumber[id] > priorityNumber[i] ||
- (priorityNumber[id] == priorityNumber[i] && id > i))) { /* nothing */ }
- }
- }
- /**
- * Generates a priority number for the thread when they aquire the lock.
- * The priority number is also known as the "Counter" in the algorithm.
- *
- * Loops through all the current priority numbers and returns the highest + 1
- */
- public int generatePriority(){
- int n = priorityNumber[0];
- for(int i = 0; i < priorityNumber.length; i++){
- if(priorityNumber[i] > n){
- n = priorityNumber[i];
- }
- }
- return n+1;
- }
- public void releaseLock(){
- }
- public int getThreadID() {
- return threadID;
- }
- public void setThreadID(int threadID) {
- this.threadID = threadID;
- }
- public int getSharedResource() {
- return sharedResource;
- }
- public void setSharedResource(int sharedResource) {
- this.sharedResource = sharedResource;
- }
- public static void main(String[] args) {
- // Initializes the global arrays to their default values
- for(int i = 0; i < 5; i++){
- choosing[i] = false;
- priorityNumber[i] = 0;
- }
- // Array of 5 threads
- TheThread[] threadArray = new TheThread[5];
- // Initializes each thread and gives them an ID + start
- for(int i = 0; i < 5; i++){
- threadArray[i] = new TheThread(i); // Where i is the threadID
- threadArray[i].start();
- }
- // Joins for each thread
- for(int i = 0; i < 5; i++){
- try{
- threadArray[i].join();
- } catch(InterruptedException e){
- // error
- }
- }
- System.out.println("Shared Resource Final Value: " + sharedResource);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement