tags:

views:

174

answers:

7

Hi,

I am a .NET dev learning Java. As a result, I understand all the fundamentals of programming like loops, variables, functions, etc. My biggest bottleneck is that I don't know my way around Java's packages like I do with .NET's.

This is the reason for this thread - where do I find the following functionality in Java?:

System.Diagnostics - where can I use things like stopwatch, programmatic breakpoints and loggers?

Is there a Java equivalent of .NET performance counters?

What are weak keys in Java? I just came across a collection which uses weak keys in Java so am asking here. :)

Thanks

+3  A: 

A dictionary which uses weak keys only keeps a weak reference to each key, so if the key is otherwise eligible for garbage collection, it may be collected and the dictionary will effectively lose the entry. See the docs for WeakReference for more about weak references.

System.Diagnostics: I don't know of any equivalent for stopwatches and programmatic breakpoints. For logging, look in java.util.logging or a 3rd party package such as log4j.

Performance counters: There may be some way of hooking some JVMs into Windows Performance Counters, but I've never seen them. There's VisualVM which you may find useful for some of the same things though.

Jon Skeet
A: 

As for loggers the package is:

java.util.loogging

import java.util.loggin.Logger;

...

Logger logger = Logger.getLogger( this.getClass().getName() );

logger.fine( "Hello" );

logger.severe(" Oh oh ");

Here are some useful links:

http://java.sun.com/javase/6/docs/api/java/util/logging/Logger.html

http://java.sun.com/javase/6/docs/api/

http://java.sun.com/javase/6/docs/api/allclasses-noframe.html

OscarRyz
A: 

You should check out http://commons.apache.org/ for a lot of your other queries. Java 1.4.2 and above have java.util.logging

Rob Elsner
+1  A: 

If you just want to time something, you can use System.nanoTime().

long start = System.nanoTime();

// a bunch of code

long end = System.nanoTime();

System.out.println("Elapsed time in seconds: " + (end-start)/1000000000.0);
dancavallaro
For getting the elapsed time in seconds System.currentTimeMillis() is a much better choice. Mainly because it's not as prone to overflow (as the JavaDoc of nanoTime() mentions).
Joachim Sauer
A: 

The concept of "weak keys" comes from encryption. Perhaps you were thinking of weak references? Both .NET and Java support weak references.

Glenn
Typically a map with weak references for keys is said to use "weak keys". See the WeakHashMap docs for an example: http://java.sun.com/javase/6/docs/api/java/util/WeakHashMap.html
Jon Skeet
I would get it more under weak references.
OscarRyz
A: 

I've noticed that common .NET classes reside in Java.Util (date is one of those common classes - something I and I'm sure every dev uses often in programming).

Ah so weak keys is related to weak references - a concept I'm familiar of in .NET. That ends that question as it's answered now. Thanks guys!

As for performance monitoring (counters), looks like there's no built in functionality into Java. Then again, performance counters is a Windows feature and not just a class in .NET I guess? I am developing on Windows.

dotnetdev
Do you have a link for "performace counters" ( ok I'm lazy I don't want to google it ) Sound interesting. :)
OscarRyz
A: 

JAMon is one possible open-source Java Monitoring library that might fit your requirements.

In addition, any performance/management counters are exposed through Java Management Extensions (JMX)

Then you have a number of monitoring consoles:

  • jconsole comes with bundled with the Java installation (Swing based)
  • MC4J open-source console (Swing based)
  • eclipse-jmx console within Eclipse (SWT based)
  • Most application servers will come with a web-based JMX console (eg: JBoss)

Alternatively, take a look at this other posting for ideas on how to monitor JMX statistics in Perfmon.

Hope this helps.

toolkit