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.
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.
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.
SharedResource Class:
SharedResource
is a class representing a shared resource, in this case, an integersharedValue
which multiple threads will increment.ReentrantLock
namedlock
to ensure thread safety during the increment operation.increment()
method increments the shared value by acquiring the lock, incrementing the value, and then releasing the lock.getSharedValue()
method returns the current shared value.WorkerThread Class:
WorkerThread
is a class that extendsThread
and represents a worker thread.SharedResource
instance, which it will use to increment the shared value.run()
method is overridden, and it increments the shared value by invoking theincrement()
method on the shared resource 1000 times.ConcurrentExample Class:
ConcurrentExample
is the main class that creates instances ofSharedResource
andWorkerThread
.WorkerThread
instances are created, both sharing the sameSharedResource
.start()
.main()
method waits for both threads to complete usingjoin()
.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.