It is known as collection framework because some classes and interfaces are introduced from java2 to manage the collection. Before the collection framework you can use array to manage the collection.
If you are managing the collection with array ten you may get following limitations
* The array is static. It means the size of the array cannot be changed dynamically.
* If you want to insert the element between the array or if you want to delete the element from the middle of the array then it will be time taking process. If you want to implement then you need to implement explicitly.
* At the time of creating the array you need to specify the type of the element that will be stored in the array. etc.,
To solve these problems Sun micro systems has provided some legacy classes and interfaces to manage the collection. Following are the legacy classes available in java.
* Vector
* Stack
* Hashtable
* Properties and
* Dictionary
* Enumeration is the Interface.
By using these classes you can collect following types of element
1. The simple element
2. The element with key and value format
Vector and Stack will be used to collect and to manage simple elements. Hashtable, Properties and Dictionary will be used to manage the element of key and value format.
Enumeration interface is used to access the information stored by using legacy classes. The problem with legacy classes is there is no specific difference where you can use the corresponding class. Almost all the methods available in the legacy classes are synchronized. Because of this one object cannot be accessed by multiple threads concurrently. To solve these problems Sun micro system has introduced some classes and interfaces to manage the collection from java2.
The collection of classes and interfaces is known as Collection Framework. These classes and interfaces are available in java.util package. In the collection framework following are the two interfaces provided.
1. Collection
2. Map
The subclass of collection interface will be used to manage the collection of simple elements. The sub classes of Map interface will be used to manage the collection of elements of key, value format.
Collection interface has two sub interfaces
1. List
2. Set
To access the element from the collection framework sun has provided following interface.
1. Iterator
2. ListIterator
Wanna Be Java Guru
Thursday, May 5, 2011
Example : Synchronization in Threads
package col.abc.test;
public class Lab4 {
public static void main(String[] args) {
Account acc = new Account();
new AccThread(acc);
new BccThread(acc);
}
}
class Account{
int bal = 970;
public synchronized void withDraw(int amt)
{
if(bal >= amt)
{
System.out.println(Thread.currentThread().getName() + " is going to withdraw... " + bal);
try {
Thread.sleep(1200);
} catch (Exception e) {
e.printStackTrace();
}
bal -= amt;
System.out.println(Thread.currentThread().getName() + " completed withdraw..." + bal);
}
else
{
System.out.println("No funds for " + Thread.currentThread().getName());
}
}
public int getBal() {
return bal;
}
}
class AccThread implements Runnable
{
Account acc = null;
AccThread(Account acc)
{
this.acc = acc;
Thread t1 = new Thread(this,"A");
t1.start();
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
acc.withDraw(225);
Thread.yield();
if(acc.getBal() < 0)
{
System.out.println("Amount is over drawn....");
}
}
}
}
class BccThread implements Runnable
{
Account acc = null;
BccThread(Account acc)
{
this.acc = acc;
Thread t2 = new Thread(this,"B");
t2.start();
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
acc.withDraw(225);
if(acc.getBal() < 0)
{
System.out.println("Amount is over drawn....");
}
}
}
}
public class Lab4 {
public static void main(String[] args) {
Account acc = new Account();
new AccThread(acc);
new BccThread(acc);
}
}
class Account{
int bal = 970;
public synchronized void withDraw(int amt)
{
if(bal >= amt)
{
System.out.println(Thread.currentThread().getName() + " is going to withdraw... " + bal);
try {
Thread.sleep(1200);
} catch (Exception e) {
e.printStackTrace();
}
bal -= amt;
System.out.println(Thread.currentThread().getName() + " completed withdraw..." + bal);
}
else
{
System.out.println("No funds for " + Thread.currentThread().getName());
}
}
public int getBal() {
return bal;
}
}
class AccThread implements Runnable
{
Account acc = null;
AccThread(Account acc)
{
this.acc = acc;
Thread t1 = new Thread(this,"A");
t1.start();
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
acc.withDraw(225);
Thread.yield();
if(acc.getBal() < 0)
{
System.out.println("Amount is over drawn....");
}
}
}
}
class BccThread implements Runnable
{
Account acc = null;
BccThread(Account acc)
{
this.acc = acc;
Thread t2 = new Thread(this,"B");
t2.start();
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
acc.withDraw(225);
if(acc.getBal() < 0)
{
System.out.println("Amount is over drawn....");
}
}
}
}
Wednesday, May 4, 2011
Eclipse
It is an IDE (Integrated Development Environment). It provides the facility to develop the application and to provide the environment by integrating the required component for the application. If you want to develop any application then you may get the following requirements. You need to create required directory structure depending on application type. You need to create multiple files and you need to place in the required directory. If you are creating java files then you may get the requirement to compile that. While compiling or file executing you may get the requirement to set the classpath for some classes or jar files.
If you are developing web application then you may get the requirement to integrate some servers, deploy the application to the servers etc., If you are performing these tasks manually in that condition it will be a time taking process. By using the IDE you can automate the above specified task.
If you are using eclipse you need to provide the workspace information. Workspace indicates the location from the filesystem where the project and the related information will be started.
If you are developing web application then you may get the requirement to integrate some servers, deploy the application to the servers etc., If you are performing these tasks manually in that condition it will be a time taking process. By using the IDE you can automate the above specified task.
If you are using eclipse you need to provide the workspace information. Workspace indicates the location from the filesystem where the project and the related information will be started.
yield(), join() and daemon thread
Yield():-
It is a static method in the thread class. When the yield() is called then the thread will be moved to runnable state from the running state and it will give chance to execute another thread with lowest priority (current thread).
Join():-
When you are calling join() then the current executing thread will be moved to waiting state and it will be re-executed after completing the thread on which the join method is called. This method may throw Interrupted Exception.
Daemon Thread:-
It is a special type of thread that will be used to provide the service for main thread. It will be executed till main thread is executing. After completing the main thread there is no guarantee that the daemon thread will be completed successfully. If you want to verify whether one thread is daemon thread or not then you can use the following method.
public final boolean is Daemon();
If you want to define one thread as daemon thread then you can call the following method with the thread object.
It is a static method in the thread class. When the yield() is called then the thread will be moved to runnable state from the running state and it will give chance to execute another thread with lowest priority (current thread).
Join():-
When you are calling join() then the current executing thread will be moved to waiting state and it will be re-executed after completing the thread on which the join method is called. This method may throw Interrupted Exception.
Daemon Thread:-
It is a special type of thread that will be used to provide the service for main thread. It will be executed till main thread is executing. After completing the main thread there is no guarantee that the daemon thread will be completed successfully. If you want to verify whether one thread is daemon thread or not then you can use the following method.
public final boolean is Daemon();
If you want to define one thread as daemon thread then you can call the following method with the thread object.
wait() and notify()
If you are creating any object in java then jvm will provide lock for every object. By default the lock will be disabled. When the object is used to call any synchronized method then the lock will be enabled and will be handed over to the thread that is using the object.
If you are calling non-static synchronized method then the object that will be used to call the method will be locked. If you are calling a static synchronized method then the default object for the class will be locked.
If you are using synchronized block then the object that will be passed as parameter for the block will be blocked. If the object is blocked by one thread then it will not be used to access any synchronized method concurrently by other thread.
If you are calling a sleep() with the thread class then the thread will be moved to sleep state. If the thread is moved to sleep state and if the thread is moved to sleep state and if the thread is locking some object then the lock will not be released.
wait() & notify():-
wait and notify methods are available in the object class. If you are calling the wait() then it must be called from the synchronized context. When you are using the object to call the wait() then the thread that will be used to invoke the statement will be moved to wait state. If another thread is using same object to call the notify() then the thread from wait state will be moved to runnable state.
If you have multiple threads in the wait state that was using the corresponding object and if you want to move all the threads from wait to runnable state then you need to call notifyAll().
What is the difference between sleep() and wait()?
sleep() :- Sleep is defined in the thread class. It is a static method in the thread class. So, it can be used with the class name. If you are calling the sleep() and if the lock is enabled for any object it will not be released. Sleep() may be called from the synchronized context or from non-synchronized context. If you are calling the sleep() then you need to specify some time. After completing the specified time the thread will be moved to runnable state.
wait():- wait() is defined in object class. It is the instance method in the object class. So, you need to use some object to call the wait(). If you are calling the wait() then the lock will be released on the corresponding object. wait() must be called from the synchronized context. If you are calling the wait() then if you want you can specify the time or you can ignore. If you are specifying the time then after completing the specified time the thread will be moved to runnable state. If you want you can call notify() to move the state to runnable before completing the time. If you are calling the wait() without specifying time then explicitly you need to call the notify() to move the thread state to runnable state.
Thread Priority():-
Whenever the thread object will be created then for every thread object some integer value will be allocated. This value is known as priority for the thread. If you are not providing priority explicitly then it will get some default priority. The priority value will be between 1 to 10.
The priority some constant is defined in the thread class. One constant is
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
The thread with highest priority will be executed by the CPU first than the other threads. If you want to provide the priority for the thread explicitly then you can call the following methods wit the thread object.
public final void setPriority(int);
If you want to access the priority of the thread you can call this method.
public final int getPriority();
If you are providing the priority to the thread less than 1 or greater than 10, then your jvm will throw one runtime exception called java.lang.IllegalArgumentException.
The priority of the thread is used if the operating system is using the priority based scheduling algorithm. Otherwise the priority of the thread will not be used. If you are defining any thread then some name will be provided for the corresponding thread. If you are not providing the name explicitly then jvm will provide some value like
Thread -
will start from 0.
If you want to provide the name of the thread, then you can use the constructor from the thread class that has the parameter of string type or you can call the following method of the thread class to provide name explicitly.
public final void setName(String);
If you want to access the name of the thread then you can call the following method with thread object
public final String getName();
If you are creating any thread object then it will be added into some ThreadGroup. In java ThreadGroup is a class available in java.lang package. It indicates the collection of the thread. One thread group will be created by default when the jvm starts with the name "main". If you are creating thread without providing thread group information the thread will be added in the main thread group. If you want you can create your own thread group and you can add threads into the current thread group.
If you want to add the thread into custom thread group then you need to use the thread class constructor that have the parameter has thread group.
You can access the thread group information of the thread by calling the following method.
public final ThreadGroup getThreadGroup();
If you are calling non-static synchronized method then the object that will be used to call the method will be locked. If you are calling a static synchronized method then the default object for the class will be locked.
If you are using synchronized block then the object that will be passed as parameter for the block will be blocked. If the object is blocked by one thread then it will not be used to access any synchronized method concurrently by other thread.
If you are calling a sleep() with the thread class then the thread will be moved to sleep state. If the thread is moved to sleep state and if the thread is moved to sleep state and if the thread is locking some object then the lock will not be released.
wait() & notify():-
wait and notify methods are available in the object class. If you are calling the wait() then it must be called from the synchronized context. When you are using the object to call the wait() then the thread that will be used to invoke the statement will be moved to wait state. If another thread is using same object to call the notify() then the thread from wait state will be moved to runnable state.
If you have multiple threads in the wait state that was using the corresponding object and if you want to move all the threads from wait to runnable state then you need to call notifyAll().
What is the difference between sleep() and wait()?
sleep() :- Sleep is defined in the thread class. It is a static method in the thread class. So, it can be used with the class name. If you are calling the sleep() and if the lock is enabled for any object it will not be released. Sleep() may be called from the synchronized context or from non-synchronized context. If you are calling the sleep() then you need to specify some time. After completing the specified time the thread will be moved to runnable state.
wait():- wait() is defined in object class. It is the instance method in the object class. So, you need to use some object to call the wait(). If you are calling the wait() then the lock will be released on the corresponding object. wait() must be called from the synchronized context. If you are calling the wait() then if you want you can specify the time or you can ignore. If you are specifying the time then after completing the specified time the thread will be moved to runnable state. If you want you can call notify() to move the state to runnable before completing the time. If you are calling the wait() without specifying time then explicitly you need to call the notify() to move the thread state to runnable state.
Thread Priority():-
Whenever the thread object will be created then for every thread object some integer value will be allocated. This value is known as priority for the thread. If you are not providing priority explicitly then it will get some default priority. The priority value will be between 1 to 10.
The priority some constant is defined in the thread class. One constant is
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
The thread with highest priority will be executed by the CPU first than the other threads. If you want to provide the priority for the thread explicitly then you can call the following methods wit the thread object.
public final void setPriority(int);
If you want to access the priority of the thread you can call this method.
public final int getPriority();
If you are providing the priority to the thread less than 1 or greater than 10, then your jvm will throw one runtime exception called java.lang.IllegalArgumentException.
The priority of the thread is used if the operating system is using the priority based scheduling algorithm. Otherwise the priority of the thread will not be used. If you are defining any thread then some name will be provided for the corresponding thread. If you are not providing the name explicitly then jvm will provide some value like
Thread -
If you want to provide the name of the thread, then you can use the constructor from the thread class that has the parameter of string type or you can call the following method of the thread class to provide name explicitly.
public final void setName(String);
If you want to access the name of the thread then you can call the following method with thread object
public final String getName();
If you are creating any thread object then it will be added into some ThreadGroup. In java ThreadGroup is a class available in java.lang package. It indicates the collection of the thread. One thread group will be created by default when the jvm starts with the name "main". If you are creating thread without providing thread group information the thread will be added in the main thread group. If you want you can create your own thread group and you can add threads into the current thread group.
If you want to add the thread into custom thread group then you need to use the thread class constructor that have the parameter has thread group.
You can access the thread group information of the thread by calling the following method.
public final ThreadGroup getThreadGroup();
Subscribe to:
Posts (Atom)