anuj guptaa

Greenhorn
+ Follow
since Aug 05, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by anuj guptaa

Thanks for your response. What exactly you mean by Niche...is it that the market for ESB is not that big ...not many takers for ESB ??

Although i did worked on Glassfish ESB and i have fair knowledge on ESB but when i tried to self learn Oracle Service bus , there was hardly any similarity between Glassfish ESB and oracle service bus.

There was no concept of Service Engine and Binding component in OSB. So Learning Glassfish ESB didnt gave me any advantage while learning other ESB products. The only benefit i see was that i understood the general SOA based architecture a little better. And i liked the term - Web service Orchestration used by Glassfish.

Anuj
10 years ago

Salil Vverma wrote:Hey Anuj,

I tried the solution is quad code processor and found it working fine. But definitely the code is prone to dead lock if first signal from main to first thread gets missed. The chances of single getting missed will increase in a single core processor machine.
The problem can be solved using Semaphore. The complete code can be accessed from the below link -

http://jyotisalil.blogspot.in/2013/09/inter-thread-communication-using-wait_19.html#SemaphoreCode



Hey Salil,

The solution using the wait/notify and Seamphore are so similar but wait/notify is prone to deadlock while Seamphore is not , how come ? Any clue. I think its the internal implementation for these 2 that makes the difference.

Anuj

Salil Vverma wrote:Anuj : We can implement the solution with lock and condition but I think, we shall have to add unnecessary complexity to make sure that no signal gets missed in that solution.



Hi Salil,

Thanks for your help. I am not sure what you mean by the Missed signal , i think its on a bigger context than this problem.

I also tried solving the same problem using Lock and condition and it works fine. Its on the same line as you did with the
Intrinsic lock and wait/notify solution. Please see the solution below.

The good thing about the Lock/condition solution is that we just used one Extrinsic lock instead of 3 Locks in the Intrinsic solution.
Also the Inter-Thread communication in the Lock/Condition solution is better and more advance. But this solution is still prone to deadlock on a duo-core processor ,so i had to use the Thread.sleep().



Hi Salil

Very cool , this solution with Semphore work with charm. You really are good with Threads ...i am still trying to wrap my head around all these complex Thread logic.

Is it possible to implement this logic using Locks & Condition ?

Thanks
Anuj
Hi Salil

Thanks for the response. I ran this program but the output is not consistent. I mean sometime it just hangs. But then i added a Sleep after T3.start() and it worked much better. I am on java version 1.6

I was asked a very similar question in Congnizant interview. It was not exactly the same.

Thanks
Anuj
Hi Chan,

Here is the CyclicBarrier code that i wrote ...i have a interview on Saturday and have to prepare on lot of topics like Spring , Hibernate so lost the track... but i will work on the Lock solution ...

Thanks for your suggestions...







Thanks
Anuj
Hi Chan,

Now i understand Lock and condition a bit ...can you give me hint now ..as i tried but couldnt get the output...

So did you do it with 3 Locks and 3 different condition ....or 1 lock and 3 different conditions ??

Thanks
Anuj
Hey Chan,

I got the answer to previous question....I just had to add a sleep in method A() after the lock... so the thread2 tried to enter B() but couldnt as it didnt had the lock...


Now i am getting confident ...now i will try the original problem for which i started this thread.

Thanks for your support Chan ....

Anuj
Hi Chan,

Thanks for pointing...it was a problem with the Calling method ..i had created two Runnable object and passed to the threads..what a mistake.... i corrected it to one Runnable object and 2 threads..

But i still have this question...

I have one lock variable defined as instance variable ReentrantLock lockVariable = new ReentrantLock();

now in Fucntion A() i do lockVariable.lock();

in other function B() in same class i do lockVariable.lock();
Thread1 call A() and Thread2 call B() on same object...

so the lock that i am getting here are 2 different locks or are they same ??

Thanks
Anuj

Chan

Sorry didnt knew abt the java code tags as first time pasting the code here

What i did is -->I defined a ReentrantLock Instance Variable called lock ....and then using this lock i defined the instance variable Condition.

Now i am trying to synchronize two methods testLock1 and testLock2 using lock.lock() in both..... I have two threads one call the testlock1() and other testlock2()......testlock1() goes into wait
and testlock2() increament count and then call signal...

Now i want to know that if lock.lock in both the methods refer to the same LOCK ???

Also why the condition.signal() not working..


I am getting the output as and prog does not stop....

TESTLOCK 1
Count now await:0
TESTLOCK 2
Count now signal:1
Lock2 released



I am confused. whats wrong with the implementaion
-------------------------------------------------------------------------------------


Main Class (Main Method)
Also why the condition.signal() not working..
Hi Chan,

I tried a program with Lock but i am only more confused now...Here is my implementation

package com.anuj.Locks;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class LocksTrial implements Runnable {

int flag;

int count = 0;
ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition();

public LocksTrial(int flag) {
this.flag = flag;
}

@Override
public void run() {

if (flag == 1) {
testLock1();
} else {
testLock2();
}

}

public void testLock1() {

System.out.println("TESTLOCK 1");

lock.lock();

try {
while (count == 0) {
System.out.println("Count now await:" + count);
condition.await();
System.out.println("Count now after await:" + count);

}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
System.out.println("Lock1 released");

}
}

public void testLock2() {

System.out.println("TESTLOCK 2");

lock.lock();

try {
count++;
condition.signal();
System.out.println("Count now signal:" + count);

} finally {

lock.unlock();

System.out.println("Lock2 released");
}
}

}



Caller Main Method

public class TestLocks {

public static void main(String[] args) {
LocksTrial locksTrial1 = new LocksTrial(1);
LocksTrial locksTrial2 = new LocksTrial(2);


new Thread(locksTrial1).start();
new Thread(locksTrial2).start();
}

}

So now my question is that i defined a ReentrantLock Instance Variable lock ....and then using this lock i defined the instance variable Condition.

Now i am trying to synchronize two methods testLock1 and testLock2 using lock.lock() in both..... I have two threads one call the testlock1 and other testlock2......testlock goes into wait
and testlock2 increament count and then call signal...

Now i want to know the lock.lock in both the method refer to same LOCK ???

I am getting the output as and prog does not stop....

TESTLOCK 1
Count now await:0
TESTLOCK 2
Count now signal:1
Lock2 released


I am confused. whats wrong with the implementaion

Thanks
Anuj
Hi Chan,

Can you give me a hint on the implementation. How did you implement it.

I am still learning Lock interface. Till now i didnt see any big difference between ReEntrant lock and Monitor. (Not talking about the ReentrantReadWriteLock which is more flexible and scalable).

Thanks
Anuj
Chan,

ok i missed that you mentioned to use condition variable...are you referring to the condition that we get from a LOCK interface...

If so i will try that ..i am still learning the Lock interface ..

Thanks
Anuj
Chan

I agree with you Barrier cannot be used for managing the sequence of the threads ...so thats why i was wondering what would be the other solution for this problem.

What other datastructure i can use here ? i really cannot think of any solution where i can control the sequence of the thread...

Thanks
Anuj