It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to create a queue that executes asynchronously, but the order in which the tasks execute must be serial
dispatch_queue_t queue;
queue = dispatch_queue_create("com.test.app", NULL); //create a serial queue can either be null or DISPATCH_QUEUE_SERIAL
dispatch_async(queue,
^{
//Mycode for doing background http
}
I think that you are doing it correctly,
dispatch_queue_t queue;
queue = dispatch_queue_create("com.test.app", NULL); //create a serial queue can either be null or DISPATCH_QUEUE_SERIAL
dispatch_async(queue,
^{
task1;
task2;
...
all this tasks wil be done done sequentially
}
Related
How can I run a loop in the background such as:
while(1==1){
NSLog(#"hello");
}
while being able to detect a button click such as:
- (IBAction)button:(id)sender {
//do something
}
You can use GCD to run the code on a background thread.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
});
Read Apple's Concurrency Programming Guide and maybe Threading Programming Guide.
The first of these will introduce you to operation queues (NSOperation) and dispatch queues (GCD), the second to threads (NSThread & Posix).
If after reading the guide you are unsure which of the approaches to take, at least for the situation you raise above, consider GCD first and then operation queues.
If you get stuck implementing your solution ask a new question, showing your code and explaining your issues. Somebody will undoubtedly help you out.
HTH
This question already has answers here:
dispatch_sync() always execute block in main thread
(3 answers)
Closed 6 years ago.
People are asking what is the way to run task in an thread. One of the answer accepted by many people(from the link) is to use GCD.
So, I tried in this way by printing out thread id to see if the task is really executed in another thread after put into GCD queue:
// a function in MyService class
- (void) doTask {
NSLog(#"start do task on thread = %#", [NSThread currentThread]);
dispatch_queue_t queue = dispatch_queue_create("com.company.myqueue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
NSLog(#"execute task on thread = %#", [NSThread currentThread]);
});
}
I run :
// this is not main thread
[myService doTask]
The console output is this:
start do task on thread = <NSThread: 0x15b4be00>{number = 6, name = (null)}
execute task on thread = <NSThread: 0x15b4be00>{number = 6, name = (null)}
It looks like the task is NOT executed in another thread, it is on the same thread as the caller thread. Do I misunderstand something? Why GCD doesn't execute task in a separate thread but many pepole accept the answer in that link?
GCD optimizes when it can. Running a block synchronously never requires switching threads, with the sole exception of dispatching to the main queue, as that must run on the main thread.
Try below code which i have taken from the Accepted answer which you have mentioned in your question.
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Add code here to do background processing
//
NSLog(#"start do task on thread = %#", [NSThread currentThread]);
dispatch_async( dispatch_get_main_queue(), ^{
// Add code here to update the UI/send notifications based on the
// results of the background processing
NSLog(#"execute task on thread = %#", [NSThread currentThread]);
});
});
It will print both different thread. In your test case which you have put in your question, Thread is same and it is synchronous approach. So, it is printing same thread. It is very broad concept to explain everything here. You should refer Apple Documentation for GCD.
Documents states about dispatch_sync,
Submits a block to a dispatch queue for synchronous execution. Unlike dispatch_async, this function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock.
Unlike with dispatch_async, no retain is performed on the target
queue. Because calls to this function are synchronous, it "borrows"
the reference of the caller. Moreover, no Block_copy is performed on
the block.
> As an optimization, this function invokes the block on the current
thread when possible.
Check this
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/dispatch_get_global_queue.3.html
It says that
Queues are not bound to any specific thread of execution and blocks submitted to independent queues may execute concurrently.
So thread selection of performing task is decide by thread pulling algorithms used by dispatch lib. but it give guarantees that queues execution is done by given configuration of queue(serial or concurrent and other)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm writing a program for parsing xml file I need to continually read the file that means if any change happens to the xml file at run time it must shown in the simulator also
I tried
NSThread *myThread =[[NSThread alloc]initWithTarget:self selector:#selector(doParsing) object:nil];
[myThread start];
and it calls the xml file but its not showing the change .How can I achieve that
Possibly something like:
NSThread *myThread =[[NSThread alloc]initWithTarget:self selector:#selector(callTimer) object:nil];
[myThread start];
- (void)callTimer
{
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(doParsing)
userInfo:nil
repeats:YES];
}
- (void)doParsing
{
//Do the stuff here
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
how to get a field press event in blackberry。
I write a component extends Field class , want to get a press event( press down still not press up ) .
please help me.
You can override this method:
protected boolean keyDown(int keycode, int time)
About the key up presses, there's a keyUp which is the method you should override to perform an action when the key is released instead of pressed (you can press and hold, and the action doesn't normally get fired until released). However, the OS doesn't callback on key up events unless it is explicitly told to do so by your app, for performance reasons.
You can overide keydown method of any field variable , or you can override navigationClick method of click
public ButtonField btn = new ButtonField("Hello");
protected boolean navigationClick(int status, int time)
{
Field f1 = getFieldWithFocus().getLeafFieldwithFocus()
if(f1 == btn)
{
<Perform your business logics>
}
Thanks
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
My servlet on receiving first request from client:
//1.
ObjectInputStream in=new ObjectInputStream(req.getInputStream());
String r=(String) in.readObject();
in.close();
ObjectOutputStream writer = new ObjectOutputStream(resp.getOutputStream());
writer.writeObject(pk);
writer.close();
//ON receiving second request:
//2.
ObjectInputStream in1=new ObjectInputStream(req.getInputStream());
String s=(String) in1.readObject();
in1.close();
OutputStreamWriter writer1 = new OutputStreamWriter(resp.getOutputStream());
writer1.write(reverseStr);
writer1.close();
But on both request only 1. is getting executed. why?
I asked this question without actually studying servlets. A servlet's method(doPost(),doGet() etc) are each called time a request is made to the servlet. Only the init() method is initialized or executed only once.
So each time a call is made to the above servlet, it starts executing from #line1.