This week's book giveaway is in the Security forum.
We're giving away four copies of Securing DevOps and have Julien Vehent on-line!
See this thread for details.
Win a copy of Securing DevOps this week in the Security forum!

Stanley Walker

Ranch Hand
+ Follow
since Sep 23, 2009
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
6
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stanley Walker

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?
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.loadDataFromFirstTable()
MockedFetchFromTable.loadDataFromSecondTable()
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?

HI,
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?
Hi Steve,

I was running the method from a junit test case.
When i ran it from a main method, the method actually continues.
Thank you.
HI,


I had some time in my hand and went about trying to solve producer consume problem without taking help from the internet. I thought i was going good, however i cannot understand why the program terminates suddenly.
So i am sure i have done something wrong or i havent checked if the producer is actually waiting when the stack is full or the consumer is waiting when the stack is empty.

Please help, i am really clueless on this. I am giving my code here.

My Stack class - the data structure that I am using, i have added logic to wait in this class.




My consumer class:



My producer class : the condition that i have given for checking is- if in any condition , the stack returns that the stack is full then the producer throws an exception stating that the stack is not waiting for consumption. since i have allowed my production/consumption to run in a infinite loop, my thought process is that it will never be the case.



My test class





the output of the above code is given below. I am not sure why it always terminates after reaching some point. the point where it terminates is not same.
sample output:

true
My Element in stack 90
My Element in stack 91
i am consuming the element-->My Element in stack 90 from the produced stack
i am consuming the element-->My Element in stack 91 from the produced stack
My Element in stack 92
My Element in stack 93
My Element in stack 94
My Element in stack 95
i am consuming the element-->My Element in stack 92 from the produced stack
i am consuming the element-->My Element in stack 93 from the produced stack
i am consuming the element-->My Element in stack 94 from the produced stack
i am consuming the element-->My Element in stack 95 from the produced stack
My Element in stack 96
My Element in stack 97
My Element in stack 98
My Element in stack 99
i am consuming the element-->My Element in stack 96 from the produced stack
My Element in stack 100
i am consuming the element-->My Element in stack 97 from the produced stack
My Element in stack 101
i am consuming the element-->My Element in stack 98 from the produced stack
My Element in stack 102
i am consuming the element-->My Element in stack 99 from the produced stack
My Element in stack 103
i am consuming the element-->My Element in stack 100 from the produced stack
My Element in stack 104
i am consuming the element-->My Element in stack 101 from the produced stack
My Element in stack 105
i am consuming the element-->My Element in stack 102 from the produced stack
My Element in stack 106
i am consuming the element-->My Element in stack 103 from the produced stack
My Element in stack 107
i am consuming the element-->My Element in stack 104 from the produced stack
My Element in stack 108
i am consuming the element-->My Element in stack 105 from the produced stack
My Element in stack 109
i am consuming the element-->My Element in stack 106 from the produced stack
My Element in stack 110
My Element in stack 111
i am consuming the element-->My Element in stack 107 from the produced stack
i am consuming the element-->My Element in stack 108 from the produced stack
i am consuming the element-->My Element in stack 109 from the produced stack
i am consuming the element-->My Element in stack 110 from the produced stack
My Element in stack 112
My Element in stack 113
i am consuming the element-->My Element in stack 111 from the produced stack
i am consuming the element-->My Element in stack 112 from the produced stack
My Element in stack 114
i am consuming the element-->My Element in stack 113 from the produced stack
My Element in stack 115
i am consuming the element-->My Element in stack 114 from the produced stack
My Element in stack 116
i am consuming the element-->My Element in stack 115 from the produced stack
My Element in stack 117
i am consuming the element-->My Element in stack 116 from the produced stack
i am consuming the element-->My Element in stack 117 from the produced stack
My Element in stack 118
My Element in stack 119
i am consuming the element-->My Element in stack 118 from the produced stack
My Element in stack 120
i am consuming the element-->My Element in stack 119 from the produced stack
i am consuming the element-->My Element in stack 120 from the produced stack
My Element in stack 121
My Element in stack 122
My Element in stack 123





It just isn't possible for Java to interrupt arbitrary code and have it return from a method, abnormally or otherwise.


I was thinking the same way, but numerous google searches on the internet on the same topic made me think that it might be possible. Thank you for the clarification, Paul
thank you paul.

lets suppose i tweak the requirement a bit. lets say my run() method needs to call repaint() only once. in such a case, how do i stop a thread from completing the repaint() method

eg given below

HI Everyone,

According to this article http://docs.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html it says to perform the following as a mechanism for stopping a thread of execution




how is calling stop() method on this ensuring that the currently running thread of execution stops? what i mean is lets say repaint()method is already invoked. while repaint() method is executing , if i call sstop() according to me nothing will happen. repaint method will still complete execution. the above code ensures that the next time when this thread is picked up for execution, it will check for the stop flag.

AM i wrong in my understanding? please help
Hi Everyone,

I was just wondering if it is possible to separate home/component interfaces for EJB.

Up until now, when ever I was writing an EJB i was always maintaining my bean class, home and component interface in the same project. I used to build it and deploy it as a jar file along with the deployment descriptors.
Since my client only requires references to home and component interfaces, I was just wondering if i can separate the two.
eg:
Bean class ABC in project com.org.bean.impl
and interfaces ABCEJBObject and ABCEJBHome in project com.org.bean.interface

In which case, i may only deploy com.org.bean.interface in the client environment.

question: in the case the above is possible, how do i deploy the two projects in my server?

Any help would be appreciated

thank you

Marco Ehrentreich wrote:In contrast for consumers acknowledge mode (or transaction handling) is important because it signals the message broker that the client has consumed a message correctly so that the broker can delete it from the queue.

Marco



now makes perfect sense
Thank you so much Marco.
So i guess unless the same session is being used to post and consume the message, while posting messages to a queue acknowledgement mode is simply ignored even though API requires us to set it.
Hi everyone,

for the last two days i have been reading quite a bit of acknowledgement mode for JMS. I understood that is required for a message to be acknowledged for its consumption to be completed.

however i do not understand what is the use of setting acknowledgement mode when we are simply posting a message to a queue.

Can someone please help?