The wait(), notify() and notifyAll() methods are used to provide an efficient way for threads to wait for a shared resource. When a thread executes an object's wait() method, it enters the waiting state. It only enters the ready state after another thread invokes the object's notify() or notifyAll() methods.
Thursday, May 13, 2010
Monday, May 10, 2010
Volatile Modifiers in Java
Volatile Modifier
The volatile modifier requests the Java Virtual Machine to always access the shared copy of the variable so the its most current value is always read. If two or more threads access a member variable, and one or more threads might update that variable’s value, and all of the threads do not use synchronization to read and/or write the variable value, then that member variable must be declared volatile to ensure all threads should get the updated value.
We will discuss the java volatile modifier using following example.
In our example, we take two threads Thread T1 and Thread T2 accessing member variable x of class C and here Thread T1 is not using synchronization and Thread T2 uses synchronization. If Thread T2 updates value of variable x from 0 to 1. But if the variable x is not declared as volatile then meanwhile Thread T1 tried to access variable then it will get value of variable x as 0. But as variable x is updated to 1 by Thread T2, To avoid this mess the variable x should be declared as volatile so Thread T1 should get updated value 1.
The volatile modifier requests the Java Virtual Machine to always access the shared copy of the variable so the its most current value is always read. If two or more threads access a member variable, and one or more threads might update that variable’s value, and all of the threads do not use synchronization to read and/or write the variable value, then that member variable must be declared volatile to ensure all threads should get the updated value.
We will discuss the java volatile modifier using following example.
In our example, we take two threads Thread T1 and Thread T2 accessing member variable x of class C and here Thread T1 is not using synchronization and Thread T2 uses synchronization. If Thread T2 updates value of variable x from 0 to 1. But if the variable x is not declared as volatile then meanwhile Thread T1 tried to access variable then it will get value of variable x as 0. But as variable x is updated to 1 by Thread T2, To avoid this mess the variable x should be declared as volatile so Thread T1 should get updated value 1.
Thursday, May 6, 2010
HashMap vs ConcurrentHashMap
Both HashMap and ConcurrentHashMap are inherits some characteristics of Hashtable. But they have some prominent differences in context with performance, scalability.
HashMap
ConcurrentHashMap
HashMap
- Since Java 1.2
- Allows null key and value
- Poor performance in highly threaded applications
- Not much scalable
- Throws a ConcurrentModificationException
- Faster in non-multi threading applications
ConcurrentHashMap
- Since Java 1.5
- Doesn't allow null key or value
- Better performance in Highly threaded applications
- Highly scalable
- Do not throw ConcurrentModificationException
- Slower in non-multi threading applications
Wednesday, May 5, 2010
Read value from Java properties
Here, you will find the source code to read value of the key from properties file. java.util.Properties class extends Hashtable. Properties class creates the properties file which stores key - value pair using Hashtable. To retrieve value from properties file we have to follow following steps.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class PropertiesReadTest {
public static void main(String[] args) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("C:\\temp\\test.properties"));
String value = properties.getProperty("key");
System.out.println("Value = " + value);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output is shown here in following image
- Create an object of Properties class.
- Load properties file using FileInputStream into properties object.
- Get value from properties object using getProperty() method passing key as an input.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class PropertiesReadTest {
public static void main(String[] args) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("C:\\temp\\test.properties"));
String value = properties.getProperty("key");
System.out.println("Value = " + value);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output is shown here in following image
Tuesday, May 4, 2010
Source code to read content of File
Here you will find the source code to read the content of file.
Steps
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReadTest {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\temp\\test.txt"));
String line = "";
while((line = br.readLine()) != null)
{
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Steps
- Create the FileReader object using file name as an input to FileReader constructor.
- Pass that FileReader object to BufferedReader.
- Read the line from BufferedReader object till readline gets null.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReadTest {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\temp\\test.txt"));
String line = "";
while((line = br.readLine()) != null)
{
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Monday, May 3, 2010
Access file using JSP & Servlets
Given source code used for accessing file from server using
Servlets
String path = getServletContext( ).getRealPath ("xyz.xml" ) ;
System.out.println ( path ) ;
File file = new File ( path ) ;
JSP
String path = application.getRealPath ("xyz.xml" ) ;
System.out.println ( path ) ;
File file = new File ( path ) ;
In above jsp code, "application" is jsp implicit object.
Servlets
String path = getServletContext( ).getRealPath ("xyz.xml" ) ;
System.out.println ( path ) ;
File file = new File ( path ) ;
JSP
String path = application.getRealPath ("xyz.xml" ) ;
System.out.println ( path ) ;
File file = new File ( path ) ;
In above jsp code, "application" is jsp implicit object.
Subscribe to:
Posts (Atom)