2

hi guys i was wondering if i can get a little advice im trying to write a program that can counts how many threads are waiting to process a function, and then once a certain number is achieved it releases all the thread. but my problem is i cant increment properly being that i can the all process the increment code at the same time , thus not incrementing it at all.

protected synchronized boolean isOpen()
{
    //this code just calls mymethod intrested where the problem lies

  lock.interested();
    while(!lock.isReady())
    {
    }
    return true;// this statement releases all my threads

 }



public synchronized void  interested()
{

    count++;// how do i make this increment correctly with threads
    System.out.println(count+"-"+ lanes+"-"+ThreadID.get());
    if(count==lanes)
    {

        count =0;
        ready =true;
    }

}
jambuls
  • 111
  • 11
  • 4
    Are you allowed to use AtomicInteger? http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicInteger.html – Jeanne Boyarsky Aug 10 '14 at 18:05
  • Im actually trying to refrain from using pre-programmed stuff, in other words if i code it like caveman i understand exactly how it works from the ground up. – jambuls Aug 10 '14 at 18:14
  • possible duplicate of [Java Concurrency Incrementing a Value](http://stackoverflow.com/questions/8515561/java-concurrency-incrementing-a-value) – DavidPostill Aug 10 '14 at 18:17
  • 1
    @jambuls if you want to implement it yourself (a.k.a. reinvent the wheel) then use AtomicInteger like Jeanne suggested above. – Nir Alfasi Aug 10 '14 at 18:31
  • OK so i replaced the count variable with a atomic integer, it still doesn't seem to be incriminating right. i use the incrementAndGet() method – jambuls Aug 10 '14 at 18:47
  • each thread ended up making its own instance of the class in which my atomic integer was, thus it was giving wrong input, thanks for the help – jambuls Aug 10 '14 at 19:21

3 Answers3

1

The problem with your approach is that only one thread can enter the synchronized method at a time and hence, you will never proceed, as all but the first threads are waiting to enter the synchronized method while the first thread is performing a busy-wait loop. You have to use wait which not only solves the waste of CPU cycles of your busy wait but will also free the associated lock of the synchronized code so that the next thread can proceed:

protected synchronized boolean isOpen()
{
    lock.interested();
    while(!lock.isReady())
    {
        wait(); // now the next thread can enter isOpen()
    }
    notify(); // releases the previous thread wait()ing in this method
    return true;
 }

However, note that this works quite unreliable due to your code being split over multiple different objects. It’s strongly recommend to put the code maintaining the counter and code implementing the waiting for the counter into one object in order to run under the same lock. Your code structure must ensure that interested() can’t be invoked on the lock instance with isOpen not noticing. From the two code fragments you have posted, it’s impossible to deduce whether this is the case.

Holger
  • 285,553
  • 42
  • 434
  • 765
0

write a program that can counts how many threads are waiting to process a function, and then once a certain number is achieved it releases all the threads

A good solution will be to use CountDownLatch.

From the manual:

A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return immediately. This is a one-shot phenomenon -- the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier.

You can find a good code example here

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0
You should not use synchronised. Because only one thread will acquire monitor at a time.

You can use CountDownLatch. Just define the no of threads while initialising CountDownLatch.

private CountDownLatch countDownLatch = new CountDownLatch(no_of_threads);
protected  boolean isOpen()
{
    //this code just calls mymethod intrested where the problem lies

    countDownLatch.countDown();
    countDownLatch.await();
    return true;// this statement releases all my threads
 }


  All the threads are waiting in countDownLatch.await(). Once the required amount of thread comes(countDownLatch.countDown() is called) it will allow to proceed. 
Siva Kumar
  • 1,983
  • 3
  • 14
  • 26