Archive for October, 2008

Does AlJazeera English brainwash you?

Friday, October 10th, 2008

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

Saturday, October 4th, 2008

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!