IPhone operation queue VS Java Thread Pool

Source: Internet
Author: User

The author Sun Dongfeng 2011-1-12 reprinted, please indicate the source

 

Introduction

How to manage multi-threaded access to shared data and prevent deadlocks between threads is a very important topic when it involves multi-threaded concurrent operations. In Java, Java provides its own thread pool ThreadPoolExecutor class starting from Java 5. In iPhone, it provides the NSOperationQueue class for multi-thread 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

 

ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue <Runnable> 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

ThreadPoolExecutor threadPool =

New ThreadPoolExecutor (2, 4, 3, TimeUnit. SECONDS, new ArrayBlockingQueue <Runnable> (3), new ThreadPoolExecutor. DiscardOldestPolicy ());

 

For (int I = 1; I <= 5; I ++)

{

Try

{

String task = "task @" + I;

System. out. println ("put" + task );

ThreadPool.exe cute (new ThreadPoolTask ());

}

}

Catch (Exception e)

{

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:

Public class ThreadPoolTask implements Runnable {

ThreadPoolTask (){}

Public void run (){

System. out. println ("start execute ");

}

}

 

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:

@ Interface ThreadPoolTask: NSOperation

{

}

@ End

 

@ Implementation ThreadPoolTask

-(Void) main

{

NSLog (@ "start execute ");

}

@ 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:

 

NSOperationQueue * threadPool = [[NSOperation alloc] init];

[ThreadPool setMaxConcurrentOperationCount: 4];

For (int I = 1; I <= 5; I ++)

{

NSString * task = [NSString stringWithFormat: @ "task % d", I];

NSLog (@ "put % @", task );

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.

 

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.