Primer for Python Decorators

In this introductory tutorial, we’ll look at what decorators are and how to create and use them. Decorators provide a simple syntax for calling higher-order functions. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it. Sounds confusing – but it’s really not, especially after we go over a number of examples.

python decorators

Functions

Before you can understand decorators, you must first understand how functions work. Essentially, functions return a value based on the given arguments.

def foo(bar):
    return bar + 1


print(foo(2) == 3)

First Class Objects

In Python, functions are first-class objects. This means that functions can be passed around, and used as arguments, just like any other value (e.g, string, int, float).

def foo(bar):
    return bar + 1

print(foo)
print(foo(2))
print(type(foo))


def call_foo_with_arg(foo, arg):
    return foo(arg)

print(call_foo_with_arg(foo, 3))

Nested Functions

Because of the first-class nature of functions in Python, you can define functions inside other functions. Such functions are called nested functions.

def parent():
    print("Printing from the parent() function.")

    def first_child():
        return "Printing from the first_child() function."

    def second_child():
        return "Printing from the second_child() function."

    print(first_child())
    print(second_child())

What happens when you call the parent() function? Think about this for a minute. You should get…

Printing from the parent() function.
Printing from the first_child() function.
Printing from the second_child() function

Try calling the first_child(). You should get an error:

Traceback (most recent call last):
File "decorator03.py", line 15, in <module>
first_child()
NameError: name 'first_child' is not defined

What have we learned?

Whenever you call parent(), the sibling functions, first_child() and second_child() are also called AND because of scope, both of the sibling functions are not available (e.g., cannot be called) outside of the parent function.

Returning Functions

Python also allows you to return functions from other functions. Let’s alter the previous function for this example.

def parent(num):

    def first_child():
        return "Printing from the first_child() function."

    def second_child():
        return "Printing from the second_child() function."

    try:
        assert num == 10
        return first_child
    except AssertionError:
        return second_child

foo = parent(10)
bar = parent(11)

print(foo)
print(bar)

print(foo())
print(bar())

The output of the first two print statements is:

<function first_child at 0x1004a8c08>
<function second_child at 0x1004a8cf8>

This simply means that foo points to the first_child() function, while bar points to the second_child() function.

The output of the second two functions confirms this:

Printing from the first_child() function.
Printing from the second_child() function.

Finally, did you notice that in example three, we executed the sibling functions within the parent functions – e.g, second_child(). Meanwhile in this last example, we did not add parenthesis to the sibling functions – first_child – when called so that way we can use them in the future. Make sense?

Now, my friend, you are ready to take on decorators!

Decorators

Let’s look at two examples …

Example 1:

def my_decorator(some_function):

    def wrapper():

        print("Something is happening before some_function() is called.")

        some_function()

        print("Something is happening after some_function() is called.")

    return wrapper


def just_some_function():
    print("Wheee!")


just_some_function = my_decorator(just_some_function)

just_some_function()

Can you guess what the output will be? Try.

Something is happening before some_function() is called.
Wheee!
Something is happening after some_function() is called.

To understand what’s going on here, just look back at the four previous examples. We are literally just applying everything learned. Put simply, decorators wrap a function, modifying its behavior.

Let’s take it one step further and add an if statement.

Example 2:

def my_decorator(some_function):

    def wrapper():

        num = 10

        if num == 10:
            print("Yes!")
        else:
            print("No!")

        some_function()

        print("Something is happening after some_function() is called.")

    return wrapper


def just_some_function():
    print("Wheee!")

just_some_function = my_decorator(just_some_function)

just_some_function()

This will output in:

Yes!
Wheee!
Something is happening after some_function() is called.

Syntactic sugar!

Python allows you to simplify the calling of decorators using the @ symbol (this is called “pie” syntax).

Let’s create a module for our decorator:

def my_decorator(some_function):

    def wrapper():

        num = 10

        if num == 10:
            print("Yes!")
        else:
            print("No!")

        some_function()

        print("Something is happening after some_function() is called.")

    return wrapper


if __name__ == "__main__":
    my_decorator()

Okay. Stay with me. Let’s look at how to call the function with the decorator:

from decorator07 import my_decorator


@my_decorator
def just_some_function():
    print("Wheee!")

just_some_function()

When you run this example, you should get the same output as in the previous one:

Yes!
Wheee!
Something is happening after some_function() is called.

So, @my_decorator is just an easier way of saying just_some_function = my_decorator(just_some_function). It’s how you apply a decorator to a function.

Real World Examples

How about a few real world examples …

import time


def timing_function(some_function):

    """
    Outputs the time a function takes
    to execute.
    """

    def wrapper():
        t1 = time.time()
        some_function()
        t2 = time.time()
        return "Time it took to run the function: " + str((t2 - t1)) + "\n"
    return wrapper


@timing_function
def my_function():
    num_list = []
    for num in (range(0, 10000)):
        num_list.append(num)
    print("\nSum of all the numbers: " + str((sum(num_list))))


print(my_function())

This returns the time before you run my_function() as well as the time after. Then we simply subtract the two to see how long it took to run the function.

Run it. Work through the code, line by line. Make sure you understand how it works.

from time import sleep


def sleep_decorator(function):

    """
    Limits how fast the function is
    called.
    """

    def wrapper(*args, **kwargs):
        sleep(2)
        return function(*args, **kwargs)
    return wrapper


@sleep_decorator
def print_number(num):
    return num

print(print_number(222))

for num in range(1, 6):
    print(print_number(num))

This decorator is used for rate limiting. Test it out.

One of the most used decorators in Python is the login_required() decorator, which ensures that a user is logged in/properly authenticated before s/he can access a specific route (/secret, in this case):

from functools import wraps
from flask import g, request, redirect, url_for


def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if g.user is None:
            return redirect(url_for('login', next=request.url))
        return f(*args, **kwargs)
    return decorated_function


@app.route('/secret')
@login_required
def secret():
    pass

Did you notice that the function gets passed to the functools.wraps() decorator? This simply preserves the metadata of the wrapped function.

Let’s look at one last use case. Take a quick look at the following Flask route handler:

@app.route('/grade', methods=['POST'])
def update_grade():
    json_data = request.get_json()
    if 'student_id' not in json_data:
        abort(400)
    # update database
    return "success!"

Here we ensure that the key student_id is part of the request. Although this validation works it really does not belong in the function itself. Plus, perhaps there are other routes that use the exact same validation. So, let’s keep it DRY and abstract out any unnecessary logic with a decorator.

from flask import Flask, request, abort
from functools import wraps

app = Flask(__name__)


def validate_json(*expected_args):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            json_object = request.get_json()
            for expected_arg in expected_args:
                if expected_arg not in json_object:
                    abort(400)
            return func(*args, **kwargs)
        return wrapper
    return decorator

@app.route('/grade', methods=['POST'])
@validate_json('student_id')
def update_grade():
    json_data = request.get_json()
    print(json_data)
    # update database
    return "success!"

In the above code, the decorator takes a variable length list as an argument so that we can pass in as many string arguments as necessary, each representing a key used to validate the JSON data. Did you notice that this dynamically creates new decorators based on those strings? Test this out.

Cheers!

Apple TV 2 XBMC Install / Kodi Downgrade

So you have a Jailbroken Apple TV 2 and want install a specific version of XBMC or Kodi, this mini tutorial will show you how to do so.

You might be wondering why would you not simply do:

  • apt-get update
  • apt-get remove org.xbmc.kodi-atv2

That would install the latest version of Kodi, which normally wouldn’t be a problem. However development of Kodi for the Apple TV 2 platform has come to an end, so eventually you might no longer be able to do this. Another reason might be you want to boot in to boot Kodi / XBMC directly when the Apple TV is powered on for ease of use by family members, this is not possible with the latest version of Kodi.

First ssh in to your Jailbroken Apple TV, the default username is root and the default password is alpine.

Now do: apt-get update – This updates the packages available from the repositories, it wont hurt to do this.

Remove Previous versions of XBMC / Kodi:

Work you way down the list below and copy / paste the commands which are highlighted in bold to remove and clean-up from any previous Xbmc / Kodi installs.

rm -f *kodi*.deb – Deletes any previous Kodi .deb files you might have downloaded

rm -f *xbmc*.deb – Deletes any previous XBMC .deb files you might have downloaded

apt-get remove org.xbmc.xbmc-atv2 – removes XBMC if you have installed it from the repository

apt-get remove org.xbmc.kodi-atv2 – removes Kodi if you have installed it from the repository

rm -Rf /private/var/mobile/Library/Preferences/XBMC – Removes any previous user settings for XBMC

rm -Rf /private/var/mobile/Library/Preferences/Kodi – Removes any previous user settings for Kodi

Installing a specific version of XBMC or Kodi:

You can download any past version of XBMC / Kodi you desire here, the Vim.org repository has all of the past XBMC / Kodi versions at the moment.

So if for example if you wanted to install the final version of XBMC which is 13.2 Gotham I would do the following:

wget http://ftp.vim.org/mediaplayer/xbmc/apt/atv2/deb/org.xbmc.xbmc-atv2_13.2-0_iphoneos-arm.deb – this would download the XBMC 13.2 Gotham version to my Apple TV. If i wanted to download a different version i would simply type wget then the address of the deb I wished to download.

dpkg -i *xbmc*.deb – This will install the XBMC 13.2 Gotham version i just downloaded.

If you get error about the dependency then do:  apt-get -f install

apt-get install org.tomcool.xbmc-booter – This installs the XBMC booter which automatically boots in to XBMC instead of the default Apple TV 2 user interface. Note: Do not install this if you wish to use Kodi, your Apple TV will siply boot to a black screen. However you can simply ssh in to the Apple TV again and type apt-get remove org.tomcool.xbmc-booter to remove the XBMC booter

reboot – Reboots the Apple TV

Once rebooted you should be presented with a clean install of your chosen version of XBMC / Kodi.

Here is a simple list of commands for your copy + pasting pleasure:

apt-get update
rm -f *kodi*.deb
rm -f *xbmc*.deb
apt-get remove org.xbmc.xbmc-atv2
apt-get remove org.xbmc.kodi-atv2
rm -Rf /private/var/mobile/Library/Preferences/XBMC
rm -Rf /private/var/mobile/Library/Preferences/Kodi
wget http://ftp.vim.org/mediaplayer/xbmc/apt/atv2/deb/org.xbmc.xbmc-atv2_13.2-0_iphoneos-arm.deb
dpkg -i *xbmc*.deb
apt-get install org.tomcool.xbmc-booter
reboot

Hopefully someone will find this useful sometime in the future to downgrade or simply install XBMC on their Apple TV 2. I personally wanted to downgrade to XBMC 13.2 Gotham because I wanted the Apple TV to automatically boot in to XBMC for ease of use by family members.

SSH Tunneling with Putty

Let say if you have the remote machine that running the web server on the certain port but that port is blocked from outside. You can set up the ssh tunnel to access to that web server from desktop using the steps below:

Requirements:
– You must have access to remote machine with ssh ( i.e. you can login to ssh with your credential)
– Install Putty on your desktop

Steps:

1/ Set up the putty session for your remote machine ( ip address, port , etc.)
2/ Go to SSH – Tunnels then enter your choice for the Source port ( it can be any random port)
3/ Enter 127.0.0.1: in the Destination where is the port which is blocked on the remote machine

Capture23/ Save the configuration and run the ssh session from your desktop.

4/ Go to the browser and access the remote site with the url : http://localhost:8111  ( In this case 8111 is my random port)

Woala !!!

ECEF (Earth Centered Earth Fixed) Coords with WGS-4 Ellipsoid to Latitude Longitude Altitude

/*
*
* ECEF - Earth Centered Earth Fixed
*
* LLA - Lat Lon Alt
*
* ported from matlab code at
*/

// WGS84 ellipsoid constants
private final double a = 6378137; // radius
private final double e = 8.1819190842622e-2; // eccentricity

private final double asq = Math.pow(a,2);
private final double esq = Math.pow(e,2);

private double[] ecef2lla(double[] ecef){
double x = ecef[0];
double y = ecef[1];
double z = ecef[2];

double b = Math.sqrt( asq * (1-esq) );
double bsq = Math.pow(b,2);
double ep = Math.sqrt( (asq – bsq)/bsq);
double p = Math.sqrt( Math.pow(x,2) + Math.pow(y,2) );
double th = Math.atan2(a*z, b*p);

double lon = Math.atan2(y,x);
double lat = Math.atan2( (z + Math.pow(ep,2)*b*Math.pow(Math.sin(th),3) ), (p – esq*a*Math.pow(Math.cos(th),3)) );
double N = a/( Math.sqrt(1-esq*Math.pow(Math.sin(lat),2)) );
double alt = p / Math.cos(lat) – N;

// mod lat to 0-2pi
lon = lon % (2*Math.PI);

// correction for altitude near poles left out.

double[] ret = {lat, lon, alt};

return ret;
}

private double[] lla2ecef(double[] lla){
double lat = lla[0];
double lon = lla[1];
double alt = lla[2];

double N = a / Math.sqrt(1 – esq * Math.pow(Math.sin(lat),2) );

double x = (N+alt) * Math.cos(lat) * Math.cos(lon);
double y = (N+alt) * Math.cos(lat) * Math.sin(lon);
double z = ((1-esq) * N + alt) * Math.sin(lat);

double[] ret = {x, y, z};
return ret;
}

Python How To Check the Point is inside the Polygon


# determine if a point is inside a given polygon or not
# Polygon is a list of (x,y) pairs

def point_inside_polygon(x, y, poly):
n = len(poly)
inside = False

p1x, p1y = poly[0]
for i in range(n + 1):
p2x, p2y = poly[i % n]
if y > min(p1y, p2y):
if y <= max(p1y, p2y):
if x <= max(p1x, p2x):
if p1y != p2y:
xinters = (y – p1y) * (p2x – p1x) / (p2y – p1y) + p1x
if p1x == p2x or x <= xinters:
inside = not inside
p1x, p1y = p2x, p2y

return inside

SSH Keying through PuTTY on Windows or Linux

Here we will go over how to setup SSH keys using PuTTY, a popular SSH client for Windows. There is also a Linux version as well.


1. Download PuTTY and PuTTYgen

First you will need to download PuTTY and PuTTYgen. You can get the latest version for both at the following links:

  • PuTTY: the Telnet and SSH client itself (Windows x86)
  • PuTTYgen: an RSA and DSA key generation utility (Windows x86)

The Unix/Linux version is available on the PuTTY download page.


2. Configure PuTTY

First things first, we need to create a profile in PuTTY to access our server. Run PuTTY and you’ll be shown the basic PuTTY options. In the Host Name field, enter your domain, or the IP address of your server. The port should already be default (22). However, if you’re connecting to your shared/reseller account, we use a non-standard port. In that event, change 22 to 2222.

Under Connection > Data, specify the username you wish to connect with under Auto-login username. If this is not root, it will most likely be your cPanel username.

Click back over to Session (top of the left menu), type a name to identify your settings, i usually use my domain name, then click save. This will save the current configuration profile into PuTTY for later use.


3. Connect to the Server

Now that PuTTY is configured, we should connect to the server at this time. To do so, click Open. This will open the PuTTY session. You will prompted about a potential security breach. This is normal, click Yes. This should bring you to the bash prompt of your server. If you see something like the below, then everything worked.

gator100 ~ $

4. Generate the SSH Key

Now we need to generate the SSH key itself. To do this, fire up PuTTYgen. Make sure to select SSH-2 RSA under type of key to generate, and specify the bits to 1024 (this is default).

Go ahead and click on generate, then move the cursor around the blank area. PuTTYgen uses this movement to generate the key’s randomness.

Once this is generated, you can set a key comment, or a passphrase. The comment isn’t important, as it’s just a label, but the passphrase will require that you enter this passphrase when using the key. If you’re trying to have a ‘passwordless login’ then this should be left blank.

Now click Save Public Key, and save this file to a name of your choosing somewhere safe on your harddrive. Please remember the location of this file, as it is needed later. Then do the same for Save Private Key.


5. Get the Keys on the Server

Now that we have the keys generated and saved, we need to get the keys on the server. Copy the data inside the putty window under Public Key for pasting into SSH authorized keys file. It should look something like this. (Note that the below example is all on one line. Do not use 3 lines, as displayed in the example.)

ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAi5zTs+THmSa5rxfc3rYzVORk9neoefVZg1PZwSI
9vj/eg3UL5fg8ENCw9OGRm3R0t2FgfvpudWGkiNaOp1HWz3FamX7zZ4svqJHloYgpL
/0vzZynFEc2Hm2O024PLzy7G6H6GDTSuGxU1Ar7gluBiNDfR9SXcUD+CddliW2+zXc= user@domain.com

Please Note:When you paste the key, it needs to be all on one line, with no extra spaces or line returns.

Go back to the PuTTY session and run the following commands in order.

mkdir ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys2

This will bring up a text editor, simply right click in the putty window to paste the key you copied earlier. Now generally this will paste into three lines, you want it to be on one line. so go to the beginning of the second and third lines, and hit backspace once. This should move the entire string all into one line.

Once it’s all in one line, and looks good, hit CTRL+X, then Y, then Enter. This will save the file and return you back to the bash prompt. There, run the following command.

chmod 600 ~/.ssh/authorized_keys2

6. Configure SSH Keys to Work

Now we’re almost done. Close PuTTY, and re-open it, so we get back to the configuration menu. Click on the profile you saved earlier, and click load. This will load the profiles configuration into the current PuTTY window.

On the left hand side, click SSH > Auth. Here is the configuration for the key. Click on browse, and browse your harddrive for the private key. we saved earlier. Now go back to sessions (again, top left) and click save once more.


7. Confirm Setup is Successful

And you’re all set. To confirm that all this worked, simply click open again. PuTTY will attempt a connection with your server, send the key, and your server should accept it without error. You should be returned instantly to the bash prompt.

Disable Notification Center on Mountain Lion

This is the preferred method:

  1. Open Terminal and enter the following command:
    launchctl unload -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist
  2. Next type the following command to kill NotificationCenter:
    killall NotificationCenter
  3. Finally, quit out of Terminal and return to Finder

This unloads Notification Center for the current user only and does not require admin access. To re-enable Notifications with this approach:

  1. Launch Terminal and enter a similar command – notice load instead of unload:
    launchctl load -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist
  2. Hit Command+Shift+G and go to /System/Library/CoreServices/ then find “Notification Center” and double-click it to launch it again

This is the older approach that is less preferred, but it does work in OS X Mountain Lion:

  1. From the OS X Finder, hit Command+Shift+G and enter the path to /System/Library/CoreServices/
  2. Locate “Notification Center.app” and click on the name to rename it to “Notification Center-disabled.app”, authenticate the change when prompted
  3. Now launch Terminal, found in /Applications/Utilities/ and type the following command:
        killall NotificationCenter
  4. Quit out of Terminal

Java Concurrency: Queue Processing, Part 2

In Java Concurrency: Queue Processing, Part 1 (the first installment in this series), we explored how to coordinate a pool of worker threads listening on a queue, which was also the monitor object used to signal workers to process the queue. In this installment, we look at thejava.util.concurrent package. Specifically, we’ll explore how to coordinate the activities of multiple threads using the SychronousQueue class and other classes that implement theBlockingQueue interface.

The Hand Off

There are times when you need to process different actions in different threads, but they need to be coordinated using a hand-off pattern. In other words, you can have a child thread that does some work, and then sits and waits for more work from a queue. On the other side, you can have a producer thread that chugs along until it needs to hand-off a portion of its work activity to another thread. With a SynchronousQueue, both sides of the queue (consumer and producer) will block until there is a corresponding producer and consumer, respectively.

To be precise, a SynchronousQueue isn’t really a queue at all, since it doesn’t have any capacity. The requirement is that upon insertion into the queue, the producer will block until there is a consumer ready to pull a message off the queue. Similarly, a consumer will block until a producer places a message onto the queue; hence the hand-off pattern.

Fortunately, a SynchronousQueue is very easy to use; all of the complexity is implemented for you within Java’s concurrent collection classes. To illustrate, let’s look at a simple example. First, the worker (consumer) thread:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MyWorker extends Thread {
    private final BlockingQueue<String> queue;
    public MyWorker(BlockingQueue<String> queue) {
        this.queue = queue;
    }
 
    public void run() {
        try {
            while ( true ) {
                String s = queue.take();
                doWork(s);
            }
        }
        catch ( InterruptedException ie ) {
            // just terminate
        }
    }
    
}

The doWork() method has been omitted, but all it does in this example is output a simple message. With the SynchronousQueue class, consumers call the blocking take() method. Calls to remove() or peek() will result in an Exception or null respectively, always. Also, the call to take() will only return when a producer places something into the queue.

The producer code is straightforward as well:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class SyncQueueExample {
    // …   
    public SyncQueueExample() {
        try {
            int workItem = 0;
            // Create a synchronous queue
            BlockingQueue<String> queue = new SynchronousQueue<String>();
            // Create the child worker thread
            MyWorker worker = new MyWorker(queue);
            worker.start();
            // Start sending to the queue
            while ( true ) {
                System.out.println("\nPlacing work on queue");
                String work = "Work Item:" + (++workItem);
                queue.put(work);
            }
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
    }
}

When run, the output between the queue producer and consumer will be “coordinated,” in effect, and alternate as such:

1
2
3
4
5
6
7
8
9
10
11
12
Placing work on queue
Thread 'Thread-1' processing work: Work Item:1
Placing work on queue
Thread 'Thread-1' processing work: Work Item:2
Placing work on queue
Thread 'Thread-1' processing work: Work Item:3
Placing work on queue
Thread 'Thread-1' processing work: Work Item:4

To continue on the worker thread pool theme from the previous blog, you can create multiple queue consumers, each of which will block on the call to the SynchronousQueue‘s take()method until it’s chosen to process a message:

1
2
3
4
5
6
// Create child worker thread pool
int POOL_SIZE = 5;
for ( int i = 0; i < POOL_SIZE; i++ ) {
    MyWorker worker = new MyWorker(queue);
    worker.start();
}

This pattern is useful if processing each queued work request were to involve some lengthy operation, such as file IO. Since there can be many consumers waiting, the work queue producer would be free to continuously generate work even while an individual consumer took considerable time processing a single item. In this case, the producer thread would only be blocked on insertion when all consumer threads were busy processing. Once a worker thread completed its processing and once again called take(), the producer’s call to put() would unblock and the entire process would continue.

Hand Off, with Buffering

A similar scenario can be achieved with the ArrayBlockingQueue. In this implementation of the BlockingQueue interface, a producer can produce work on a queue until a certain number of outstanding queued items exist, at which point it will block. Until that number is reached, however, the producer will be free to continue producing work. As with a SychronousQueue, calls to take() by a consumer will block if there are no items on queue, but will return if and when queued items exist.

As for the consumer worker threads, the MyWorker class remains exactly the same as in the previous example. Since both SychronousQueue and ArrayBlockingQueue are instances of the BlockingQueue interface, no code change is required — this is polymorphism in action.

Regarding the producer, the code from the previous example is mostly unchanged, except the single line where a SynchronousQueue was created. Instead, we need to create anArrayBlockingQueue, where a number is provided in the constructor:

1
2
3
// Create a synchronous queue
int MAX_Q_SIZE = 5;
BlockingQueue<String> queue = new ArrayBlockingQueue<String>(MAX_Q_SIZE);

This number indicates the maximum size of the queue, which is the number of items that can be placed on the queue before the producer blocks on calls to put(). Once consumers remove items and the queue size drops below this maximum, the producer’s call to put() will return. Depending on OS thread scheduling as well as whether you specify the “fairness” of the queue’s consumer processing, you will see varied output in terms of messages being placed on the queue and when they are processed:

1
2
3
4
5
6
7
8
9
10
11
12
...
Placing work on queue
Placing work on queue
Placing work on queue
Thread 'Thread-1' processing work: Work Item:6
Thread 'Thread-2' processing work: Work Item:7
Thread 'Thread-3' processing work: Work Item:8
Thread 'Thread-4' processing work: Work Item:9
Thread 'Thread-5' processing work: Work Item:10
Placing work on queue
Placing work on queue
...

Priority, Delays, and Other Details

There are multiple queue classes in the java.util.concurrent package that implement theBlockingQueue interface (we’ve examined the first two in detail here):

  • SynchronousQueue: A hand-pattern. Producer(s) block until there is a consumer available; consumer(s) block until there is an enqueued message.
  • ArrayBlockingQueue: Similar to the SynchronousQueue, except the queue can contain a pre-set number of items before the queue producer(s) block on queue insertion.
  • DelayQueue: Elements placed in the queue are not available for removal until their delay time has expired. Items that are furthest past their expiration time are available for removal first. Calls to put() do not block since this queue is unbounded, although calls to take()will block until an expired message is available.
  • LinkedBlockingQueue: Similar to an ArrayBlockingQueue, but where queue growth is allowed. However, the queue constructor does accept an optional parameter to constrain the growth to a maximum number of items.
  • LinkedTransferQueue: Similar to SynchronousQueue, except the queue is unbounded in size and calls to put() never block. Calls to take(), however, will block until a queued item is available for removal.
  • PriorityBlockingQueue: Similar to SynchronousQueue, except the queue is unbounded in size and calls to put() never block. Calls to take(), however, will block until a queued item is available for removal, and those items are subject to priority-based ordering. Enqueued objects must be comparable, meaning they must implement the Comparableinterface and required methods.

In part 3 of this series on Java concurrency, we’ll continue with the java.util.concurrent package and explore Executors and the ExecutorService class.

Java Concurrency: Queue Processing, Part 1

The Queue as Monitor

Every Java class extends the Object class by default. There are several built-in methods on this class, of which we’ll focus on wait()notify(), and notifyAll(). These methods operate on the object’s monitor, which is used to synchronize operations across a group of threads. There are some rules to follow, but it’s really quite simple: The waiting thread needs to own the object’s monitor, which means it must call wait() within the code that synchronizes on the monitor. There are three ways of doing this, reference the Java API for more details. Here’s one example:

1
2
3
4
5
6
synchronized (obj) {
     while ( condition ) {
         obj.wait();
         // perform some action
     }
}

Conversely, the thread that signals the waiting thread(s) will call notify()or notifyAll() on the same monitor object. It, too, needs to own the monitor to do this, as in this example:

1
2
3
synchronized (obj) {
    obj.notify();
}

Calling notify() wakes up a single thread waiting on the monitor, while notifyAll() wakes up all of the threads waiting on the monitor. In general, calling notify()is more efficient as it results in fewer thread context switches, but there are times where notifyAll() may be a better choice. For instance, when work is produced at a high rate, it may be more efficient to wake as many threads as possible each time work is placed on the queue. This is mainly an implementation decision that you need to make.

Multithreaded Queue Example

Let’s examine a common scenario in which tasks are executed by worker threads, and where the work is coordinated via a queue to ensure once-and-only-once execution of each task. In this case, we’ll use the queue (itself a Java Object) as the monitor as well. Here’s the overall design:

  • Create a queue to act as a monitor and a way to transfer work
  • Create a pool of worker threads (consumers) that each wait on the monitor (the queue)
  • Create one or more producers that place items on the queue
  • Notify the monitor when an item is placed on the queue, which in turn wakes up a worker to pull the next item off the queue

Let’s start by looking at the worker’s class, MyWorker, which itself extends Thread:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class MyWorker extends Thread {
    private static int instance = 0;
    private final Queue<String> queue;
    
    public MyWorker(Queue queue) {
        this.queue = queue;
        setName("MyWorker:" + (instance++));
    }
    
    @Override
    public void run() {
        while ( true ) {
            try {
                Runnable work = null;
                synchronized ( queue ) {
                    while ( queue.isEmpty() )
                        queue.wait();
                    
                    // Get the next work item off of the queue
                    work = queue.remove();
                }
                // Process the work item
                work.run();
            }
            catch ( InterruptedException ie ) {
                break// Terminate
            }
        }
    }
    private void doWork(Runnable work) { ... }
}

The code that creates a pool of MyWorker objects, which is essentially a Thread pool, looks like this:

1
2
3
4
5
6
7
for ( int i = 0; i < THREAD_POOL_SIZE; i++ ) {
    // Create and start the worker thread, which will
    // immediately wait on the monitor (the queue in
    // this case) to be signaled to begin processing
    MyWorker worker = new MyWorker(queue);
    worker.start();
}

The code to place work on the queue and signal a worker thread looks like this:

1
2
3
4
5
6
7
8
9
10
synchronized ( queue ) {
    // Add work to the queue
    Runnable work = getMoreWork();
    queue.add(work);
    // Notify the monitor object that all the threads
    // are waiting on. This will awaken just one to
    // begin processing work from the queue
    queue.notify();
}

This code illustrates how straightforward basic Java thread synchronization is, although it is low-level. In Part 2 of this series, we’ll look at the java.util.concurrent package and how it contains built-in classes to make synchronized queue and task processing even simpler.

Jailbreak IPhone IPad IOS5 and Restore to the previous backup

This guide show you how to Jailbreak Iphone 4, 3GS and Ipad (not the Ipad 2) with the newly released IOS5 and most importantly how to restore to the previous backup(s).

As you know, IOS5 jailbreaking is still a tethered jailbreak that means every time the devices are reboot you will have to use redsn0w to enter DFU mode and have it “just boot tethered”.  You will find tons of sites showing you how to jailbreak but restore to the previous backup(s) is a little bit tricky since the you have to deal with tethering reboot.  The purpose of this guide is the help you for the second part – restoring after jailbreak.

  • Download Itune 10.5, Redsn0w and IOS5 for your devices ( I won’t show you the links but please head to Google for that)
  • Backup and sync your devices to latest setting, you will need to use that to restore.  In case if you don’t know, Edit – Preferences – Devices tab will show the name of all your backups.
  • Connect your iDevice with your PC via USB, and open iTunes 10.5 then Restore to iOS 5.0.  Right after the devices are upgrade to IOS5 disconnect Itunes and ignore all the initial settings with iCloud, etc.
  • Start Redsn0w, press on “Extras > Select IPSW” and point it to iOS 5.0 firmware ipsw file.
  • Now select “Install Cydia” and click “Next”. Optionally (if available), you can also select any other option you like.
  • Follow the instructions on Redsn0w to go thru the DFU mode and wait until you get iOS 5.0  jailbroken in few minutes
  • Once current Redsn0w jailbreak is still tethered jailbreak, you will be required to connect your iOS device with your computer and run Redsn0w again then choose “Just boot tethered right now”.
  • Now go to Cydia to set source and install some relevant apps (such as Installous, etc)
  • Here is the tricky part.  At this point your devices are jailbroken but you will lose all your previous setting as well as your previous apps. So you need to perform the restore to get back to what you have before.  Now connect to Itunes then right click on your  device, chose “Restore from backup” and select the name of the backup that you ‘re done in the first step to restore
  • Your device will restore and reboot; however it will be stuck at about approximately 90% of the rebooting process.  The reason is it need a tethering reboot with Redsn0w
  • Keep everything (i.e. your device still connect to Itunes) and launch Redsnow to perform the tethering boot.  Itunes will automatically be closed, wait for you device boot up and reconnect with Itunes to finish up the restore process