This comes from a piece of code from an example of JSch , a good ssh client in java. A work collegue had the bad idea to remove the Thread.sleep call and was struggling with why it would randomly work.The way I would have done it is the following:
This has the advantage of being more readable and having less secret spices in it. In the first code, the call to available() is non blocking, meaning that without the Thread.sleep(), there will never be the time for the socket buffer to fill up. But is the first code more efficient or the second code?
I did a search on google to understand the interest of the first code. The only advantages I found in the first code are the possibility to interrupt the thread running the code and a finer grained control on timeouts.
There is a lengthy explanation by Doug Lea in his book "Concurrent Programming in Java". This book usually provides excellent explanations, and is a must read for anybody doing concurrent programming. But this time, about this subject, I did not find him that clear.
There is a more simple explanation in a course from San Diego State University (see last example)
A read() on an inputstream or reader blocks. Once a thread calls read() it will not respond to interrupt() (or much else) until the read is completed. This is a problem when a read could take a long time: reading from a socket or the keyboard. If the input is not forth coming, the read() could block forever.
When a thread executes a read() call on an InputStream, if no byte is available. The calling Thread blocks, in other words, stops executing until a byte is available or the Thread is interrupted.
Still I am wondering if the second code would not just go into IOException (socket timeout), on timeout (adjustable with Socket.setTimeout ) and release the Thread then. Do you have an idea when the first code could be better?
Why I'm leaving Sun and... what next? (304): Leaving Sun is possibly one of the most difficult decisions I've ever made. But I think it's time for me to start new things and, well, it's also time for Sun to start new things. [read]
Testicular Cancer (198): I've just been diagnosed with testicular cancer. It's probably seminoma (which is relatively good). However, other things are now on my mind and so this blog will not be updated for a while. [read]
JBoss Seam: Make Spring inside (111): We here try out JBoss Seam, and find JBoss Microkernel, is this a IOC from JBoss? Why dont we use Spring instead? [read]
Why are we still dealing with C++ vs Java (97): I had an interesting conversation this week with someone that believed that the Java world was filled with people who couldn’t code. In his opinion, [read]
"Look Ma, no locks!" (97): Brian Goetz has written an excellent introductory article on nonblocking algorithms and showcases some simple nonblocking data structures with code examples and pictures. [read]
6 Ways of Setting Java Classpath (96): How to set Java classpath? List as many ways as you can. This can be an interesting Java job interview question. [read]
Spring, JBoss and The Elephant (96): Interface 21 and BEA recently announced the release of Pitchfork, the EJB3 implementation within the Weblogic container, built using Spring. [read]
Ubuntu 6.06 (94): I upgraded my Ubuntu box the other night. The process was pretty impressive. For one thing, it was significantly faster than an MSWindows or MacOS upgrade. [read]
Integrating spring and GWT (84): I just completed my first shot at integrating Spring with GWT. You can check it out here Here’s what you do to expose a simple service. 1) Write the remote service, [read]
Java is supposed to be much better to build big projects, because of static type checking, and all the rigour around the language. But how many of you have seen medium sized projects taking more than 30 minutes to build.
At work, they have a standard J2EE project, with only about 50 EJBs, hundreds of JDO classes, and standard classes. Between the JDO generation, EJB generation, EJB dependencies calculations, and packaging, it takes 20 minutes. And the project is not doing that much. One can optimize to avoid dependencies calculations and it would then take about 12 minutes. But still.
With Python, Ruby or your favorite interpreted language, this would be at most a few seconds to test a new version. Now I am a fan of Java and don’t enjoy that much programming in interpreted languages, especially since modern IDE like Eclipse do so much for you.
To sum up, Java is supposed to be the right language for large scale projects, and yet it is on large scale projects that compilation is an issue. Ok, this will enforces a better separation of concerns, and will probably be beneficial in the end. But I am not sure that most people do a good separation of concerns in projects they manage. I think it is no accident if the next step in Java development involves much more runtime compilation. It somehow started with JSPs, and then with the various XML config files combined to java reflection. It is now made even more popular with annotations, see EJB 3.0.
I am wondering a bit if one day compiled languages will be only a curiosity for most enterprise projects.
Recently, one of my relatives computer under Windows XP, refused to boot. There was no way of fixing it with Windows Install CD as partition table seemed corrupt to Windows. I tried everything in an 2003 Ultimate Boot CD, but nothing worked out.
Someone gave me an install cd of Ubuntu Linux, and it managed to read the data. Well sometimes only. The erratic behaviour was due to a bad ATA cable. This probably was the cause of the corruption in the first place. Anyway with a new cable, Windows was still not able to read its data. But Ubuntu Linux, now working well, was able to, without having anything to configure (except mounting the drive).
So I copied the data, and reformated the NTFS partition, reinstalled Windows, recopied the data.
After this experience, I’d recommend to any Windows user to have a spare Ubuntu Linux Live CD, just in case your Windows corrupts itself.
Every week, I post javablogs top 10 most read blog entries on this blog. The reason for it was that I don't look at what's happening on the week-end and this will pickup interesting stories from the weekend, and I also don't watch javablogs everyday. Overall I find it quite good to be uptodate with interesting stuff happening on javablogs.
As mentionned in an earlier post my library of choice to do the parsing is htmlparser (on sourceforge) because it's free, open source and because I am lazy and did not want to do my own. If you know a better open source library, feel free to add a comment about it, I'll be glad to hear about it. htmlparser is not the easiest library to use, there are many entry points and it's not immediately clear which one to choose. So I post here how I used it if it can save a few minutes to people having to do this task.
private static Entry parseEntry(String content) throws ParserException { final Entry entry = new Entry();
final NodeVisitor linkVisitor = new NodeVisitor() {
@Override public void visitTag(Tag tag) { String name = tag.getTagName();
if ("a".equalsIgnoreCase(name)) { String hrefValue = tag.getAttribute("href"); if (hrefValue != null && !hrefValue.startsWith("http://")) { if (!hrefValue.startsWith("/")) hrefValue = "/"+hrefValue; hrefValue = "http://javablogs.com"+hrefValue; //System.out.println("test, value="+hrefValue); } if (hrefValue != null) { hrefValue = hrefValue.replaceAll("&", "&"); tag.setAttribute("href", hrefValue); } } }
};
NodeVisitor visitor = new NodeVisitor() {
@Override public void visitTag(Tag tag) { String name = tag.getTagName(); if ("span".equalsIgnoreCase(name) || "div".equalsIgnoreCase(name)) { String classValue = tag.getAttribute("class"); // LOGGER.debug("visittag name="+name+" class="+classValue+"children="+tag.getChildren().toHtml()); if ("blogentrydetails".equals(classValue)) { Pattern countPattern = Pattern.compile("Reads:\\s*([0-9]*)"); Matcher matcher = countPattern.matcher(tag.getChildren().toHtml()); if (matcher.find()) { String countStr = matcher.group(1); entry.count = new Integer(countStr).intValue(); }
Thanks... and good luck Bruce! (203): It is unfortunate that Bruce Tate forgot to enable comments to his final blog entry. It would be a shame to see him off without at least a small well-wishing. (possibly a little roast too ;-) [read]
Google Web Toolkit Angst (202): I've been using Google Web Toolkit for the last week or so. I'm really liking it, it is really productive and once you getting it working everything is sweet. The problem is, [read]
Why ORM Tools are Not Recommended (185): Sandeep Sha has written an a forum posting by Why ORM Tools are Not Recommended that has some interesting points. Although I do not agree with all the points, [read]
The Dojo Toolkit in Practice (185): We have posted a new article on using the Dojo Toolkit in a project. The article discusses a piece of a project that uses Ajax to create a responsive itinerary viewer. [read]
Thanks... and good luck Bruce! (203): It is unfortunate that Bruce Tate forgot to enable comments to his final blog entry. It would be a shame to see him off without at least a small well-wishing. (possibly a little roast too ;-) [read]
What’s Up With Huge Resumes? (150): What’s up with huge resumes these days? The company I work for has been hiring lately and so I usually end up interviewing one to two people a week. [read]
Introducing jvm-languages.com (147): Back in September of 2004, I tried to write a book. It would have been called Dynamic Languages and Java. Unfortunately, I never completed it. [read]
Then God said let there be Ubuntu... ahem (130): Finally I got a version of Linux, which works as good as XP or even better ;) ; using which I can get to do my work seamlessly. Its none other than Ubuntu Dapper. [read]
Job Trend, Not Google Trend (121): Wanna know the amount of Java jobs versus .Net jobs, or the growth of AJAX jobs? Google Trend may be able to help you a bit, but the result is not scoped for jobs only. Indeed. [read]
1-Minute Quiz: Why is Hyphen Illegal in Identifier? (110): Why is hyphen (-) an illegal char in Java identifier? Why can't we use variable names like first-name, as we do in xml files? The answer to this question is not hard, but the challenge is, [read]
The Worst Java Job Interview Questions. (269): Why are you looking for a job? Strictly speaking, this is not a java question, but it shows up in almost every job interview I've been to. [read]
Google Web Toolkit: A Brief Review (219): Google has released GWT - a java window toolkit which converts your java applications (using the toolkit API) to javascript (incl. AJAX) and HTML. [read]
Google Web Toolkit: Web Applications Just Got Harder (182): Oh the buzz. Oh the excitement. Oh the AJaX Gods has released their secret sauce with an Apache license. Google Web Toolkit allows one to develop AJaX web applications entirely in Java, [read]
PDFs available for JavaOne 2006 Sessions (177): Check out the JavaOne 2006 Conference Session Catalog: “Presentation files available for download are indicated with a paperclip icon. After clicking on a paperclip, [read]
PDFs available for JavaOne 2006 Sessions (177): Check out the JavaOne 2006 Conference Session Catalog: “Presentation files available for download are indicated with a paperclip icon. After clicking on a paperclip, [read]
Cringely: Why IBM Is in Trouble (159): Robert X. Cringley doesnt have a high opinion of IBM. Last week, he wrote, ...what is IBM? IBM is a disaster-in-the-making. [read]
Day 5: McNealy, Gosling, Gage: "Forget the box" (139): With a mixture of sadness, relief, and hope for the future, former Sun CEO Scott McNealy took the stage this morning at the final keynote address of JavaOne 2006. [read]
This is genuine Microsoft (120): I started playing with Google Web Toolkit beta- actually I didn’t really start. Because I had to uninstall IE7 (which I don’t use at all), but hey I’d been curious. [read]
Axis2: Why bother? (257): The Axis team is kicking up a big fuss about their recent release of Axis 2 (1.0!) Surprisingly, this library is so so abysmally bad, [read]
Google trends proves: Java is doomed (251): Google trends is a nice idea, and I had to apply it adhoc to Java, Ruby, Python and C#. Interesting results, I can see a decline in Java! [read]
Your Next Programming Language (216): Many people talk about how, as software developers, we should learn new programming languages frequently. [read]
How to recognize a "Sacred Code" (210): You know you are dealing with a "sacred code" when you ask a previous developer (or the designer of the code) a question about the code and his immediate reply is .... [read]
7 Reasons Why Web Apps Fail (179): Web applications are popping up faster and faster every day, and quite a few are using the power that Ajax offers to their advantage. [read]
How to Design a Good API (176): I was reading this presentation on the Design of API's by Joshua Bloch it talk's about how to design a good api but more importantly the reasons why doing certain things results in a good design. [read]
JRuby on Rails Is Born (172): JavaOne attendees are in for a treat. Not only will they be receiving a DDJ issue which calls Rails a tipping-point to a new era in enterprise computing (or something like that)... [read]
JavaOne day -1 : Bird Strike (147): The plan was to fly out from Sydney to San Francisco today. The plane was fueled, the travelers boarded. The aircraft taxied out to the runway, takeoff speed was reached, [read]
10 things i love about my Mac (125): A switcher Top 10 of nice things on Mac OS X: 1. The way programs live in the system (no registry shit) 2. The shell 3. Firewire boot capabilities 4. Apps like iChat, iSync and Addressbook 5. [read]
10 things i hate about my Mac (113): A switcher Top 10 of ugly issues with Apple Mac OS X: 1. No @ key in boot camp windows installation available 2. All banking programs on mac really suck 3. adv. [read]
Commons Collections 3.2 Released (113): Commons Collections 3.2 has been released. Commons Collections is a library that builds upon the Java Collection Framework. It provides additional Map, [read]
GoogleTrends : Java vs C# vs PHP (112): La comparaison est un poison. Ceci dit, comparer "l'intérêt" pour java, C# et PHP avec GoogleTrends, le nouveau service de Google, était très tentant... [read]
If you need to cache objects in your system, Ehcache is a simple cache written in Java, widely used and well tested. I will present here a short tutorial on how to use EhCache for people who don't want to look around the documentation at first, but just want to test if it works in their project and to see how easy it is to setup.
Unpack Ehcache with an unpacker that knows the tgz format. For unix users, it is trivial, for windows users, 7zip is a free (and open-source) unpacker. It is probably the most popular, but there are other ones like tugzip or izarc or winrar.
In your java project you need to have ehcache-1.2.jar, commons-collections-2.1.1.jar and commons-logging-1.0.4.jar (versions numbers may vary) in your classpath, those libraries are shipped with ehcache.
Cache Configuration
Write an ehcache.xml file where you describe what cache you want to use. There can be several files per project, several cache descriptions per file. I use here a persistent cache. Configuration file is well described at http://ehcache.sourceforge.net/documentation/configuration.html
/** *retrievesvaluefromcacheifexists *ifnot createitandaddittocache*/ public String doit(String key, String value) { //getanelementfromcachebykey Element e = cache.get(key); if (e !=null) { value = (String)e.getValue(); LOGGER.info("retrieved"+value+"fromcache"); } else{ value ="newvalue" ; cache.put(newElement(key, value)); } return value; }
I am not particularly a fan of JCS (Jakarta Cache System) as I find ehcache code very clean and simple. But I have to say the author has some good comments on the site:
Nulls vs. Errors
I started to support ObjectNotFoundExceptions for failed gets but the overhead and cumbersome coding needed to surround a simple get method is ridiculous. Instead the JCS return null.
For having seen too many times the ObjectNotFoundException "pattern", I can only agree!