What does this java.execute () method mean?

I read tutorials on sun java and I came across this page here:

How to make an applet

Under the heading "Threads in Applets", I found this piece of 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(); } 

I'm a beginner at first, so I'm sorry if this is a stupid question. However, I have never heard of this .excecute (). I do not understand this, and I cannot find anything about this from Google. I see this is ... an anonymous inner class? (Please correct me) and it starts a stream to upload images. I thought the run () method is called with a start () call? Please help me clear this confusion.

+6
source share
2 answers

execute is a SwingWorker method. What you see is an anonymous class created and having an execute method that is called immediately.

I have to admit that I'm a little surprised that the code is compiling because it seems to assign the execute result to the worker variable, and the documentation says that execute is void .

If we look at this code a bit, it might be clearer. First, we create an anonymous class that extends SwingWorker , and instantiate it at the same time (this is a big bit 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 worker (which is a bit which, it seems to me, should not be compiled):

 SwingWorker worker = tmp.execute(); 

Update . And indeed, I tried this, and it does not compile ., So this is not a great example of 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(); 
+7
source

.execute() calls the execute method on the anonymous class instance; those. an object created using new SwingWorker<ImageIcon[], Object>(){...} . (This is a class that extends the SwingWorker class.)

According to javadoc, the execute method schedules the task represented by the instance that will be executed in the existing workflow. Workflow life cycle (for example, creation, launch, etc.) Implemented by Swing infrastructure.

+1
source

Source: https://habr.com/ru/post/916660/


All Articles