2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2020

10/14/2003: Using synchronized in Java

For quite awhile, I’ve been using the sychnronized keyword in an incorrect manner, probably because of my ColdFusion background. Synchronization locks in ColdFusion lock access to a block of code based on some arbitrary lock phrase. As long as two locks have the same lock phrase, they single-thread the process through the protected code.

The concept that I recently relearned was that Java sychronizes on an object - typicallly ‘this’. Which has some interesting ramifications when one object starts two threads which both refer to static objects. The parent can have every method sychronized without affected the child threads.

The above issue is relative mundane, but my blindness to the true nature of Java synchronization shows how important it is to constantly reexam even your most basic understandings about the tools that you use everyday.

10/03/2003: Creating an Ant Task with Eclipse

Creating an Ant Task with Eclipse

  1. Add ant.jar to your eclipse project.
  2. Create package to hold task classes. I recommend xxx.yyy.ant.tasks
  3. Create task class. Here is a simple one:
    package com.affy.ant.tasks;
    import org.apache.tools.ant.*;
    public class HelloWorld extends Task {
        public void execute() throws BuildException {
            System.out.println("Hello World!");
        }
    }
    
  4. Select Window, Preferences, Ant, Classpath
  5. Set ANT_HOME
  6. Add the folder where your class files are stored (xxx/build/classes, for example)
  7. Select Task, Add Task
  8. Assign a task name (ie, HelloWorld) to use in build.xml
  9. Select location (the folder xxx/build/classes for example) </ol>

    That's how easy it is to start writing your own Ant tasks. If you're going to invoke build.xml from within Eclipse, then you only need the following lines:

        <target name="HelloWorld">
            <HelloWorld/>
        </target>
    

    Otherwise, you need to tell Ant where to find the class file. For example,

        <path id="cp">
            <pathelement path="xxx/build/classes"/>
        </path>
        <target name="HelloWorld">
            <taskdef name="HelloWorld" classname="org.wwre.ant.tasks.HelloWorld" classpathref="cp"/>
            <HelloWorld/>
        </target>