#Need help with Java Concurrency

16 messages · Page 1 of 1 (latest)

gray talonBOT
#

Hey, @urban pulsar!
Please remember to /close this post once your question has been answered!

hasty cliff
#

currid = 1 should be static i guess

#

also u r just increasing it then how thread 1 will get chance again

urban pulsar
hasty cliff
#

after Thread 3: 15 currid will be 4 right ?

#

then there will be no if (currid == threadId)

#

so all thread will be in wait()

urban pulsar
#
  if (currid == threadId) {
                currid = threadId;
                
                currid++;
                if (currid == 4) {
                    currid = 1;
                }
......
#

doesnt' help me

hasty cliff
#

two thing u have to do
1: make currid static
2: before comparing if (currid == threadId) u have to check if it's 4 then make it 1 before if (currid == threadId)

urban pulsar
#

Maybe whole method is wrong

hasty cliff
urban pulsar
#

thanks

#
class Solution implements Runnable {

    private int threadId;
    private Object o;


    public Solution(int threadId, Object o) {
        this.threadId = threadId;
        this.o = o;
    }

    private static volatile int integer = 1;
    static volatile int currid = 1;

    @Override
    public void run() {
        while (integer < 28) {
            synchronized (o) {
                if(currid==4){
                    currid=1;
                }
                if (currid == threadId) {
                    currid++;
                    for (int i = 0; i < 5; i++) {
                        System.out.println(threadId + "   " + integer++);
                        if (integer == 29) {
                            break;
                        }
                    }
                    o.notifyAll();
                } else {
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        }
    }
}