Multi-threading usage in Eclipse client programs

Source: Internet
Author: User
Tags thread thread class


Introduction: This paper analyzes the implementation of the multi-thread program in Eclipse, discusses the application of multithreading in the development of Eclipse client programs and the problems to be noticed, and also discusses some debugging and problem-solving methods of multithreaded programming.



Eclipse, as a development platform, is increasingly used, and more and more client programs are developed based on the Eclipse Rich-client platform. In today's increasingly complex application environment, our client programs are inevitably multitasking. An excellent client program allows users to start multiple tasks at the same time, greatly improving the user's productivity and user experience. In this article, let's talk about how multitasking is done in eclipse.



In our eclipse-based Java programs, we have a number of ways to provide a multitasking implementation. A friend who is familiar with Java will immediately think of the Java thread class, which is one of the most common Java-multitasking classes. The Eclipse platform provides its own API for multitasking, which is job and uijob. The job in Eclipse is a encapsulation of Java thread, providing a more convenient interface for multitasking. The following are the basic uses of the job:



Listing 1. Job Usage Examples



Job job = new Job(“Job Name”){
protected IStatus run(IProgressMonitor monitor) {

     // Add your task code here
     return Status.OK_STATUS;
   }
};
job.schedule(delayTime);



We also often use display.asynchexec () and Display.synchexec () in Eclipse to start the execution of tasks. These two methods are mainly to facilitate our task of interface operation. The following is the use of display.asynchexec (), display.synchexec () and similar.



Listing 2. Display.synchexec () Usage Example



Display.getDefault().asyncExec(new Runnable() {
public void run() {
   // Add your task code here
     }
   });



Typically, in eclipse, it is best to use the job interface provided by eclipse to implement multitasking, rather than using Java thread. Why, then? There are several main reasons for this:



A job is a reusable unit of work, a job we can easily let it execute multiple times.



The job provides a convenient interface that allows us to communicate with the outside world and report the current execution progress.



Eclipse provides a mechanism that allows programmers to easily intervene in job scheduling, such as we can easily implement a job with only one type running at a time



Eclipse defaults to a job management program that can view all current jobs and their progress, as well as UI termination, pausing, and continuing with the specified job



Using a job can improve the performance of your program and save the overhead of thread creation and destruction. The job in eclipse encapsulates the implementation of the thread pool. When we start a job, Eclipse does not create a new thread at once, it will look for idle threads in its thread pool, and if there are idle threads, it will run your job directly with the idle thread. When a job terminates, its corresponding thread does not terminate immediately, and it is returned to the thread pool for reuse. In this way, we can save the cost of creating and destroying threads



Here are a few ways to discuss the implementation and usage aspects of the job in eclipse.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.