I am trying to learn concurrency in Java and not getting the desired results, why arent my locks working? I have tried every tutorial I can find and it is getting me closer, but not 100%
import java.util.concurrent.locks.*;
public class Concurrent extends Thread {
private Object lock1;// = new Object();
// static means the variable is shared by all objects, i.e. global
static long counter = 0;
static long threads = 10;
static long count_each = 1000000;
//private Object lock1 = new Object();
private Object lock2 = new Object();
ReentrantLock lock = new ReentrantLock();
public synchronized void run() //Helps ++
//public void run()
{
//synchronized (lock1)
//{
//lock.lock();
//Object lock1 = new Object();
long count = count_each; // local variable for this thread only
while (count-- > 0)
{
//lock.lock();
//try
//{
Object lock1 = new Object();
synchronized (lock1) //Helps ++
{
counter++;
}
//}
//finally
//{
// lock.unlock();
//}
}
//lock.unlock();
//}
}
Previous attempts are commented out, I have tried just about every combination of everythnig you see.