I've spent countless hours troubleshooting different applications. Based on past experience, I can conclude that for most developers, you should stay away from a few Java SE features or APIs. Most developers here refer to generic Java EE developers rather than library designers or infrastructure developers.
Frankly speaking, in the long run, most teams should stay away from the Java features below. But there are always exceptions to everything. If you have a strong team and always have a clear sense of what you're doing, just do what you think. But for the most part, if you use the following Java features in your project's development, you'll regret it in the long run.
reflection : In popular libraries such as spring and Hibernate, reflection naturally has its niche. But introspective business code in many cases is not a good thing, for a lot of reasons, generally I always recommend that you do not use reflection.
The first is code readability and tool support. It's easy to open the familiar IDE and look for the internal dependencies of your Java code. Now, using reflection to replace your code and try again, what's the result? If you modify an already encapsulated object state by reflection, the result becomes more uncontrolled. Take a look at the following sample code:
If you do this, you will not be guaranteed a compile-time security. As in the example above, you will find that if the parameters of the Getdeclaredfield () method call are wrong, they are only available at runtime. To be sure, finding a run-time bug is far more difficult than the compile-time bug.
Finally, we need to talk about the cost issue. The degree to which the JIT optimizes reflection is different, some optimizations take longer, and some cannot even be optimized. Therefore, sometimes the performance loss of reflection can reach several orders of magnitude difference. In a typical business application, however, you may not notice the cost.
To summarize, I think the only reasonable (direct) use of reflection in business code is through AOP. Besides, you'd better stay away from this feature of reflection.
Bytecode manipulation : If you use Cglib or ASM libraries directly in your Java EE application code, I suggest you take a good look. Just as the negative effect of the reflection I mentioned earlier, the pain caused by using bytecode manipulation may be several times as many as reflections.
Even worse, you don't see executable code at compile time. Essentially, you don't know what code is actually running in the product. So you have to spend more time in the face of running problems and debugging.
ThreadLocal: Seeing ThreadLocal in business code can make me tremble for two reasons. First, with the help of threadlocal, you can pass a variable without explicitly passing a method call and become addicted to it. In some cases it may be reasonable to do so, but if you are not careful, then I can guarantee that there will be a lot of unexpected dependencies in the final code.
The second reason is related to my daily work. Storing data in threadlocal can easily cause a memory leak, and at least one of the 10 permanent generation leaks I see is caused by an overdose of threadlocal. Even the use of the same loader and thread pool, "Java.lang.OutOfMemoryError:Permgen space" is not far away waiting for you.
class loader : First, the ClassLoader is a complex thing. You must first understand them, including hierarchical relationships, delegation mechanisms, class caches, and so on. Even if you feel that you have mastered the ClassLoader, there are a variety of problems that can occur when you start using it, and will likely cause the classloader to leak. Therefore, I recommend that you leave the ClassLoader with the application server.
weak references and soft references: about weak references and soft references, do you know only what they are and how they are used in a simple way? Now that you have a better understanding of the Java kernel, will it be possible to rewrite all the caches using soft references? This is not very good, but you can not have a hammer in hand everywhere to find drums knocking.
You may be wondering why I said caching is not very suitable for using soft references. After all, using soft references to build a cache can be a good illustration of delegating some complexity to a GC rather than implementing it yourself.
Let me give you an example. You build a cache with soft references so that when the memory line is exhausted, the GC will intervene and start cleaning up. But now you have no control over which objects are removed from the cache, and it is likely that the object will be recreated again in the next cache. If the memory is still tight and triggers a GC to perform a cleanup, there is a good chance that a dead loop will occur, and the application will take up a lot of CPU time and the full GC will continue to execute.
Sockets: Java.net.Socket is simply too hard to use. I think its flaws ultimately stem from the nature of its blockage. When writing a typical Java EE application with a web front end, you need a high degree of concurrency to support a large number of user accesses. The last thing you want to do is let the less scalable thread pool stay there and wait for the blocking sockets.
Now that there's a great third-party library to solve these problems, don't write it yourself, try Netty.
6 Java features you should be away from