mysql> show variables like 'port'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+ 1 row in set (0.02 sec) mysql> show variables like 'version'; +---------------+-------------------+ | Variable_name | Value | +---------------+-------------------+ | version | 5.0.45-community-nt | +---------------+-------------------+ 1 row in set (0.00 sec)
Tuesday, March 20, 2012
How to find MYSQl Port No and Version ?
Monday, March 12, 2012
Java JVM Shutdown Hook - tutorial
JVM shutdown hooks provide a clean and simple mechanism for registering application-specific behavior that performs the required cleanup work when a JVM terminates.
It is a Java feature that let you have a piece of Java code run whenever the JVM terminates under one of the following conditions:
- The program exits normally, such as when the last non-daemon thread exits or when the Runtime.exit() method is invoked.
- The virtual machine is terminated in response to a user interrupt, such as typing CTRL-C, or a system-wide event, such as user logoff or system shutdown (for example, the JVM receives one of the interrupt signals SIGHUP (Unix Only), SIGINT, or SIGTERM).
A shutdown hook is a Java class that extends java.lang.Thread and is get registered with the Runtime.addShutdownHook()method. Your application may install multiple shutdown hooks. On JVM termination, each shutdown hook will be started and will run concurrently, so the normal rules of thread synchronization apply. You can write shutdown hooks to do anything that a normal thread would do; the JVM will treat them like any other.
Generally we write a shutdown hook to do any last-minute tidying, such as flushing memory buffers, closing files, or displaying an exit message. The JVM will always wait until all shutdown hooks have completed before continuing with the rest of the shutdown sequence, so it is important that your shutdown hooks do actually complete.
The following example shows how to write and install a Java shutdown hook.
/**
* Implementation of JVM shutdown hook
*
* @author abdul
*
*/
public class ShutDownHook {
public static void main(String args[]) {
ShutDownHook hook = new ShutDownHook();
System.out.println( "Running Main Application..." );
// Java code to register shutdown hook:
hook.attachShutDownHook();
// You can unregister the shutdon hook too.
hook.removeShutdownHook();
System.out.println("Exit");
}
Thread shutdown =new Thread() {
@Override
public void run() {
//Do your cleaning stuff here eg closing connection,Saving some logs etc
System.out.println("Inside Add Shutdown Hook");
}
};
private void attachShutDownHook() {
Runtime.getRuntime().addShutdownHook(shutdown);
System.out.println("Shut Down Hook Attached.");
}
public void removeShutdownHook(){
Runtime.getRuntime().removeShutdownHook(shutdown);
}
}
There is also a method named removeShutdownHook, which also takes the reference to the Thread object as a parameter. This method allows you to remove a Thread from the list of threads to be started by the VM before it closes (Runtime.getRuntime().removeShutdownHook(shutdown); for the above shutdown hook example).
FAQ On JVM Shutdown Hook API
It is a Java feature that let you have a piece of Java code run whenever the JVM terminates under one of the following conditions:
- The program exits normally, such as when the last non-daemon thread exits or when the Runtime.exit() method is invoked.
- The virtual machine is terminated in response to a user interrupt, such as typing CTRL-C, or a system-wide event, such as user logoff or system shutdown (for example, the JVM receives one of the interrupt signals SIGHUP (Unix Only), SIGINT, or SIGTERM).
A shutdown hook is a Java class that extends java.lang.Thread and is get registered with the Runtime.addShutdownHook()method. Your application may install multiple shutdown hooks. On JVM termination, each shutdown hook will be started and will run concurrently, so the normal rules of thread synchronization apply. You can write shutdown hooks to do anything that a normal thread would do; the JVM will treat them like any other.
Generally we write a shutdown hook to do any last-minute tidying, such as flushing memory buffers, closing files, or displaying an exit message. The JVM will always wait until all shutdown hooks have completed before continuing with the rest of the shutdown sequence, so it is important that your shutdown hooks do actually complete.
The following example shows how to write and install a Java shutdown hook.
/**
* Implementation of JVM shutdown hook
*
* @author abdul
*
*/
public class ShutDownHook {
public static void main(String args[]) {
ShutDownHook hook = new ShutDownHook();
System.out.println( "Running Main Application..." );
// Java code to register shutdown hook:
hook.attachShutDownHook();
// You can unregister the shutdon hook too.
hook.removeShutdownHook();
System.out.println("Exit");
}
Thread shutdown =new Thread() {
@Override
public void run() {
//Do your cleaning stuff here eg closing connection,Saving some logs etc
System.out.println("Inside Add Shutdown Hook");
}
};
private void attachShutDownHook() {
Runtime.getRuntime().addShutdownHook(shutdown);
System.out.println("Shut Down Hook Attached.");
}
public void removeShutdownHook(){
Runtime.getRuntime().removeShutdownHook(shutdown);
}
}
There is also a method named removeShutdownHook, which also takes the reference to the Thread object as a parameter. This method allows you to remove a Thread from the list of threads to be started by the VM before it closes (Runtime.getRuntime().removeShutdownHook(shutdown); for the above shutdown hook example).
FAQ On JVM Shutdown Hook API
Thursday, March 8, 2012
Java HashMap Example
The HashMap class uses a hash table to implement the Map interface. This allows the execution time of basic operations, such as get() and put(), to remain constant even for large sets.
The following constructors are defined:
Hash map does not guarantee the order of its elements. Therefore, the order in which elements are added to a hash map is not necessarily the order in which they are read.
Here is the code:
import java.util.HashMap;
import java.util.Map;
/**
*
* This Java HashMap example describes the basic operations performed on the HashMap
* @author abdul
*
*/
public class HashMapExample {
public static void main(String args[]){
// constructs a new empty HashMap with default initial capacity
Map<Integer,String> hashMap = new HashMap<Integer,String>();
// adding value into HashMap
hashMap.put(new Integer(1),"DELL");
hashMap.put(new Integer(2),"SONY");
hashMap.put(new Integer(3),"LENOVO");
hashMap.put(new Integer(4),"SAMSUNG");
hashMap.put(new Integer(5),"HP");
//To get the size of HashMap
System.out.println("HashMap Size: " + hashMap.size());
// To Find particular value from the HashMap:
if(hashMap.containsValue("HP")){
System.out.println("HashMap contains HP");
}else{
System.out.println("HashMap does not contain HP");
}
//To find particular key in HashMap
if( hashMap.containsKey(new Integer(2)) ){
System.out.println("HashMap contains key");
}else{
System.out.println("HashMap does not contain key");
}
/*
Use get method of HashMap to get value mapped to particular key.
Signature of the get method is,
Object get(Object key)
*/
String value = hashMap.get(new Integer(5));
System.out.println("Value : " + value);
//To get the key value pair of hashMap
for (Map.Entry<Integer, String> entry1 : hashMap.entrySet()) {
Integer key = entry1.getKey();
String data = entry1.getValue();
System.out.println("key value : " + key + "\t" + data );
}
// To remove something from the hashmap
System.out.println( hashMap.remove(new Integer(4)) + " removed from the hashMap");
//Display value of HashMap
System.out.println("HashMap : " + hashMap );
}
}
Output:
HashMap Size: 5
HashMap contains HP
HashMap contains key
Value : HP
key value : 1 DELL
key value : 2 SONY
key value : 3 LENOVO
key value : 4 SAMSUNG
key value : 5 HP
SAMSUNG removed from the hashMap
HashMap : {1=DELL, 2=SONY, 3=LENOVO, 5=HP}
The following constructors are defined:
HashMap( )
HashMap(Map m)
HashMap(int capacity)
HashMap(int capacity, float fillRatio)
Hash map does not guarantee the order of its elements. Therefore, the order in which elements are added to a hash map is not necessarily the order in which they are read.
Here is the code:
import java.util.HashMap;
import java.util.Map;
/**
*
* This Java HashMap example describes the basic operations performed on the HashMap
* @author abdul
*
*/
public class HashMapExample {
public static void main(String args[]){
// constructs a new empty HashMap with default initial capacity
Map<Integer,String> hashMap = new HashMap<Integer,String>();
// adding value into HashMap
hashMap.put(new Integer(1),"DELL");
hashMap.put(new Integer(2),"SONY");
hashMap.put(new Integer(3),"LENOVO");
hashMap.put(new Integer(4),"SAMSUNG");
hashMap.put(new Integer(5),"HP");
//To get the size of HashMap
System.out.println("HashMap Size: " + hashMap.size());
// To Find particular value from the HashMap:
if(hashMap.containsValue("HP")){
System.out.println("HashMap contains HP");
}else{
System.out.println("HashMap does not contain HP");
}
//To find particular key in HashMap
if( hashMap.containsKey(new Integer(2)) ){
System.out.println("HashMap contains key");
}else{
System.out.println("HashMap does not contain key");
}
/*
Use get method of HashMap to get value mapped to particular key.
Signature of the get method is,
Object get(Object key)
*/
String value = hashMap.get(new Integer(5));
System.out.println("Value : " + value);
//To get the key value pair of hashMap
for (Map.Entry<Integer, String> entry1 : hashMap.entrySet()) {
Integer key = entry1.getKey();
String data = entry1.getValue();
System.out.println("key value : " + key + "\t" + data );
}
// To remove something from the hashmap
System.out.println( hashMap.remove(new Integer(4)) + " removed from the hashMap");
//Display value of HashMap
System.out.println("HashMap : " + hashMap );
}
}
Output:
HashMap Size: 5
HashMap contains HP
HashMap contains key
Value : HP
key value : 1 DELL
key value : 2 SONY
key value : 3 LENOVO
key value : 4 SAMSUNG
key value : 5 HP
SAMSUNG removed from the hashMap
HashMap : {1=DELL, 2=SONY, 3=LENOVO, 5=HP}
Subscribe to:
Posts (Atom)
How TOPT Works: Generating OTPs Without Internet Connection
Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...
-
Prerequisite: Basic knowledge of Spring boot application I am working on a series of implementing frameworks with Spring boot application b...
-
In my previous blog, I have already discussed on Lambda Expression and its Syntax as well as Functional Interface. So, if you are not awa...
-
From last one month, My internet device is missing from my terrace. So, Mostly I use mobile to access my mail but accessing mail via mobile...