Wednesday, March 6, 2013
Friday, June 12, 2009
How to generate class-path variable in Manifest file of a jar
For that we need to generate Class-Path attribute in the MANIFEST.MF dynamically. The following code shows how to do that.
1. Define a path element say, classpath
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
2. Convert the path element values into relative path and store it in a string property, say jar.classpath
<pathconvert property="jar.classpath" refid="classpath" pathsep=" ">
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="lib/*.jar"/>
</chainedmapper>
</mapper>
</pathconvert>
<target name="jar" depends="clean,compile">
<mkdir dir="${jar.dir}"/>
<mkdir dir="${jar.lib.dir}"/>
<!-- Copying the external libraries to build/jar/lib folder. -->
<copy todir="${jar.lib.dir}">
<fileset dir="${lib.dir}" excludes="**/*.svn"/>
</copy>
<jar destfile="${jar.dir}/${jar.file}" basedir="${classes.dir}" excludes="**/*.svn">
<manifest>
<attribute name="Class-Path" value=". ${jar.classpath}" />
</manifest>
</jar>
</target>
Note :- The lib directory with the external jar files should be in the same directory as that of the project jar file.
We can't put the external jar files inside the project jar files with the above method.
Tuesday, June 9, 2009
Reusability of Thread.
Can you resuse a Thread Object? Can you restart it by calling start() again?
No. Once a thread's run() method has completed, the thread can never be restarted. In fact, at that point the thread moves into a state - dead. In dead state, the thead has finished its run()method and can never be restarted. The Thread object might still be on the heap, as aliving object that you can call other methods on (if appropriate), but the Thread object has permanently lost its 'threadness'. In other words, there is no longer a seperate call stack, and the Thread object is no longer a thread. It's Just an object, at that point, like all other objects.
But there are design patterns for making a pool of threads that you can keep using to perform different jobs. But you don't do it by retsarting a dead thread.
How to Implement a scheduler in java
Use java.util.Timer and java.util.TimerTask to Implement a scheduler in java.
The following are all the Timer methods you can use to schedule repeated executions of tasks:
schedule(TimerTask task, long delay, long period)
schedule(TimerTask task, Date time, long period)
scheduleAtFixedRate(TimerTask task, long delay, long period)
scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
When scheduling a task for repeated execution, use one of the schedule methods when smoothness is important and a scheduleAtFixedRate method when time synchronization is more important.
schedule method is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run.
While in scheduleAtFixedRate, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up."
Call Timer.cancel() to stop the Timer.
Example:
public class AnnoyingBeep {
Toolkit toolkit;
Timer timer;
public AnnoyingBeep() {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
1*1000); //subsequent rate
}
class RemindTask extends TimerTask {
int numWarningBeeps = 3;
public void run() {
if (numWarningBeeps > 0) {
toolkit.beep();
System.out.format("Beep!%n");
numWarningBeeps--;
} else {
toolkit.beep();
System.out.format("Time's up!%n");
//timer.cancel(); //Not necessary because
//we call System.exit
System.exit(0); //Stops the AWT thread
//(and everything else)
}
}
}
...
}
To Move a File from One directory to another in Java
We can use File.renameTo() method to move a file from one folder to another.
File file = new File("filename");
// Destination directory
File dir = new File("directoryname");
// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));
if (!success) {
// File was not successfully moved
}