A Java Developer’s Guide to Threads on iPhone

Java makes it ridiculously simple to run code in threads and ensure that your objects are thread safe. When developing user experiences, threads enable you to do things in the “background” (like downloading and parsing content) without rendering your application unresponsive and giving the user the sense that the application may have “frozen”. The value of threads apply just a much to an iPhone application.

In Java, there are three key things you need to know about to work effectively with threads …

  1. java.lang.Thread
  2. java.lang.Runnable
  3. synchronized (…) { … }

… interestingly, in Objective-C there are almost the same constructs …

  1. NSThread
  2. @synchronized (…) { … }

The key difference is the absence of the “Runnable” interface. This may be misleading at first until you recognize that Objective-C frameworks often don’t declare formal protocols for their delegates … in fact, using Objective-C, you can pass in the method to be executed by the thread without it having to be wrapped in a formal class.

Let’s consider a simple example in Java. First you would begin by defining a class that implemented the Runnable interface.

1
2
3
4
5
6
7
8
9
10
public class MyClass implements Runnable {
 
    public void startMyThread() {
      Thread myThread = new Thread(this);
      myThread.start();
    }
    public void run() {
        /* Do your threaded code here */
    }
}

In Objective-C we would do this all like this …

1
2
3
4
5
6
7
8
9
10
11
12
13
@implementation 
- (void) startMyThread {
  [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
}
 
- (void) run {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
   /* Do your threaded code here */
 
  [pool release];
} 
@end

Some of the things to note about the Objective-C side of this are:

  • You must always specify a NSAutoreleasePool and release it for each thread you run
  • You can pass any method to NSThread, it doesn’t have to be called “run”
  • You can even pass in parameters to the “run” method unlike the Java equivalent

There is a lot more to learn about NSThread and I encourage you to take a look. In my opinion it has some features that are better than the Java equivalent. In a future post I’ll cover off on how to make you classes thread-safe using the @synchronized() {..} block. For those of you already familiar with Java, @synchronized is exactly like its Java counter part.

  1. Thanks for the article.

    One thing: should there be a colon after ‘run’ when handing it off as a selector?

    e.g.

    @selector(run:)

    instead of

    @selector(run)

    ?

  2. @John,

    Yes you are correct. Thanks for catching that! Should be correct now.

    I stand corrected … because the run method doesn’t take any arguments, there should be no ‘:’.

    For example, had the method signature looked like:

    – (void) run:(NSString *)someValue;

    then and only then would the selector look like:

    @selector(run:)

    and to further illustrate the usage, consider the following three run methods and their respective selectors …

    – (void) run; // @selector(run)
    – (void) run:(NSString *)someValue; // @selector(run:)
    – (void) run:(NSString *)someValue someOtherValue:(NSString *)value; // @selector(run:someOtherValue:)

  3. I’m the only one in this world. Can please someone join me in this life? Or maybe death…

  4. Thank you! I never leave comments.. but I just had to say thanks for this one!

  5. Thanks for this. Very handy. However…..

    In you comment you said you can do this:

    – (void) run:(NSString *)someValue someOtherValue:(NSString *)value;
    // @selector(run:someOtherValue:)

    However, NSThread documentation:
    + (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument

    …reads that you can only pass one argument into the thread.

    Thanks

  6. Appreciating the hard work you put into your site and detailed information you provide. It’s nice to come across a blog every once in a while that isn’t the same unwanted rehashed material. Great read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.

  7. Very lovely description. One things I want to ask can we run same thread after 1mnt of time regularly?

Comments are closed.