Multithreaded programming in Netbeans Platform
I've looked through the books/docs about NBP, but there is nothing directly talks about multithreaded programming in NBP. Is the开发者_StackOverflow中文版re anything particular that needs attention regarding to multithreading in NBP? So, if I want to create a multithreaded NBP application, I just need to follow regular Java multithreaded programming practices, right?
The main thing to look at would be RequestProcessor and RequestProcessor.Task. RequestProcessor is a thread pool; a RequestProcessor.Task is a job.
Most of what RequestProcessor does is similar to what ExecutorService now does in the JDK. The main thing that is not easy w/ just the JDK is creating a task which can be rescheduled and run repeatedly. That is very useful if you, say, want to perform some work after a timeout when the user stops typing:
private static final RequestProcessor rp = new RequestProcessor(MyClass.class);
private RequestProcessor.Task task = rp.create(new Runnable() {
public void run() {
//...do some expensive parsing or similar
}
});
public void keyPressed (KeyEvent ke) {
task.schedule(200); // (re)schedule the task 200ms in the future; if schedule() is called again, it will be postponed
}
If you're using the Nodes API, it is thread-safe, and you can update your nodes as you wish.
If you are doing something which touches Swing components, remember to always run that code with EventQueue.invokeLater(Runnable) - and never use EventQueue.invokeAndWait() - it is a recipe for deadlocks.
If you have code which may sometimes be called on the event thread and sometimes not, NetBeans has a simple way to ensure your code runs on the event thread: Mutex.EVENT.readAccess (new Mutex.Action() { ... })
I just need to follow regular Java multithreaded programming practices, right?
Yes
It is only java compiler that will compile your Javacode from your NBP.
There are a number of classes that support multithreaded programming in the NetBeans RCP.
There are some interesting classes in the org.openide.util package that are related to threads and the RCP.
Most all these classes help you create threads and the like. You will still need to use java multithreaded programming practices after you have created them.
You may want to look at the Progress UI api, too.
精彩评论