Khyber, the most dangerous place on earth?

According to Tariq Hayat Khan its safer than Chicago! This interview is a fascinating insight into the area, especially into the amount of power vested in one man to aminister an extremely significant area in Pakistan (but of course, quite far its nuclear weapons).

Chomsky said that violence works


You might wish it isn’t true, but its hard to disagree with him.

Half a half: Part 2

Ten days ago we had a look at Java’s ability to deal with numbers which had alot of decimal places. Liam pointed out that having to loop through a variety of operations is longer than performing a more efficient operation once.

With Liams magical formula, we get the following Java, with no loop:

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
import java.math.BigDecimal;
 
public class The_proper_half {
 
    public static Integer times_to_loop;
 
    /**
     * @param args the number of times you want to run the loop
     */
    public static void main(String[] args) {
        calculate(Integer.valueOf(args[0]));
 
    }
 
    public static void calculate(int times_to_loop) {
        //The Number we want to half
        BigDecimal number_to_half = new BigDecimal("10");
        //The result of doing the half
        BigDecimal the_half = new BigDecimal("0");
        //The number we want to divide by
        BigDecimal divider = new BigDecimal("2");
 
        the_half = (number_to_half.divide(divider.pow(times_to_loop)));
 
        System.out.println("The last number has " +
                ((Integer) number_to_half.subtract(the_half).toString().length()
                - 2) + " decimal places: ");
 
        System.out.println(number_to_half.subtract(the_half));
    }
}

As you can see, we only perform the opertation once (line 23), but admitadly, to get the correct number at the end, we still do the subtraction at the end!

Regarding optimisation, there are huge benefits. It turns out that the old way if hugely costly for thousands of iterations. These are the results for 10000 iterations, the old way:

anton@anton-laptop classes $ time java Half_of_a_half 10000 > /dev/null

real    5m22.714s
user    5m21.320s
sys    0m1.052s

The results for the new way are much better:

anton@anton-laptop classes $ time java The_proper_half 10000 > /dev/null

real    0m1.019s
user    0m1.248s
sys    0m0.052s

So it went from over five minutes, down to just a second. That was just by changing from a loop a single statement. Thats pretty cool stuff!

I have to admit here, the difficult bit is probably not the code. Its the abillity to see how a problem may be translated from one way of expressing it to another. Its that bit of genius thats really done the work here. Credit to Liam for the genius!

An evening of violence.

This event happened about a week ago.

Its windy, dark, cold and wet outside. There is no rain, but a previous rain has left the pavement in a slippery way. I’ve gone for a run round the usual route round the Parc du Champs de Mars, which is just next to the Eiffel Tower. The half an hour run in these harsh conditions is quite rewarding, especially when you wake in the morning after.

The run finishes with a quick sprint up the steps of Musee de L’homme, and round the corner past the musee de l’homme cafe. That evening, it wasn’t possible to finish the run. 15 meters in front of me is a 6 foot 4 man, intimidating a woman at least a full head height shorter than him. He goes for a headbutt, missing by some distance quite deliberately. He leaves his head forward as he places clenched fists by his side, talking precise steps towards her. The gap between them is closing, despite the fact that shes cowering and peering behind her for a space to step. Shes clearly been in this position before; to twist your body round to look is to take your eyes off what you are avoiding is a bad move to make.

This incident is about to unfold in full sight of the 7 or so people around. Surprisingly no-one has made steps to intercede. Everyone has limits; mine are set at the instant he pulls his hand back for a strike. That is the moment for action. Before then, the mater may resolve itself with words, the preferred solution.

Something that I haven’t noticed is that upon completing the sprint, I’ve paused to observe the situation in a jog on the spot motion. Not a huge knees in the air jog, but one which resembles the boxers side to side stepping, just before he lunges forward at the punch bag. Coupled with an all black outfit (it just so happened to be all black that evening) and low comfortable shoulders, my presence is looking quite aggressive. Underneath this appearance, I’m just observing, waiting for the trigger, but hoping that its not pulled. I’m relaxed and calm, and with each step my breath is getting slower and slower, back to normal standing rate.

So what does every coward do before doing something stupid? He looks round. Having spotted me, he suddenly realises that he’s been watched all this time, and that he might have trouble pulling off a smack. He turns his attention towards me and takes a step in my direction.

From an Islamic perspective, Muslims are supposed to enjoin the good and forbid the evil. This can take many forms, and in this instance it involves defending someone who is unable to defend themselves. This includes all children, the elderly and women by default. Mostly, this situation is win-win. Allow me to break my analysis down for you:

  • If he’s intimidated out of hitting her and calms down, she wins and he wins too; he won’t be going to jail

The complicated part is in explaining what might happen if he tries to attack me instead. Firstly, as a muslim, I believe that only God decides the timing of my death. If I’m to live until I’m 80 years old, its guaranteed. If I’m to die on a ferry which sinks in the sea, then I can’t avoid it. So in this instance, I’m not worried about death. If God decides I’m to die that day, its going to happen whether I’m hit by a bus, if I fall down a manhole, or if this man attacks me. This may sound a bit morbid, but its quite a relief knowing that all I have to do is take care of my health, God will decide the rest. Of course, my greater concern is always for those around me, family and friends who would be upset or set back in the event of my death. And of course, I don’t want it to happen any time soon :) My work schedule is too busy  ;)

If he attacks, and we have a little scuffle, he’s going to loose interest in further dealings very quickly. This is the nature of isolated public violence. People get embarrassed, feel ashamed, or realise that its just not worth it. I don’t want to hurt anyone, but sometimes you have to do what is right. Sometimes that means you get hurt, thats just how life is. Having grown up on a council estate, I’ve seen this happen many times before. 99% of incidents like this rely totally on posturing, so now is not the time to shrink in character or resolve. Most men appreciate that if their opposition is completely committed, its not worth it.

So what happened, I hear you asking?

He took a second step towards me, waving his arm vaguely at me as if to say “Whos this punk? “. I’m still jogging on the spot. I can hear the pat pat of water being lightly splashed under my feet; I had managed to stand in a shallow puddle. Just then, she shouts something to him, and he swings around with his attention back on her again. By now, some of those 7 or so people nearby have drawn closer to the situation and appear to know both the women and the man. Their group moulds back together as the man is calming down, him on one side of them, and the women on the other. They form together and the begin to head on a path beyond me, probably to go to the Eiffel Tower. I’m still not moving from my position. To do so is like saying your no longer paying attention. Taking your eyes off that kind of situation is the last thing you want to do. They walk past, the man only a few meters away from me now, but predictably he says and does nothing as they leave. I can smell alcohol on his breath. The pat pat sound turns into a squish squish sound as I walk back to the flat, reflecting on what just happened.

Half a number, half it again and add it to the first half.

This is a common riddle, so if you’ve heard it, don’t shout out the answer (people might think your strange for shouting at the internet….)

Take a number. Then, half that number. Then half that half and add it to the first half. Before that gives people a headache, lets do a little example:

Lets use 10 . Then half of ten gives us 5. Then, half five (2.5) and add it to the first half, 5. This gives us 7.5

Continue to half the last half, adding it to the sum of the previous halves. Phew, I hope thats explained properly.i.e. half two point five, and add it the the 7.5 we already have, which give us 8.25

Okay, so the question is this. If we keeping adding on the halves, will we ever reach 10 again? Well, I like Java, so lets get all geeky and model the problem. For this, the double and float data types won’t give us enough decimal places. For this, we’ll need: math.BigDecimal . The Big Decimal. Sounds ominous!

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
import java.math.BigDecimal;
public class Half_of_a_half {
 
    public static Integer times_to_loop;
 
    /**
     * @param args the number of times you want to run the loop
     */
    public static void main(String[] args) {
        calculate(Integer.valueOf(args[0]));
    }
 
    public static void calculate(int times_to_loop) {
        //The Number we want to half
        BigDecimal number_to_half = new BigDecimal("10");
        //The result of doing the half
        BigDecimal the_half = new BigDecimal("0");
        //The number we want to divide by
        BigDecimal divider = new BigDecimal("2");
 
        for (int i = 0; i < times_to_loop; i++) {
 
            number_to_half = number_to_half.divide(divider);
            the_half = the_half.add(number_to_half);
 
            System.out.println(the_half);
        }
        System.out.println("The last number has " +
                ((Integer) the_half.toString().length() - 2) + " decimal places");
    }
}

This code takes a single parameter on the command line: the number of times to half by

e.g.

anton@anton-laptop classes $ java Half_of_a_half 10
5
7.5
8.75
9.375
9.6875
9.84375
9.921875
9.9609375
9.98046875
9.990234375
The last number has 9 decimal places

So as we can see, the decimal places just get long and longer. The additions are never big enough to get us close enough to the original number. Problem solved!

The fun bit is realising the significance of the calculation, or rather, the speed with which it was done. Performing 1000 divisions and additions takes less that 0.2 of a second! Maybe thats not so fast by todays computing ability, but by human standards thats pretty extreme.

anton@anton-laptop classes $ time java Half_of_a_half 1000 > /dev/null


real    0m1.387s
user    0m1.916s
sys    0m0.036s

House of God to get capacity upgrade

Its great news to hear that the grounds at Mecca are getting an upgrade in capacity! Its about time, given that despite the 900,000 capacity, during Hajj there are more people outside than in!!Given that Islam is the fastest growing religion in the world, they should probably build it for a 5 million capacity, the Muslims will most definitely fill the space.

In terms of an engineering project, its hugely interesting. A few features spring to mind:

  • How to facilitate the wudhu needs of the people. This of course includes the need for restroom facilities
  • Crush control structures in the common case of overcrowding
  • Some kind of water mist system / water dispensing for when it gets really hot
  • Entrance ways and exits
  • Medical attention facilities
  • Viewing platforms, for filming perhaps

These kinds of things, if properly addressed, will take more distractions away from what the people really went there for: to make pilgrimage for the sake of God, which itself is quite interesting. Like ramadhan, it is one of the pillars of Islam, and has positive effects on people. Its quite a demonstration of solidarity between people of all colours and backgrounds. Whilst everyone is dressed as humble pilgrims, there is no distinction between rich and poor, or even of where they are from. Its an oppourtunity to reflect on the origins of their faith, and to consider what that means in the 15th century AH, (21st CE) .

An end, a pause, a beginning

There is a single moment that sticks in my mind about the 2008 US Presidential race. Maybe its not so significant, but if what it represents remains consistent, then the ideology executed in front of it will be far reaching in its effect. I’m talking about the moment that Barack Obama finished his election victory speech. He waited, thought about the moment, and then smiled. There is at least a 7 second gap.

His pause, before showing emotion, is very significant. Why? It signals his ability to contain himself, his ability to suppress ego (think what most men would do in his position), despite the temptation to go wobbly at the knees. This is a man who, when in front of 100,000 people, wasn’t grinning like some child whose most hated bully had been slapped in the face. He simply paused, absorbed the reaction, waited a moment and smiled.

The bit to watch out for is the last 10 seconds of the clip, although the whole clip is quite informative and worth watching.

The honeymoon will be over when the new elect President comes to tell America some truths:

  • “Iraq” is Arabic for “Vietnam”
  • Afghanistan can never be won, and Pakistan won’t be worth the effort (for the same reasons as Afghanistan)
  • Their brand of capitalism will need serious welding to fix the banking/housing crisis
  • Globalisation has permanently relieved the America economy of jobs

Click here for a high quality filming of the event and here for the script of the speech.

As for foreign policy, even Family Guy writers can’t write this stuff, they just copy and paste:

Possibly the best part of having Mr Obama as President, is that he’s going to open channels of dialogue with the Iranian Government. This can only be a good thing; as not too far in the future Iran might be nuclear capable, which is perceived to be an obstacle to international relations (even though its fine to break principles when cash is flashed).

Seeing a man who will clearly go on to be a great statesmen, I can’t help but look to the east and wonder where the great Muslim statesmen are. The leaders of Egypt, Syrian, Jordan, Kuwait, Saudi Arabia and Qatar fail to impress. A pity, since some of the greatest statesmen of history have passed through their lands.

May God have mercy on us all. Let us all be the change we want to see in the world. It sounds like therapy, but its what alot of people need to hear; yes, we can.

Does AlJazeera English brainwash you?

Well, if you get a sudden urge to go out and buy stocks in an energy firm in Qatar, then yes. Or if you buy a new Toshiba Tecra, then yes. Then again, thats probably because you’ve been watching the advertisements and not the news.

So why is this news channel controversial? Put quite simply, they don’t have a watershed. They will show any clip, no matter how scary/bloody/happy at any time of the day. If Bush says something, they show it. If Bin Laden says something, they show it. In fact, theres not much they won’t show. When the Russian Georgian conflict/invasion/defence operation/<insert your opinion here> kicked off, they interviewed the following people:

  • Dmitry Medvedev – President of Russia
  • The Georgian Foreign Minister

A short list of other people that are have conducted interviews with in the past few months are:

  • Hans Blix – Former UN weapons inspector
  • Hamid Kharzi – President of Afghanistan
  • Nigers Environment Minister
  • Anwar Ibrahim – Deputy Prime Minister of Malaysia
  • Ehud Barak – Current Isreali Defense Minister, former Prime Minister
  • Chimon Peres – President of Israel
  • David Cameron – Leader of the Conservative party, UK
  • Jaime Bermudez – Columbian Foreign Minister
  • Mahinda Rajapaksa – Current Sri Lankan President

So does AlJazeera brainwash? Maybe a better question would be, how many other news outlets make as many interviews? Of course, everyone should always read a broad spectrum of news, but for international relations, this is a good start.

Using synchronized methods in java

This post has long code segments in it, so I’ll explain what it all does at the top. Then at the end, just copy paste into your favourite editor/IDE to play with it.

So the title kind of gives it away, but the concept behind it was quite interesting. Imagine you have some kind of resource, like a network connection, a file etc, which is needs to be accessed by many threads, but only one thread at a time. Using synchronized methods achieves this. Its especially handy where you might have hundreds of threads which need to modify the same resource, but should do it, one at a time. This code can do that (although its not perfect by far)

In this example, we have a padlock, which we can open , and close. Of course, we can’t open it whilst its being closed, and vica versa. You could just run the openLock and closeLock methods one after the other, but using threads gives us some bonus features. For example, we might want to open the padlock, get half way through, and then realise we want to pause, leaving the rest of the work for later. Or maybe we want to get halfway through closing the padlock, and then change our mind and open it (i.e. without fully closing it)

So heres the code, enjoy!
The padlock:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package workingWithSynchronizedThreads;
 
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 * This class represents a simple padlock. You know, like the kind you use to
 * lock up your bike. I use it as an example of how we can make fields in
 * java , then try and access them through different threads. Using syncronised
 * methods, we will prevent the lock from being opened whilst were still trying
 * to close it.
 * @author anton
 */
public class padlock {
 
    /**
     * Constructor object to make a new padlock
     * @param isThePadlockOpen Whether the padlock is open or closed
     */
    public padlock(boolean isThePadlockOpen) {
        this.isThePadlockOpen = isThePadlockOpen;
    }
    /**
     * This field tells us whether or not the lock is open or closed (true for
     * open, false for locked)
     */
    public boolean isThePadlockOpen;
 
    /**
     * This method simply sets the padlock to be closed by setting isOpen to false
     * Closing the lock takes two seconds (hence the sleep statements) ).
     */
    public synchronized void closeLock() {
        try {
            Thread.sleep(1000);
            isThePadlockOpen = false;
            System.out.println("I'm closing the lock");
            Thread.sleep(1000);
            tellMeIfThisLockIsOpen();
        } catch (InterruptedException ex) {
            System.out.println("I couldn't close the lock");
            Logger.getLogger(padlock.class.getName()).log(Level.SEVERE, null, ex);
        }
 
    }
 
    /**
     * This method simply sets the padlock to be open by setting isOpen to true.
     * Opening a lock takes two seconds (hence the sleep statements ).
     */
    public synchronized void openLock() {
        try {
            Thread.sleep(1000);
            isThePadlockOpen = true;
            System.out.println("I'm opening the lock");
            Thread.sleep(1000);
            tellMeIfThisLockIsOpen();
        } catch (InterruptedException ex) {
            System.out.println("I couldn't open the lock");
            Logger.getLogger(padlock.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
    /**
     * Lets us know if our padlock object is open or closed
     */
    public void tellMeIfThisLockIsOpen() {
        if (isThePadlockOpen == true) {
            System.out.println("The lock is open!");
        } else if (isThePadlockOpen == false) {
            System.out.println("The lock is closed!");
        } else {
            System.out.println("Something is seriously wrong!");
        }
    }
}

The closeLockThread:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package workingWithSynchronizedThreads;
 
/**
 *
 * @author anton
 */
public class closeLockThread extends Thread {
 
    public padlock padlock;
 
    public closeLockThread(Object padlock) {
        this.padlock = (padlock) padlock;
    }
 
    @Override
    public void run() {
        padlock.closeLock();
    }
}

The openLockThread:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package workingWithSynchronizedThreads;
 
/**
 *
 * @author anton
 */
public class openLockThread extends Thread {
 
    public padlock padlock;
 
    public openLockThread(Object padlock) {
        this.padlock = (padlock) padlock;
    }
 
    @Override
    public void run() {
        padlock.openLock();
 
    }
}

Okay, less mess with some threads!!!:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package workingWithSynchronizedThreads;
 
/**
 *
 * @author anton
 */
public class messAroundWithSomePadlocks {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        padlock myLock = new padlock(true);
        closeLockThread iWannaCloseTheLock = new closeLockThread(myLock);
        openLockThread iWannaOpenTheLock = new openLockThread(myLock);
        iWannaCloseTheLock.start();
        iWannaOpenTheLock.start();
    }
}

The things that need attention are:

  1. Do we need to do that casting from padlock to padlock in the openLock and closeLock thread classes?
  2. Is it ok/understandable to make a field a question? (public boolean isThePadlockOpen;) ?

The inspiration for this work came from looking at some code were working on in my department, but the learning was really done looking at the Java Tutorials, which are ace!

Solaris support for Rock processor

Its been added to snv (Solaris Nevada, the development build of Solaris) build 100 (were on 99 at the moment).

This includes the following changes:

6368478 Solaris needs to support the Rock processor
6440653 stores to sun4v error queue head registers are missing a membar #Sync
6639717 FLUSH instruction used more than necessary in sun4v