Parse iPhone operation queue and Java Thread Pool

Source: Internet
Author: User

IPhone operation queueAndJava Thread PoolThis article describes how to manage multi-threaded access to shared data and prevent deadlocks between threads when concurrent operations are involved. InJavaLanguage, starting from Java 5,JavaProvides your own thread pool ThreadPoolExecutor class;IPhoneThe NSOperationQueue class is provided for multithreading management and scheduling.

What is a thread pool?

What is the thread pool? In fact, the thread pool principle is very simple, similar to the buffer concept in the operating system. Its typical execution process is as follows:

First, start several threads and put them in sleep state.

Second, when the client has a new request, the thread pool will wake up a sleep thread to process client requests.

Finally, when the request is processed, the thread is sleep.

Java Thread Pool

The thread pool can be implemented by programmers themselves. But starting from Java 5, the Java language comes with the thread pool class ThreadPoolExecutor, which provides a typical interface for thread pool management, to study the implementation of the ThreadPoolExecutor class.

The common construction method of the ThreadPoolExcutor class is

 
 
  1. ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> 
  2. workQueue,RejectedExecutionHandler handler) 

The corePoolSize parameter is the minimum number of threads maintained by the thread pool.

The maximumPoolSize parameter is the maximum number of threads maintained by the thread pool.

The keepAliveTime parameter is the idle time allowed by the thread pool maintenance thread.

The unit parameter is the unit of idle time allowed by the thread pool maintenance thread.

The workQueue parameter is the Buffer Queue used by the thread pool.

The handler parameter is the processing handle of the thread pool to reject tasks.

A task can be added to the thread pool through the excute (Runnable) method. A task is an object that implements the Runnable interface, and the thread pool is run () through the Runnable object () method to execute the task.

The typical usage is as follows:

First, construct a thread pool

 
 
  1. ThreadPoolExecutor threadPool =  
  2. new ThreadPoolExecutor(2,4,3,TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(3),new ThreadPoolExecutor.DiscardOldestPolicy());  
  3. for(int i = 1;i <= 5;i++)  
  4. {  
  5.  
  6.      try  
  7. {  
  8.      String task = “task@”+i;  
  9.      System.out.println(“put”+task);  
  10.      threadPool.execute(new ThreadPoolTask());  
  11. }  
  12. }  
  13. catch(Exception e)  
  14. {  
  15.      e.printStackTrace();  

The Runnable interface must be implemented for the task object to be executed in the thread pool. The run () method of the task object is called when the thread pool executes the task object. Its implementation code is as follows:

 
 
  1. public class ThreadPoolTask implements Runnable{  
  2. ThreadPoolTask(){}  
  3. public void run(){  
  4.      System.out.println(“start execute”);  
  5. }  

IPhone operation queue

The iPhone also supports multi-threaded development. Similarly, the NSThread class also faces multi-threaded shared data management and deadlock issues when providing multi-threaded development support, therefore, the iPhone also provides a solution similar to the Java thread pool: task queue NSOperationQueue class.

Like the Runnable interface in Java, the iPhone provides the NSOperation interface to encapsulate task objects. By adding task objects to the NSOperationQueue queue, the NSOperationQueue queue allocates threads to execute task objects, the task object is executed using the-(void) main method. The following describes the implementation of typical task objects and task Queues:

 
 
  1. @interface ThreadPoolTask:NSOperation  
  2. {  
  3. }  
  4. @end  
  5.  
  6. @implementation ThreadPoolTask  
  7. - (void)main  
  8. {  
  9.   NSLog(@”start execute”);  
  10. }  
  11. @end 

Like in Java, a multi-thread pool is constructed and the task object is added to the thread pool. The thread pool calls the-(void) main method of the task object to execute the task, the typical Implementation of task queue in iPhone is as follows:

 
 
  1. NSOperationQueue* threadPool = [[NSOperation alloc] init];  
  2. [threadPool setMaxConcurrentOperationCount:4];  
  3. for(int i = 1;i <= 5;i++)  
  4. {         
  5. NSString* task = [NSString stringWithFormat:@”task %d”,i];  
  6. NSLog(@“put %@”,task);  
  7. [threadPool add:([[ThreadPoolTask alloc] init])];  

We can see that the iPhone provides a mechanism similar to the thread pool through NSOperationQueue, through which multi-thread concurrent operations can be performed more conveniently, this frees programmers from complicated multi-threaded shared data management and deadlock issues.

Summary: The content of parsing the iPhone operation queue and Java thread pool has been introduced. I hope this article will be helpful to you!

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.