A sure way to kill off your Java program's used resources

We have all read about finalizers and the reasons why you shouldn’t use it within your program.
However, I have a good way of making use of them.

If you have resources that need to be freed within your main program hint hint, then why not use finalizers in that one place?
It isn’t going to do anything bad. Java will have to handle all the horrible exceptions and internal mess afterwards and not in the program.

BTW: I realise this isn’t how you properly terminate threads. :slight_smile:


class Main extends JFrame
{
        .....................

        public static void main(String[] elephants){
          .....................
        }

        ...........

        protected void finalize() throws Throwable
	{
		super.finalize();
                /*
                most unsafe code in the universe!
                then again, why should I care?
                this is the JVM's mess now, my program has already ended by the time this is called.
                this just makes it absolutely certain that everything is dead.
                */
		tmpThread.interrupt(); //will throw an exception
		tmpThread = null; //God no! Am I on drugs? Nulling a thread while it's quite possibly still RUNNING!!!
	}

         ................
}

Because there is no gaurantee it will ever get called.

In fact, its likely not to as there is no need to collect the Main in most cases since at the moment the main ends so does the process.

While in a resouce tracked OS this might all be irrelevent, since the OS wil lcollect all the resources at process end, ist still abd form as you are implying code will get called that wont be. It CAN even be dangerous if you are expecting it to do domething like send another process an IPC message before the provess ends.

Thanks.
I thought finalizers do get called, but are unpredictable in terms of time.
Looks as though I’m only partly right.

Now I can see why finalizers should never be used under any circumstance.
Why aren’t they removed from Java alltogether?

Because it’s too late.

I think there’s some massive irony somewhere in that statement, now I’ve posted it…

Cas :slight_smile:

Heh heh :slight_smile:

Cas is absolutely right. There are too many out there and we’d break code worse then it might already be broken if we removed them.

They CAn be used as a kind of hail-mary last ditch thing. We do that in some of our JDK librarys. we tell you that you MUST explicitly call a dispose() but we also call it in a finalizer as a “okay, you were an idiot but maybe we can save your bacon” sort of thing. There are lots of philosophical arguments possible on whetehr this is a good or bad thing.

Since the generational colelctor though tehy have ahd an additional negative imapct in that they force an object to live past the eden-space even if its shoirt lived. For that reason you don’t really ever want a finalizer on a short-lived object.

I think Java needs a lot of remodelling.

I hope Java 2.0(Not Java 2, but Java 2.0.0, or should I call it Java 3?) will eliminate deprecated methods, implement runtime generics and remove finalizers among many other things.

[quote=“K.I.L.E.R,post:7,topic:24480”]
I hope it doesn’t :stuck_out_tongue:
Imagine how much software that would break.

Imagine how much future software it would make better. :slight_smile:
You could argue that there wouldn’t be any more future software if it breaks compatibility.

Damned if you do, damned if you don’t.

At Some Point…

When the VM really is doing everything everyoen wants of the current Java, I could see an argumetn for a new project that took everything we’ve learned and started over. It would probably be better to name it something different to make it clear that it was a totally new fork…

Anyone for Java++ ;D

[quote]When the VM really is doing everything everyoen wants of the current Java[…]
[/quote]
You can see that happening?
You’re definitely an optimist. :lol:

Someone will always ask for more.
Eventually you’re probably going to have a VM that programs itself at the command of a human thought.
Even that wouldn’t be enough for some people.

I don’t see how taking out finalizers and deprecated methods makes future software better. Future software are not supposed to use deprecated methods or finalizers anyway. Taking them out might get rid of some compiler warnings, but will mostly just break existing code. Not worth it IMHO.

What the world needs is more dreamers…

Kev

I believe the problem is that there are too many dreamers.
What the world needs is people having realistic expectations and reasonable demands.

Then we’d still be throwing sticks and stones at rabbits in thick forests.

No we wont. I said that we have “too many dreamers”.

It was actually a compliment - for the imagination of the comment.

What the world probably needs is less people trying to decide what the world needs :slight_smile:

Kev