csubhasundar / CodingPractice-Hacktoberfest23

Repository totally dedicated to Hacktober Fest 2023, feel free to use it. Topics hacktoberfest hacktoberfest-accepted hacktoberfest2023
32 stars 293 forks source link

Concurent example in java #217

Closed CheetahCodes21 closed 1 year ago

CheetahCodes21 commented 1 year ago
  1. SharedResource Class:

    class SharedResource {
        private int sharedValue;
        private Lock lock;
    
        public SharedResource() {
            this.sharedValue = 0;
            this.lock = new ReentrantLock();
        }
    
        public void increment() {
            lock.lock(); // Acquire the lock
            try {
                sharedValue++;
            } finally {
                lock.unlock(); // Release the lock
            }
        }
    
        public int getSharedValue() {
            return sharedValue;
        }
    }
    • SharedResource is a class representing a shared resource, in this case, an integer sharedValue which multiple threads will increment.
    • It uses a ReentrantLock named lock to ensure thread safety during the increment operation.
    • The increment() method increments the shared value by acquiring the lock, incrementing the value, and then releasing the lock.
    • The getSharedValue() method returns the current shared value.
  2. WorkerThread Class:

    class WorkerThread extends Thread {
        private SharedResource sharedResource;
    
        public WorkerThread(SharedResource sharedResource) {
            this.sharedResource = sharedResource;
        }
    
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                sharedResource.increment();
            }
        }
    }
    • WorkerThread is a class that extends Thread and represents a worker thread.
    • It has a reference to a SharedResource instance, which it will use to increment the shared value.
    • The run() method is overridden, and it increments the shared value by invoking the increment() method on the shared resource 1000 times.
  3. ConcurrentExample Class:

    public class ConcurrentExample {
        public static void main(String[] args) {
            SharedResource sharedResource = new SharedResource();
    
            // Create multiple worker threads
            WorkerThread thread1 = new WorkerThread(sharedResource);
            WorkerThread thread2 = new WorkerThread(sharedResource);
    
            // Start the threads
            thread1.start();
            thread2.start();
    
            try {
                // Wait for threads to finish
                thread1.join();
                thread2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            // Display the final shared value
            System.out.println("Final shared value: " + sharedResource.getSharedValue());
        }
    }
    • ConcurrentExample is the main class that creates instances of SharedResource and WorkerThread.
    • Two WorkerThread instances are created, both sharing the same SharedResource.
    • The threads are started using start().
    • The main() method waits for both threads to complete using join().
    • Finally, it prints the final shared value by invoking getSharedValue() on the shared resource and displays it to the console.

The overall purpose of this code is to demonstrate safe concurrent incrementing of a shared value using multiple threads, where the SharedResource class ensures thread safety through the use of a lock.