Skip to main content

Posts

Java Annotations

Self-Descriptive Coding in java. Java has introduced the feature called annotation through which elements of a program can be marked to contain some Meta data. This Meta data may be available at compile time or at running time. Now it is possible to provide some information for methods, fields, classes etc. that will be available at processing time. You can annotate your method telling to calling method how to call your method. You can annotate your class to tell something to class loader how to cerate the object of class and for what the object should be used. You can declare fields in you class those may be annotated to get their value from specified source. New version of java and j2ee has been updated to use annotations. Like EJB, webs services, JPA(java persistence API) etc. New version EJB3.x has been simplified to create the EJB without using any XML file. Because the information required to EJB container are provided by the annotation. EJB programmer annotates his class to prov...

Thread Synchronization using synchronized blocks

In the previous example the Shared class provides synchronized method for threads. Threads do nothing so if share class removes the synchronized then thread object will not be able to access the print method synchronously.             There is second mechanism by which the thread can provided the synchronization to the any object or block. class  MyThread1  extends   Thread {         Shared s;     MyThread1(Shared s)     {          this .s = s;     }          public   void  run()     {              synchronized (s)         {            ...

Thread Synchronization

The mechanism to accomplish the mutual access of shared resource for two or more thread is referred as the synchronization .when two or more thread access single resource then it is ensured that only one thread should access it at a time. The core concept of synchronization is the monitor. Only one thread can get the access to monitor at a time or we can say only one thread can enter in the monitor at a time. If one thread using the shared resource than it is said to be in the monitor and it acquires the lock on the monitor so no anther thread has the permission to access the shared recourse. When first thread finishes its execution then it release the lock from the monitor and frees the monitor to use by another thread. In the java all the object have their implicit monitor. So it is easy to synchronize any object or any method of object. There are two ways in which execution of code can be synchronized: ·          synchronized methods ·  ...

Thread priority

Thread priorities are integer values between 1 and 10 used by the thread scheduler to assign the CPU time for the thread for execute their task. When many threads are executed by the thread scheduler, then the thread scheduler uses the priority associated with thread to decide that how much CPU time should be assigned for particular thread. The thread having the higher priority will execute fast then lower priority because the CPU time assigned to higher priority thread is more then lower priority thread. Some constants are defined in the thread class for specify the priority values and they can be used to assign the relative priorities among the threads. Those are below with their access specifier and access modifier. public static final int MAX_PRIORITY 10 public static final int MIN_PRIORITY 1 public static final int NORM_PRIORITY 5 The execution of multiple threads on a single CPU, in some order, is...

Joining the threads

If you want that one thread should waits until another thread return from its running process or it die You can do it by using the join() method. If join method is called then the execution stops on that line of code until the thread on which join() called is died . //join //Author @ Hemraj class  MyThread  extends   Thread {      String  msg;     MyThread( String  msg)     {          this .msg = msg;     }      public   void  run()     {          for ( int  i = 0;i < 10;i ++ )         {                  try             {      ...

Checking the thread’s life (isAlive())

To check whether a particular thread is in live state or not, at any time we use isAlive() method. This method returns true if the thread for which it called is in the living state and it returns false if the thread is not alive. //isAlive  class  MyThread  extends   Thread {      public   void  run()     {          for ( int  i = 0;i < 10;i ++ )         {                  try             {                 sleep(500);             } catch ( InterruptedException  e){}         }    ...

Basic Thread’s States

Life cycle of the thread is valuable in the large multithreading application. State of a thread specifies the current activities of the thread at any movement. There are four basic state of the thread. 1.      New – when the object of the class that representing a thread after creating the object, is created then before call the start method the thread can’t run. So it is called new state of the thread or in other words we can say that the thread is said to be in new state. It means the thread has been created and it is ready to run. 2.      Runnable -- Thread exists in several states. A thread that has been just created is in the new state. The thread remains in this state until the thread’s start method is called. This causes the thread to enter the runnable (ready) state when the system assigns a processor to the thread Running – A thread is said to be in the running state if it is executing instructions. The instructions are placed...