This is Java What does the execute () method call mean?

I'm reading the Sun Java Tutorial. I see this page here:

How to Make an Applet

Under the heading "threads in applets", I found this Code:

//Background task for loading images.
    SwingWorker worker = (new SwingWorker<ImageIcon[],Object>() {
            public ImageIcon[] doInBackground() {
                final ImageIcon[] innerImgs = new ImageIcon[nimgs];
            ...//Load all the images...
            return imgs;
        }
        public void done() {
            //Remove the "Loading images" label.
            animator.removeAll();
            loopslot = -1;
            try {
                imgs = get();
            } ...//Handle possible exceptions
        }

    }).execute();
}

First of all, I'm new, so if this is a stupid question, I'm sorry But I've never heard of ". Exercise()" I don't understand. I can't find anything about it from Google What I see here is... An anonymous inner class? (please correct me), it is starting a thread to load images I thought I called the run () method by calling start ()? Please help me clear this confusion

Solution

Execute is a method of swingworker What you see is that anonymous class is instantiated and its execute method is called immediately

I have to admit that I was a little surprised by the code compilation, because it seems to assign the execution result to the worker variable, and the document tells us that execute is a void function

If we deconstruct the code a little, it will be clearer First, we create an anonymous class that extends swingworker and create an instance of it at the same time (this is the majority in parentheses):

SwingWorker tmp = new SwingWorker<ImageIcon[],Object>() {
    public ImageIcon[] doInBackground() {
            final ImageIcon[] innerImgs = new ImageIcon[nimgs];
        ...//Load all the images...
        return imgs;
    }
    public void done() {
        //Remove the "Loading images" label.
        animator.removeAll();
        loopslot = -1;
        try {
            imgs = get();
        } ...//Handle possible exceptions
    }

};

Then we call execute and assign the result to the worker (in my opinion, this should not be compiled):

SwingWorker worker = tmp.execute();

Update: in fact, I tried it. It doesn't compile So it's not a good example code This will compile:

SwingWorker worker = new SwingWorker<ImageIcon[],Object>() {
    public ImageIcon[] doInBackground() {
            final ImageIcon[] innerImgs = new ImageIcon[nimgs];
        ...//Load all the images...
        return imgs;
    }
    public void done() {
        //Remove the "Loading images" label.
        animator.removeAll();
        loopslot = -1;
        try {
            imgs = get();
        } ...//Handle possible exceptions
    }

};
worker.execute();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>