Asynchronous Call in Java
Tuesday, July 30, 2013
The
java.util.concurrent.ExecutorService interface represents an asynchronous execution mechanism which is capable of executing tasks in the background. An ExecutorService is thus very similar to a thread pool. In fact, the implementation of ExecutorService present in the java.util.concurrentpackage is a thread pool implementation:
/**
* Asynchronous call to invoke any method
* * *
* @author Manesh Abhimanyu
* @param String Input 1
* @param String Input 2
*/
public void asyncServiceMethod( final String input1, final String input2)
{
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
Runnable task = new Runnable(){
public void run()
{
try {
invokeMyMethod( input1, input2 );
} catch ( Exception ex ) {
// handle error which cannot be thrown back
}
}
};
executor.execute( task );
} catch ( Exception e ) {
// handle error which cannot be thrown back
} finally {
// gargabe collect
executor.shutdown();
}
}
* Asynchronous call to invoke any method
* * *
* @author Manesh Abhimanyu
* @param String Input 1
* @param String Input 2
*/
public void asyncServiceMethod( final String input1, final String input2)
{
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
Runnable task = new Runnable(){
public void run()
{
try {
invokeMyMethod( input1, input2 );
} catch ( Exception ex ) {
// handle error which cannot be thrown back
}
}
};
executor.execute( task );
} catch ( Exception e ) {
// handle error which cannot be thrown back
} finally {
// gargabe collect
executor.shutdown();
}
}
More reading:
Labels: Asynchronous, Asynchronous call, concurrency, example, ExecutorService, ExecutorService example, java
posted by MIGHTYMAK @ 11:52 PM,
,
Different ways of creating objects in Java
Saturday, March 3, 2012
1. Using new keyword
This is the most common way to create an object in java. In this, we will be using the new operator which will allocates the memory space and initialize the field with default value. Then it executes the code inside the specified constructor which normally re-writes the default values with the value inside the constructor.
2. Using Class.forName()
Generally we use this while creating a database connection. If we know the name of the class & if it has a public default constructor we can create an object in this way.
jcapsmentor.com.Manesh – is the class name
3. Using clone()
The clone() can be used to create a copy of an existing object. Object.clone() is a native method which translates into instructions for allocating the memory and copying the data.Even if you override the clone method then also you need to call the super.clone() method inside the overridden clone method. In this method a copy instruction is called and which copy the data from original object to clone object.
4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form. It will uses the ‘new’ operator internally and always calls the default constructor.
5. Using class loader
one more is through creation of object using classloader like
This is the most common way to create an object in java. In this, we will be using the new operator which will allocates the memory space and initialize the field with default value. Then it executes the code inside the specified constructor which normally re-writes the default values with the value inside the constructor.
MyObject object new MyObject(); |
2. Using Class.forName()
Generally we use this while creating a database connection. If we know the name of the class & if it has a public default constructor we can create an object in this way.
MyObject object (MyObject) Class.forName( jcapsmentor.com.Manesh ).newInstance(); |
3. Using clone()
The clone() can be used to create a copy of an existing object. Object.clone() is a native method which translates into instructions for allocating the memory and copying the data.Even if you override the clone method then also you need to call the super.clone() method inside the overridden clone method. In this method a copy instruction is called and which copy the data from original object to clone object.
MyObject anotherObject new MyObject();MyObject object anotherObject.clone(); |
4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form. It will uses the ‘new’ operator internally and always calls the default constructor.
ObjectInputStream inStream new ObjectInputStream(anInputStream );MyObject object (MyObject) inStream.readObject(); |
5. Using class loader
one more is through creation of object using classloader like
this.getClass().getClassLoader().loadClass( com.amar.myobject ).newInstance(); |
Labels: class, classforName, java, new, objects, serialization
posted by MIGHTYMAK @ 8:48 PM,
,
Calling javascript code from applet
Tuesday, November 27, 2007
Sometimes you may want your applet to interact with javascript.
Apply this code to your applet class.
private void alert(){
try{
getAppletContext().showDocument(
new URL("javascript:doAlert()"));
}catch (MalformedURLException ex) { }
}When running this code you'll execute the javascript doAlert() placed in the htm file where the <applet> tag is located.
<script language="javascript" type="text/javascript">
function doAlert() {
//some code to be executed...
alert('doAlert() exevcuted!');
}
</script>
-OR-
Using the netscape.javascript package in your java code and the mayscript option in your applet tag. For this you need a Netscape JAR file name java40.jar to compile the javascript code.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import netscape.javascript.*;
public class jsobject extends Applet implements ActionListener
{
Button button;
public void init()
{
button = new Button("hello");
button.addActionListener(this);
add(button);
}
public void actionPerformed(ActionEvent event)
{
JSObject obj = JSObject.getWindow(this);
obj.call("hello", null);
}
}
Regards,
Manesh Koovappillil
Labels: applet, code, java, javascript, Netscape JAR
posted by MIGHTYMAK @ 2:05 AM,
,
Opening Notepad (or any other external application) with JAVA
Wednesday, November 21, 2007
public class test {
public static void main(String args[])
{
System.out.println("\nSuccess");
Runtime r= Runtime.getRuntime();
Process p=null;
try {
p=r.exec("notepad.exe");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Labels: java
posted by MIGHTYMAK @ 7:52 AM,
,

