Peter Hsu

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

Recent posts by Peter Hsu

I am trying do debug an application that I am not that familiar with, I am seeing this in our stack trace

java.lang.NullPointerException
com.opensymphony.xwork2.validator.AnnotationValidationConfigurationBuilder.processCustomValidatorAnnotation(AnnotationValidationConfigurationBuilder.java:412)
com.opensymphony.xwork2.validator.AnnotationValidationConfigurationBuilder.processAnnotations(AnnotationValidationConfigurationBuilder.java:122)
com.opensymphony.xwork2.validator.AnnotationValidationConfigurationBuilder.buildAnnotationClassValidatorConfigs(AnnotationValidationConfigurationBuilder.java:888)
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildClassValidatorConfigs(AnnotationActionValidatorManager.java:283)
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildValidatorConfigs(AnnotationActionValidatorManager.java:371)
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildValidatorConfigs(AnnotationActionValidatorManager.java:350)
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:102)
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.validate(AnnotationActionValidatorManager.java:141)
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.validate(AnnotationActionValidatorManager.java:133)
com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.validate(AnnotationActionValidatorManager.java:128)
com.opensymphony.xwork2.validator.ValidationInterceptor.doBeforeInvocation(ValidationInterceptor.java:222)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:546)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
com.mycompany.servlet.filter.JspFilter.doFilter(JspFilter.java:58)
com.mycompany.filter.SecurityFilter.doFilter(SecurityFilter.java:70)
com.mycompany.spnego.http.InitiateAction.run(HttpAuthFilter.java:215)
com.mycompany.spnego.http.HttpAuthFilter.executeFilterWithCreds(HttpAuthFilter.java:176)
com.mycompany.spnego.http.HttpAuthFilter.doFilter(HttpAuthFilter.java:74)
com.mycompany.LogFilter.doFilter(LogFilter.java:39)

Apparently the line that throws exception is the following


I am guessing validatorFactory is null? It look like AnnotationValidationConfigurationBuilder's constructor gets it from AnnotationActionValidatorManager which should get a setter injection of validatorFactory
How are validator factory injected?

Sorry for such clueless post but I am really frustrated...
10 years ago
Does it mean I need to allocate more threads than number of cores? That's it?
(If that's the case it's really unforgivable for me to miss it... I just need to pass higher value to the executor service?)
10 years ago
Recently I have been asked an interview question on how to do a somewhat elaborated multithreaded search. (Due to NDA I can't say too much about it though)
So what I did was divide the tasks into chunks and try and load balance them before throwing them to a executor service.
I was asked to optimize it a little further. He gave me a hint that "when you are reading from a disk, the thread is doing what" I answered "waiting", but then I wasn't able to go any further based on that hint.
I am pretty sure I am missing something... Help? (The interview is over but I am still wondering)

thanks
10 years ago
This is a bit embarrassing to ask... Feel like I should know this by now....

When talking about volatile variables, we always say that thread has a local copy of a variable from the main memory. Writes needs to be flushed out to take affect. I just realized this didn't make sense to me quite 100%. I think because I don't have enough knowledge on the hardware side.

What is the significance of that local copy of an variable? Does it correspond to register? CPU cache?
11 years ago
thanks. This really helps

Assuming we don't want to trim the zero, this is it:

11 years ago

chris webster wrote:If you simply have a list of numbers, you could use the List's sum() method directly (min() and max() methods are also available):
The same methods should work on other collection types e.g. Arrays as well. Scala's collections offer a lot of extra functionality (more than Java) so it's worth looking through the relevant APIs before you try to re-invent the wheel. Also check out Effective Scala on collections, and 10 Scala One Liners To Impress Your Friends for a quick insight into how concise and elegant Scala code can be.



hmm... I am aware of these methods. But none of them helped me on my problem.
Note that I want the following
input
1, 3, 4, 5
output
1, 4, 8, 13
11 years ago
When doing dynamic programming, it's very often that you need to calculate a running sum to an array. I decided that I want to write a function that does this. But I got stuck. Can someone help me?

I wanted to write something like:


Another crappy version that won't compile either, of course
11 years ago

Mike Simmons wrote:What's misleading about it? And why belabor the obvious? Of course there's no reason to allocate n pieces of memory for identical copies. That's why I said it was efficient.


I meant no offense. I agree with you then. I just thought that some people might take it as a big object got allocated.
11 years ago

Seetharaman Venkatasamy wrote:

Peter Hsu wrote:Just got a rejection call from a prominent company.


I usually don't like long message. rejection at one point just one point! all is well ...



Rejection is not what bums me out. It's being criticized for writing slow code... Really shattered my ego
11 years ago

Peter Hsu wrote:

Mike Simmons wrote:To be fair, the first line does create a collection of size n. It just does it very efficiently.


Technically you are right, but I found this to be a misleading statement
It does create a "collection" which's c.size()==n but it does not allocate n piece of memories and that's what makes it efficient



See: http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#nCopies%28int,%20T%29
11 years ago

Mike Simmons wrote:To be fair, the first line does create a collection of size n. It just does it very efficiently.


Technically you are right, but I found this to be a misleading statement
It does create a "collection" which's c.size()==n but it does not allocate n piece of memories and that's what makes it efficient
11 years ago
I just realized something
At one point I use the following code



instead of



I used the first one because I wanted to show off my knowledge in java collection utilities. In retrospect, I wonder if he thought the first one created a collection of size n... That would have really looked slow.

The general rule of thumb is that the bigger n is the faster the first one is compare to the second one. I should have defended all lines of my code as I am writing them!
11 years ago
Just got a rejection call from a prominent company. It was the first time in my career that I failed to get a job that I really want (among ones that actually gave me an interview for) It was not a huge loss as I am currently employed. She said that it was "very close" and ask me if I would like to be contacted in 18 months. I said yes, of course.

For me, that "very close" meant that one of the interviewer really didn't like me. Among 5 rounds of interviews, I believe I did ok in 4 of them, but I really messed up royally in one round. I never gave it a second thought until I get the rejection, but when I really think of it... I really sucked then.

What really shattered my confidence is that when I asked her for feedback. She responded "my code were not efficient enough". Oh no... anything but that...

I remember completing one of interviewer's question early and we had a little chat, I even mentioned that I felt that I am a little better than average in terms of optimizing performance of a piece of code. I even gave a quick example of what I did in my previous job that fixes a huge performance issue and he was quite happy about it. I also added something like "When I optimize, I never sacrifice clarity for performance. For example I prefer objects over functions." It was not very well put but what I meant was that my thinking is very object oriented and flexible. But I think he gets what I meant and agreed with me and even complimented me on something I did earlier in his question and how he likes the way I encapsulate a piece of logic in a class.

Ok, here's the deal. When I code, I always count the number of operations in my mind and how expensive are those operations. I always ask myself "are they minimal?" I have always thought I have gotten quite good at it already.

I knew I sucked at my first round. I knew I sucked because I panicked after I realized I have gotten the requirement wrong and I have already written a whole whiteboard of code. I would have been able to fix it... If I wasn't in such an panic and running around like an ant on a heated frying pan. I don't exactly remember what I did but I just remember it was wrong solution after wrong solution because I have never calmed down to think about the problem.

On that round, my code has efficiency problem, for sure, but I really wanted to believe only because I am not myself. It's just hard when I felt I am being judged on something that I believe I am strong in.
11 years ago
Great conversation, thanks Chris, Steve

Chris Hurst wrote:It's got some subtle nasty's ....

The assignment of data although to an immutable doesn't help the reference for data is not published, getData could in theory return null after updateData (as an example) has been called you need a write memory barrier between the assignment of updateData and a potential read from another thread of getData. updateData should have been synchronized or data volatile etc etc Synchronzing getData and updateData is by far the simplest thing so if your having difficulty just do it, I would always recommend just synchronizing all public methods in such a class or marking the class clearly as unthread safe.

I would also do as little as possible in a constructor, its generally a bad rule of thumb to publish the this pointer from a constructor anywhere another thread can get hold of it as special memory model guarantees (e.g. finals are final) apply only after you exit a ctr. In this case I can't see anything wrong as scheduling should synchronize and give you happens before ordering at that point and you have no code after the schedule and the timer task would only call back after a second anyway lol . If your not confident synchronize your public methods and keep your ctr simple, if you later get performance issues profile and address then.



Yes, I would perform a null check if this is production level code. However, I would not agree would making everything synchronized though. This kind of defeats (at least part of) the purpose of immutable pattern.