Stanley Walker

Ranch Hand
+ Follow
since Sep 23, 2009
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stanley Walker


other than implementing observer pattern, is there any other way to pass a message between a contained object to a container object?

Essentially, lets say i have a Computer class which contains a single SoundCard class. Essentially Computer has-a Soundcard.

now i Computer object can call methods on SoundCard objects, by passing a parameter -- essentially pass a message to the soundcard object.

but how does the other way happen? I dont want Soundcard to hold a reference to the computer object.

so how can soundcard pass a message to the Computer object.

one way i can think of is observer pattern. is there any other way?
6 months ago
sample output of the above code is

i am in doing smthing dont distubr 2018-06-25T21:51:49.623
i am in doing smthing dont distubr 2018-06-25T21:51:54.626
i am in doing smthing dont distubr 2018-06-25T21:51:59.628
i am in doing smthing dont distubr 2018-06-25T21:52:04.630
----clear waiting time...i am able to pause and resume-------
i am in doing smthing dont distubr 2018-06-25T21:52:29.558
i am in doing smthing dont distubr 2018-06-25T21:52:34.558
i am in doing smthing dont distubr 2018-06-25T21:52:39.559

I want to implement a service, where some work happens periodically in its own thread.
However outside client should be able to pause the thread for a while, and then resume it

I have been able to implement the same using ScheduledExcutorService and semaphore. however i am not satisfied with the implementation. As you can see in the code below, it has become necessar for the client to pass a semaphore instance to the running task.

is there a better way to implement the same

hi Junilu

thank you so much for your responses.
this is realy helpful

6 months ago

Piet Souris wrote:hi Stanley,

as said: it is not a question of whether a Function works and a Consumer doesn't, but the question what is a legal Function and what is a legal Consumer.

I must say: your code, although nothing wrong with it, is not very transparant. To make the error a little easier to spot, let's make a separate Function and a separate Consumer. If you get an error in one of these, then you know that the problem is not in the 's.changeState(2)' part of the code,

So here goes:

So, when we apply this function to some StateCount c, then the return value is c.count + 1.
On its own, c.count + 1 means nothing, you get a compiler error here, stating that this value should be assigned to a variable. Now have a look at the method 'stateChanged':

You invoke it with:

So, in effect, what you do is saying:

and as you can see, this is perfectly legal code.

Now for the Consumer part. Again, let's check if the Consumer as you defined it is correct:

As you will notice, this gives a compiler error, stating that an int (the result of the right part) cannot be converted to void, as a Consumer must be.
So, let's try a variant:

Now we return nothing, but as before, we get the error that the right hand part is not a statement and should be assigned to a variable.
Well, let's do that, but the way I do it makes it necessary to make the variable 'count' available to other classes as well: make it a public variable (that is bad practice, never do that in your code, but it is just for demonstrations sake)

And that is perfectly legal as well.

So, with the Function sToi and Consumer consumer defined (and legal), we can invoke them, either as you invoke them,but also as follows:

As said, having the separate sToi and consumer makes it just a tad easier to see what is going on.

Looking back, I must admit that this is actually not an easy subject at all! It is a subject that takes a lot of practising, and a lot of trying until you get it right! But the reward of putting all that effort is there: it is big fun working with all of these Consumers, Functions, Predicates and similar. Of course, if you have additional questions, just come up with them.

THank you Piet, i totally appreciate the pain you took to explain the problem to me. And i must say, i do understand now why one works and the other doesnt.

Appreciate your help
6 months ago

Junilu Lacar wrote:Additionally, your usage of the Function and Consumer interfaces are smelly. I can't quite put my finger on it but it just seems like there's way too much unnecessary intimacy between the StateCount class and the lambdas. The lambdas having a reference to StateCount does not look right to me. And Consumer seems like the opposite of what you should use. That is, it seems to me a Producer Supplier is more appropriate for what you want to do. I'll try to come up with an example to show something I think is more sensible.

Hi Junilu,

I promise you, i was just trying out different ways to fail. to be honest, thats the only way i have learnt lambdas as much as  i have(which is not too much, i must confess)
i keep trying out weird stuff with functions api and this particular piece confused me, since one works and the other doesnt. and compiler errors are not that descriptive either

Can i ask you a question - why do you say  

Junilu Lacar wrote:between the StateCount class and the lambdas

Is it because the lambdas passed in is changing the state of the class??
6 months ago

Piet Souris wrote:hi Stanley,

it is the expresson 'c -> s.getCount() += 1' that is invalid. In fact, it boils down to: s.getCount() = s.getCount() + 1, which is invalid.

If you add the method

and make the Consumer:

then all is well.

Hi Piet,

Thank you for the response.

I understand  s.getCount() = s.getCount() + 1 is an invalid statement

But why does it work when i use a Function

check here

the above code works. How come consumer it doesnt work

like below

6 months ago
essentially, i am not able to figure out why FUnction works and consumer does not
6 months ago

I want to create a simple class which has a single instance variable int count.
I want to be able to add/decrease the value of count. But i dont want to have methods increment/decrement. i want a single method which does the same by taking a lambda

if you see the below code, when i use a FUnction it works.
however if i use a consumer , i am not able to do it using a consumer.

i get this error. The left-hand side of an assignment must be a variable and The method changeState1(Function<StateCount,Integer>) is ambiguous for the type StateCount

6 months ago
java.util.function.Predicate has a method isEquals with the following signature:

static <T> Predicate<T> isEqual(Object targetRef)

i do not understand the signature though. THe method returns Predicate<T> . i get that , however what is <T> Predicate<T> . why the additional <T> in the front?
9 months ago
Really looking forward to any help with the above question
I have been playing around FutureTask recently. I wanted to fetch data from two tables in a parallel execution. Currently my solution is mocked, where it just fetches some hardcoded data.

1. I created futuretask1 which fetches data from 1st table
2. I created futuretask2 which fetches data from 2nd table
3. I submitted the task to executor services for execution. Things are working fine.
4. However my problem is if I submit any one of the future task twice, i do not see execution happening twice. Clearly java is not executing a task which it has already executed. However I could not find anything alluding to it in the java docs for ExecutorService or Executor. I had also opened up the java api, and I could not figure out how it detects uniqueness.

Clearly I am missing out something here, any help would be appreciated.
My code snippets are given below:

The classes DataFromFirstTable and DataFromSecondTable are simple POJO's

The output that i get on running the above code is:

MockedFetchFromTable.parallelFetchFromTwoTable() second result----->nameId#first name#secondName
MockedFetchFromTable.parallelFetchFromTwoTable() result----->AUD#10

The output tells me that even though I have submitted my futuretask2 twice, the line MockedFetchFromTable.loadDataFromSecondTable() is only getting printed once.

Please help.

Also one more question, if i do not shutdown my executorservice manually , i can see that my program waits for close to a minute even after all execution is complete. Is this normal behavior? or have i done something wrong?

This is a question one of my juniors asked me and I was completely dumbstruck.

according to java docs for Hibernate :

for Session

It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.

for SEssionFactory.getCurrentSEssion()

Obtains the current session. The definition of what exactly "current" means controlled by the CurrentSessionContext impl configured for use.

So if session object is not threadsafe, what happens when two threads call sessionFactory.getCurrentSession() .... if they get the same session object will there not be chances of all the dangers that threads cause when they get the same instance?

java api of CurrentSessionContext did not help and when i browsed through the code for implementors of CurrentSessionContext, it seemed to me that it is returning the same session object.

Please help, this has been bugging me for some time now.
Thank you steve for all your help and especially for the link.
I will try and write down the checking conditions
Thank you Steve for the response. I do have couple of questions though.

This check only happens once, when the run() method starts, and the isFull() method is not safe so it can not be relied on. This would only work as a check if the stack were full before the producer was started.

I totally agree with you that the other methods should have been synchronized and i also understand why my check is incorrect. Please help with a condition/check which will underline that the producer consumer problem is not solved.

Not sure if defining and returning the element outside the synchronized block is the right thing to do. It doesn't save you anything, and sets you up to use the same paradigm later when it will cause a problem. I would avoid it

agreed, that way my bad.

If an interruption occurs, then you can progress through the method while the list is past capacity. You should change the if(isFull()) to while(isFull()) so, even if interrupted, the method won't progress.

That is a very good point and i wanted to confirm that you meant this

Whenever possible, for multi-threaded state, make things final. In this case, both list and maxSize should be final

Do you mean that if i want to write thread safe classes i should make all my instance variables as final?