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
- 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.execute(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.
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!