Reputation: 37
I am newbie in Java , trying to learn Java concepts with Implementation. The reason for ReentrantLock class here is to understand Locks.
I am spawning 3 threads , and in these I just increment a Global Counter. I am protecting the Counter overwrites by other thread using Locks.
import java.util.concurrent.locks.ReentrantLock;
class ReentryHandledSingleThread extends Thread
{
static long counter = 0;
private int myId;
private final ReentrantLock myLock = new ReentrantLock();
public ReentryHandledSingleThread(int id)
{
this.myId = id;
}
public void incrementTheCounter()
{
long stackvariable;
int i;
for (i = 0; i < 10000; i++)
{
stackvariable = ReentryHandledSingleThread.counter;
stackvariable = stackvariable + 1;
ReentryHandledSingleThread.counter = stackvariable;
}
System.out.println("The value from counter is " + ReentryHandledSingleThread.counter);
return;
}
public void run()
{
System.out.println("Started Thread No. " + this.myId);
this.myLock.lock();
{
System.out.println("LOCKED Thread No. " + this.myId);
this.incrementTheCounter();
}
System.out.println("UNLOCKED Thread No." + this.myId);
this.myLock.unlock();
}
}
public class RentryHandle
{
public static void main(String[] args)
{
System.out.println("Started Executing Main Thread");
int noOfThreads = 3;
ReentryHandledSingleThread threads[] = new ReentryHandledSingleThread[noOfThreads];
for (int j = 0; j < noOfThreads; j++)
{
threads[j] = new ReentryHandledSingleThread(j);
}
for (int j = 0; j < noOfThreads; j++)
{
threads[j].start();
}
for (int j = 0; j < noOfThreads; j++)
{
try
{
threads[j].join();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Finished Executing Main thrread");
}
}
Observed Output from above code
Started Executing Main Thread
Started Thread No. 0
LOCKED Thread No. 0
Started Thread No. 2
LOCKED Thread No. 2
The value from counter is 10226
UNLOCKED Thread No.0
The value from counter is 16165
UNLOCKED Thread No.2
Started Thread No. 1
LOCKED Thread No. 1
The value from counter is 26165
UNLOCKED Thread No.1
Finished Executing Main thrread
My Expected Output
Started Executing Main Thread
Started Thread No. 0
LOCKED Thread No. 0
The value from counter is 10000
UNLOCKED Thread No.0
Started Thread No. 1
LOCKED Thread No. 1
The value from counter is 20000
UNLOCKED Thread No.1
Started Thread No. 2
LOCKED Thread No. 2
The value from counter is 30000
UNLOCKED Thread No.2
Finished Executing Main thrread
I went through reentrantlock-lock-doesnt-block-other-threads However ,I am NOTusing here
Condition.await()
Thus I was not able to co-relate with my implementation. Please help me to understand the mistake or Understand ReentrantLock application in my implementation which is causing the diference in Exepected Output and observered output.
Upvotes: 1
Views: 162
Reputation: 500167
The issue is that every thread object has its own lock (and is able to lock it independently of what all the other threads are doing):
private final ReentrantLock myLock = new ReentrantLock();
If you want the lock shared across the threads, make the above object static
.
Upvotes: 1