I. Differences between processes and threads
Processes and threads are the basic units for running programs that the operating system understands. The system uses this basic unit to realize the system's concurrency for applications. The difference between a process and a thread is: in short, a program has at least one process, and a process has at least one thread. The division scale of the thread is smaller than that of the process, making the multi-thread program highly concurrent. In addition, the process has independent memory units during execution, and multiple threads share the memory, which greatly improves the program running efficiency. The execution process of a thread is different from that of a process. Each Independent thread has a program running entry, sequence execution sequence, and program exit. But the thread cannot be executed independently. It must exist in the application and the application provides multiple thread execution control. Logically, multithreading means that multiple execution parts in an application can be executed simultaneously. However, the operating system does not view multiple threads as multiple independent applications to implement process scheduling, management, and resource allocation. This is an important difference between processes and threads.
II. Implementation results
Click the button to start the progress bar. The label value after the result is displayed changes with the progress bar.
The button disappears while moving forward, and appears again after reading it.
Iii. Code
1. Add attributes:
A label output port, a UIProgressView, a button attribute, and button action Methods
# Import
@ Interface WeFirstThreadViewController: UIViewController
@ Property (retain, nonatomic) IBOutlet UILabel * valueText;
@ Property (retain, nonatomic) IBOutlet UIProgressView * myViewProgress;
@ Property (retain, nonatomic) IBOutlet UIButton * startBtn;
-(IBAction) startBtnAction :( id) sender;
@ End
2. method implementation
// View control Initialization
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Progress bar is initialized to 0
Self. myViewProgress. progress = 0;
// The hidden button is initialized to not hidden
Self. startBtn. hidden = NO;
}
// Method corresponding to the button
-(IBAction) startBtnAction :( id) sender
{
Self. startBtn. hidden = YES;
// Create a thread
[NSThread detachNewThreadSelector: @ selector (startBackgroundJob :) toTarget: self withObject: nil];
}
-(Void) startBackgroundJob :( id) sender
{// Start thread
[Self initialize mselecw.mainthread: @ selector (updateProgressView :) withObject: nil waitUntilDone: NO];
}
// Update data
-(Void) updateProgressView :( id) sender
{
Self. myViewProgress. progress + = 0.1;
Float value = self. myViewProgress. progress;
If (value <1)
{// Sleep for 0.5 seconds, called once
[NSTimer scheduledTimerWithTimeInterval: 0.5 target: self selector: @ selector (updateProgressView :) userInfo: nil repeats: NO];
}
Else
{
Self. startBtn. hidden = NO;
}
// Display the data on the label to update the data
Self. valueText. text = [NSString stringWithFormat: @ "%. 2f", value];
}
@ End
Demo related: http://download.csdn.net/detail/u012887301/6777319