| // |
| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License. |
| // |
| |
| = DevFaqRequestProcessor |
| :jbake-type: wiki |
| :jbake-tags: wiki, devfaq, needsreview |
| :jbake-status: published |
| :keywords: Apache NetBeans wiki DevFaqRequestProcessor |
| :description: Apache NetBeans wiki DevFaqRequestProcessor |
| :toc: left |
| :toc-title: |
| :syntax: true |
| |
| === When should I use RequestProcessor.getDefault() and when should I create my own RequestProcessor? |
| |
| `RequestProcessor.getDefault()` is tempting to use, but it is also dangerous. This FAQ item will tell you when *not* to use it. |
| |
| One of the most common threading bugs in NetBeans happens like this: |
| |
| * Someone needs to run a background operation, so they use [link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/RequestProcessor.html#getDefault([http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/RequestProcessor.html#getDefault(]) `RequestProcessor.getDefault()`, the global thread pool |
| * They don't realize that it is possible for either that code to be called repeatedly (by the user, in a loop or whatever) |
| * The background work can take some time to run |
| * A user notices that NetBeans is slow, takes a thread dump, and there are 40 background threads all doing the same thing at the same time |
| |
| `RequestProcessor` has a constructor argument for its _throughput_. That says how many threads this `RequestProcessor` is allowed to use at the same time. When you call `new RequestProcessor("Useful name for thread dump", 3)` you are creating a thread pool that can have 3 threads available to run things on simultaneously. |
| |
| *The throughput of `RequestProcessor.getDefault()` is `Integer.MAX_VALUE`*. Think about what that means: it can potentially create thousands of threads; but your OS cannot necessarily handle thousands of threads, and you probably don't have thousands of CPUs. More threads than CPUs means the OS does extra work time-slicing between the threads and things get slower, not faster. |
| |
| `RequestProcessor.getDefault()` is useful for _one-off_ operations - you have some situation that happens once in a great while, and, say, while constructing some object, you need to do some work in the background; that work will probably never need to be done again for the life of the Java VM. That's a perfect case for `RequestProcessor.getDefault()`. |
| |
| Now here is the *anti-example*: You are creating a `link:http://bits.netbeans.org/dev/javadoc/org-openide-nodes/org/openide/nodes/Node.html[Node]` that represents a file. It needs to mark itself with an error badge and color its text in red if the file contains errors. You can't read the file when you create the Node - that takes too long. So when the node is created, it runs a background task to check its status, and updates its icon and display name after it has read the file. Now imagine you did this with `RequestProcessor.getDefault()`. What happens when the user expands a folder that contains 1000 of your files? 1000 threads get created, and the whole application gets very, very slow. For that, you are much better off creating one new `RequestProcessor` and using it for all your nodes. The FAQ entry about `link:DevFaqRequestProcessorTask.asciidoc[RequestProcessor.Task]` shows how to do this correctly. |
| |
| If you create your own RequestProcessor, please always use a name. If you get a deadlock it makes debugging much easier. |
| |
| === Apache Migration Information |
| |
| The content in this page was kindly donated by Oracle Corp. to the |
| Apache Software Foundation. |
| |
| This page was exported from link:http://wiki.netbeans.org/DevFaqRequestProcessor[http://wiki.netbeans.org/DevFaqRequestProcessor] , |
| that was last modified by NetBeans user Braiam |
| on 2010-03-03T22:04:22Z. |
| |
| |
| *NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed. |