My focus, at least initially, is working on client side (desktop) applications as opposed to server based enterprise systems. When learning a language / environment I find it very useful to be able to easily write code that gives rapid visual feedback that indicates to me that what I'm doing is actually correct. I feel choosing JSE as opposed to JEE allows me to more easily do this.
Basic multi-threading
I certainly haven't delved into the depths of the threading library in Java however the simplicity of the Runnable interface is very nice. You simply implement the Runnable interfaces Run() method on a class that requires another thread of execution.Then create an instance of the Thread class, passing an instance of the Runnable class to the constructor. Finally, call start() on the thread instance to begin thread execution. Here's an example:
public class AppWindow extends Canvas implements Runnable { private Thread _thread; @Override public void run() { // Do work on another thread here } public void start() { _thread=new Thread(this); _thread.start(); } public static void main(String[] args) { AppWindow appWindow = new AppWindow(); appWindow.start(); } }
Of course the real "fun" with threading depends on what you actually do in the run() method.
For example it's essential to avoid a situation where two (or more) threads read the same shared value, modify the value in some way and then update the shared value. This may not directly cause your application to crash, however it causes extremely confusing exceptions / behaviours. This is called a race condition and is a well known problem in concurrent programming.
The Java synchronized keyword is useful to ensure only one thread can run a block of code or a method at a time (serialized access) but this also can create a bottleneck when threads have to wait for a synchronized code to complete. In this way the Synchronized keyword is semantically identical to the c# lock keyword, although lock cannot be used on methods (a MethodImpl attribute is required).
I've also come across the AtomicXXXX classes such as AtomicBoolean, AtomicInteger that give you a safe way to modify some the atomic variable in multi-threaded code. Using an Atomic variable avoids the need for the Synchronized keyword and its associated bottleneck issue.
The methods on these classes such as compareAndSet() are guaranteed to complete before any other thread can interrupt and possibly modify the same variable. For example you could use one Atomic variable as a lightweight and safe way to count the number of threads currently executing some code and make a decision based on that number. NB: These classes are not radioactive in any way ;), the word "atomic" in this context means "all or nothing".
Multi-dimensional arrays
Java does support arrays of arrays (jagged arrays) however it does not provide a built-in C# style array [,]. However this hasn't been an issue for me because in Java you can declare an array of arrays by specifying the lengths of all dimensions at in the declaration:int [][] twoDArray = new int[10][10];
In C# you'd have to write:
int [][] twoDArray = new int[10][]; for(int i=0;i<10;i++) twoDArray[i]=new int[10];
Of course multidimensional C# arrays using [ , ] do make declaration easier, however in my experience they can be slower than jagged arrays for an application doing heavy array accessing.
In fact, in an informal performance test (I may need to create another post with details this test) Java jagged arrays were a little faster in access than C# jagged arrays and clearly faster than C# [ , ] style arrays.
No operator overloading
Java does not support custom operator overloading (except built-in string concatenation +). As a result much of my Vector manipulation code in Java looks like:Vec2D a = Vec2D.add(b.multiply(-1), c.multiply(0.5)); d.multiply(a);
With operator overloading the following would be possible:
Vec2D a = (b* -1) + (c*0.5); d *= a;
Now there are heated views for both sides on value of operator overloading. In all my time using C# I've only needed operator overloading on quite specific but very applicable situations such as with geometrical Vector and Matrix classes. Moreover, the increase in clarity of the above code cannot be ignored.
In my opinion redefining + - * / for a class such as Vector has no real drawbacks. The most common concern is that overloaded operators hide the actual code and therefore make it easier to write poorly performing code. However a decently written overloaded operator should be identical to the longhand approach in terms of performance. The programmer always has the choice of using the longhand approach anyway.
I personally can't remember ever redefining any further operators than the 4 mentioned above in C#. Adding the ability to overload at least these operators to Java is long overdue. Limiting it to these 4 operators is also likely to reduce the occurrence of obscure or misleading operator overloading.
Overriding methods
In Java, to override a method of a base class in the sub class you just add the @Override annotation above your overriding method. No need to check that the base class method allows it. This is because methods are virtual by default.In C# you must first ensure that the method you are overriding is either virtual or abstract and then add the override keyword as part of the method declaration. Methods are non-virtual by default in C#.
For reasons of clarity I prefer the C# explicit style where the programmer is forced to think about which methods should be overridable. If a programmer does not think about this at all (eg. leaves all methods as non-virtual), performance is still good as there is no dynamic binding required to call to these methods and they can be in-lined by the compiler giving the associated benefits.
Making a method overridable often requires careful thought about how this can change the behaviour of other class methods that may depend on it, as well as the overall usage of the class itself. So C# leans towards safety here. Classes are not sealed by default however, so this seems a bit inconsistent.
In Java if the programmer does not consider which methods should be overridable, they are, by default, all overridable and so require dynamic binding . The programmer can explicitly add the final modifier to a method to stop anyone overriding it. In the past this also has the side benefit of telling the Java runtime that the method does not need dynamic binding and can be inlined if beneficial.
So then, coming from C#, I might be tempted to add final to all of my base class methods. However the modern Java runtime can determine methods that are not being overriden and will treat treat them like a final method, with the associated performance benefits.
Therefore the rule here is to use the final keyword where it makes sense from in the context of your design, ie you specifically want to stop someone overriding for correctness / clarity of usage reasons. Do not use final for performance reasons as it obscures your intent to another programmer. The runtime is intelligent enough to do this for you.
No comments:
Post a Comment