blob: f5b5f0dd51423f30e661f70888a62e0ac013a07c [file] [log] [blame]
//
// 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.
//
= DevFaqThreading
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqThreading
:description: Apache NetBeans wiki DevFaqThreading
:toc: left
:toc-title:
:syntax: true
=== I need to run some code on a background thread. Can the platform help me?
First, ask yourself why you need to do this and if it is really necessary. Generally there is only one reason: _You are doing something takes some time (file I/O, computing something large and complicated, talking to a network socket) that will block the UI._
NetBeans contains a thread pool `org.openide.util.RequestProcessor`. (You can use the thread pools that exist today in `java.util.concurrent` but it is more typical to use `RequestProcessor`.) There is a general purpose built-in thread pool - `RequestProcessor.getDefault()`. You can use that for things that only happen once in a while; otherwise you are probably better off creating your own instance of `RequestProcessor`. There is a link:DevFaqRequestProcessor.asciidoc[FAQ item about how to know when to do which]. In its most simple usage, `RequestProcessor.post()` is called with a `Runnable`. The call returns a `RequestProcessor.Task` which you can use to monitor the status of the task and listen to task finish among other.
Note that if you are doing something in the background, you may want to use the link:http://bits.netbeans.org/dev/javadoc/org-netbeans-api-progress/org/netbeans/api/progress/ProgressHandleFactory.html[Progress API] to show a progress indicator in the status-bar (or use it to put up a modal progress dialog if the UI really needs to be blocked - use with care, only when really necessary).
Remember that if you are running more threads than you have processors (or cores) - and your OS is probably using some as well - then when you ask to multi-thread, you are asking your CPU to divide the time of the CPUs you have between more virtual threads. And switching the context a CPU is working in - sending it off to some other memory space and set of instructions, and then another - takes time. So heavy use of multi-threading, especially on single CPU machines, can slow things down rather than speed them up. If you can make your code run faster, do that first.
=== 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/DevFaqThreading[http://wiki.netbeans.org/DevFaqThreading] ,
that was last modified by NetBeans user Dsimonek
on 2009-12-03T15:28:32Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.