Thursday, 30 July 2015

What will be the output of java program

public class Test {
    public static void main(String[] args) {
        System.out.println(Math.min(Double.MIN_VALUE, 0.0d));
    }
}
Answer: 

This question is tricky because unlike the Integer, where MIN_VALUE is negative, both the MAX_VALUE and MIN_VALUE of the Double class are positive numbers.

The Double.MIN_VALUE is 2^(-1074), a double constant whose magnitude is the least among all double values. So unlike the obvious answer, this program will print 0.0 because of Double.MIN_VALUE is greater than 0.

Difference between == and .equals()

They both differ very much in their significance. equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects.

Whereas the '==' operator is expected to check the actual object instances are same or not.

For example, lets say, you have two String objects and they are being pointed by two different reference variables s1 and s2.


 s1 = new String("abc");
 s2 = new String("abc");

Now, if you use the "equals()" method to check for their equivalence as


 if(s1.equals(s2))
      System.out.println("s1.equals(s2) is TRUE");
 else
      System.out.println("s1.equals(s2) is FALSE");
You will get the output as TRUE as the 'equals()' method check for the content equivality.

Lets check the '==' operator.


if(s1==s2)
     System.out.printlln("s1==s2 is TRUE");
   else
     System.out.println("s1==s2 is FALSE");

Now you will get the FALSE as output because both s1 and s2 are pointing to two different objects even though both of them share the same string content.

It is because of 'new String()' everytime a new object is created.

Try running the program without 'new String' and just with


   String s1 = "abc";
   String s2 = "abc";

You will get TRUE for both the tests.

Monday, 13 July 2015

Java Creating a Thread

In the most general sense, you create a thread by instantiating an object of type Thread. Java identifies two ways in which this can be accomplished:

(i) You can implement the Runnable interface.

(ii) You can extend the Thread class, itself.

1. Implementing Runnable


The easiest way to create a thread is to create a class that implements the Runnable interface. To implement Runnable, a class need only implement a single method called run():

public void run()

Inside the run() method, you will define the code that constitutes the new thread. The run() can call other methods, use other classes and declare variables, just like the main thread.

The only difference is that run() establishes the entry point for another, concurrent thread of execution within your program. This thread will end when run() returns.

After you create a class that implements Runnable, you will instantiate an object of type Thread from within that class using one of the following constructors:

Thread(Runnable threadObj)

Thread(Runnable threadObj, String threadName)

Here threadObj is the object whose run method is called and threadName is the name of the new thread.

After the new thread is created, it will not start running until you call start() method. The start() method puts the thread in the ready queue (runnable state). Whenever the thread gets scheduled its execution will start from the run() method.

Example:

class X implements Runnable
      public void run()
     { 
          for(int i=1; i<=5; i++)
          { 
               System.out.println("Child Thread: " + I);
          }

          System.out.println("Exiting child thread");

      }

}

class RunnableTest
     public static void main(String args[])
     { 
         X runnable  = new X();

         Thread ct = new Thread(runnable);

          ct.start();

          System.out.println("main thread exiting");

      }

}

Output:

main thread exiting

Child Thread: 1

Child Thread: 2

Child Thread: 3

Child Thread: 4

Child Thread: 5

Exiting child thread

Example:

class NewThread implements Runnable
     Thread t;

      NewThread()
     { 
         t = new Thread(this, "Demo Thread");

         System.out.println("Child thread: " + t);

         t.start();

     }

     public void run()
     { 
           for(int i=5; i>0; i--)
           {
                 try
                 { 
                       System.out.println("Child Thread: " + I);
                 }
                 catch(InterruptedException e)
                { 
                     System.out.println(e);
                }
      }

      System.out.println("Exiting child thread”);

      Thread.sleep(500);

    }

}

class ThreadDemo
     public static void main(String args[])
    { 
          new NewThread();
          try
          { 
               for(int n=5; n>0; n--)
               {
                   System.out.println("Main Thread: " + n);
                   Thread.sleep(1000);
               }
           }
           catch(InterruptedException e)
           {
                System.out.println("main thread interrupted");
            }
           System.out.println("main thread exiting");
     }
}

Output:

Child thread: Thread[Demo Thread,5,main]

Main Thread: 5

Child Thread: 5

Child Thread: 4

Main Thread: 4

Child Thread: 3

Child Thread: 2

Main Thread: 3

Child Thread: 1

Exiting child thread

Main Thread: 2

Main Thread: 1

main thread exiting

2. Extending Thread class

The second way to create a thread is to create a new class that extends Thread and then create an instance of that class. The extending class must override the run() method, which is the entry point for the new thread. It must also call start() to begin the execution of the new thread.

Example:

public class NewThread extends Thread
     NewThread()
     { 
            super("Demo Thread");
            System.out.println("Child thread: " + this);
            start();
      }

      public void run()
      { 
          for(int i=5; i>0; i--)
          { 
              try
              {
                  System.out.println("Child Thread: " + I);
                  sleep(500);
              }
              catch(InterruptedException e)
              {

              }

          }
          System.out.println("Exiting child thread");
          System.out.println(e);
     }
}

class ExtendThread
        public static void main(String args[])
        { 
            new NewThread();
            try
            { 
                for(int n=5; n>0; n--)
                {
                     System.out.println("Main Thread: " + n);
                     Thread.sleep(1000);
                }
             }
            catch(InterruptedException e)
            {
                 System.out.println("main thread interrupted");
            }
            System.out.println("main thread exiting");
       }
}

Output:

Child thread: Thread[Demo Thread,5,main]

Main Thread: 5

Child Thread: 5

Child Thread: 4

Main Thread: 4

Child Thread: 3

Child Thread: 2

Main Thread: 3

Child Thread: 1

Exiting child thread

Main Thread: 2

Main Thread: 1

main thread exiting

Note: If the thread is not assigned any name, it will be something like Thread-1, Thread-2, Thread-3 etc.

3. Choosing an Approach

The thread class defines several methods that can be overridden by a derived class. Out of these methods, the only one that must be overridden is run().

That is, of course, the same method required when you implement the Runnable interface. Many Java programmers feel that classes should be extended only when they are being enhanced or modified in some way.

So, if you will not be overriding any of Thread’s other methods, it is probably best simply to implement a Runnable interface.

Java The main thread

When a Java program starts up, one thread begins running immediately. This is usually called the main thread of your program because it is the one that is executed when your programs begins.

The main thread is the thread from which other “child” threads are created.

Although the main thread is created automatically when your program is started, it can be controlled through a Thread object. To do so, you must obtain a reference to it by calling the method currentThread(), which is public static member of Thread class. This method returns a reference to the thread in which it is called.

Once you have a reference to the main method, you can control it just like any other thread.

Example: The following example demonstrates how we can acquire reference of main thread and then access its properties using methods of Thread class.

class CurrentThreadDemo
     public static void main(String args[])
     { 
           Thread t = Thread.currentThread();
           System.out.println("Current thread: " + t);

           System.out.println("Name: " + t.getName());
           System.out.println("Priority: " + t.getPriority());

            t.setName("My Thread");

            t.setPriority(10);

            System.out.println("After name and priority change : " + t);

             System.out.println("New Name: " + t.getName());

             System.out.println("New Priority: " + t.getPriority());

             try
             { 
                    for(int n=5; n>0; n--)
                    { 
                         System.out.println(n);
                    }
             }
             catch(InterruptedException e)
             { 
                  System.out.println("main thread interrupted");
             }

             Thread.sleep(1000);
     }

}

Output:

Current thread: Thread[main,5,main]

Name: main

Priority: 5

After name and priority change : Thread[My Thread,10,main]

New Name: My Thread

New Priority: 10

5

4

3

2

1

The sleep() method in Thread might throw an InterruptedException, which is a checked exception. This would happen if some other thread wanted to interrupt this sleeping one.

Notice the output produced when t (thread reference) is used as an argument to println(). This displays in order: the name of the thread, its priority, and the name of its group. Its priority is 5, which is the default value, and main is also the name of the group of thread to which this thread belongs.

A thread group is a data structure that controls the state of a collection of threads as a whole.

Java The Thread class and the Runnable interface

Java’s multi-threading system is built upon the Thread class, its methods, and its companion interface, Runnable. This class belongs to package java.lang and hence there is no need of explicitly importing it.

Thread encapsulates a thread of execution, since you cannot directly refer to the internal state of a running thread, you will deal with it through its proxy, the Thread instance that spawned it. To create a new thread your program will either extend Thread class or implement the Runnable interface.

The Thread class defines several methods that help manage threads:


Java Messaging

When programming with most other languages, you must depend on the O.S. to establish communication between threads. This, of course, adds overhead.

By contrast, Java provides a clean, low-cost way for two or more threads to talk to each other, via calls to predefined methods that all objects have.

Java’s messaging system allows a thread to enter a synchronized method on an object, and then wait there until some other thread explicitly notifies it to come out of wait state.

Java Synchronization

Because multi-threading introduces an asynchronous behaviour to your programs, there must be a way for you to enforce synchronization when you need it.

For example, if you want two threads to communicate and share a complicated data structure, such as a linked list, you need some way to ensure that they do not conflict with each other.

That is, you must prevent one thread from writing data while another thread is in the middle of reading it. Java uses monitor for inter-thread synchronization.

You can think of a monitor as a very small box that can hold only one thread.

Once a thread enters a monitor, all other threads must wait until that thread exits the monitor. In this way, a monitor can be used to protect a shared asset from being manipulated by more than one thread at a time.

Most multi-threaded systems expose monitors as objects that your program must explicitly acquired and lock. Java provides a cleaner solution.

There is no class "monitor", instead, each object has its own implicit monitor that is automatically entered when one of the object's synchronized method is called.

Once a thread is inside a synchronized method no other thread can call any other synchronized method on the same object. This enables you to write very clear and concise multi-threaded code, because synchronization support is built into the language.

Java Thread Priorities

Java assigns to each thread a priority that determines how that thread should be treated with respect to the others.

Thread priorities are integers that specify the relative priority of one thread to another. As an absolute value, a priority is meaningless; a higher-priority thread does not run any faster than a lower-priority thread if it is the only thread running. Instead, a thread’s priority is used to decide when to switch from one running thread to next. This is called a ontext switch.

The rules that determine when a context switch takes place are simple:

  • A thread can voluntarily (on its own) relinquish control. This is done by explicitly yielding, sleeping or blocking on pending I/O. In this scenario, all other threads are examined, and normally the highest-priority thread that is ready to run is given the CPU.

  • A thread can be pre-empted by a higher-priority thread. In this case, a lower priority thread that does not yield the processor is simply pre-empted no matter what it is doing, by a higher priority thread. Basically, as soon as a higher-priority thread wants to run, it does. This is called preemptive multitasking. Some OS support non-preemptive priority based scheduling. In such case a high priority thread gets chance only when low priority thread completes.
  • In cases where two threads with the same priority are competing for CPU cycles, the situation is a bit complicated. For OS such as windows 98, threads of equal priority are normally time-sliced automatically in the round-robin fashion. For other types of OS's, thread of equal priority must voluntarily (on their own) yield (give) control to their peers. If they do not, the other threads will not run.

Note: Problems can arise from the differences in the way that O.S.’s context-switch threads of equal priority.

Java Thread Life Cycle (Thread States)

Thread exists in several states. A thread can be running. It can be ready to run as soon as it gets CPU time. A running thread can be suspended, which temporarily suspends its activity. A suspended thread can then be resumed, allowing it to pick up where it left off. A thread can be blocked when waiting for a resource. At any time, a thread can be terminated, which halts its execution immediately. Once terminated a thread cannot be resumed.

1. Newborn State

When we create a thread object, the thread is born and is said to be in newborn state. The thread is not yet scheduled for running. At this state, we can do only one of the following things with it:

1. Schedule it for running using the start() method.

2. Kill it using stop() method

If scheduled, it moves to runnable state. If we attempt to use any other method at this stage, an exception will be thrown.

2. Runnable State

The runnable state means that the thread is ready for execution and is waiting for the availability of the processor. That is, the thread has joined the queue of threads that are waiting for execution. If all threads are of equal priority, then normally they are given time slots for execution in round robin fashion. The thread that relinquishes control joins the queue at the end and again waits for its run. However, if we want a thread to relinquish control to another thread of equal priority before its turn comes, it can do so by invoking the yield() method.

3. Running State

Running means that the processor has given its time to the thread for its execution. The thread runs until it relinquishes control on its own or it is pre-empted by a higher priority thread.

A running thread may relinquish its control in one of the following situations:

  • Its time-slice is over
  • It is pre-empted by a higher priority thread
  • It yields i.e. voluntarily gives up control.
  • It has completed its execution
  • It is stopped by some other thread.
  • It has been suspended using suspend() method; a suspended thread can be revived by using the resume() method. This approach is useful when we want to suspend a thread for some time due to certain reason, but do not want to kill it.
  • It has been made to sleep. We can put a thread to sleep for a specified time period using the static method sleep(time) of Thread class where time is in milli-seconds. This means that the thread is out of the queue during the time period. The thread re-enters the runnable state as soon as this time period is elapsed.
  • It has been told to wait until some event occurs. This is done using the wait() method. The thread can be scheduled to run again using the notify() method.
  • It is performing some I/O operation.

4. Blocked State

A thread is said to be blocked when it is prevented from entering into the runnable state and subsequently the running state. This happens when the thread is suspended, sleeping or waiting in order to satisfy certain requirements. A blocked thread is considered “not runnable” but not dead and therefore fully qualified to run again.

5. Dead State

Every thread has a life cycle. A running thread ends its life when it has completed executing its run() method. It has a natural death. However, we can kill it by sending the stop message to it at any state thus causing a premature death. A thread can be killed as soon as it is born, or while it is running, or even when it is in “not runnable” (blocked condition).

Wednesday, 8 July 2015

Java Multi-Threaded Programming

1. Introduction

A thread is a single flow of control within a program. Thread is very much similar to a process. In fact, the thread is also called a lightweight process.

1.1 Thread v/s Process

Normally different processes occupy different memory space. When the CPU shifts from one process to another process, the state of the currently running process is saved and the state of another process is restored. No useful work is being done during state switch. This is referred to as context switch. The context switch time should be as less as possible to maximize the CPU utilization.

Threads are also like independent processes but they share the same memory and state. The separate threads also have separate state but the state is very small as compared to process state.

The state may contain just program counter and stack pointer. So switching from one thread to another thread takes very little time. Thus the context switch time for threads is very small as compared to the process. Hence CPU utilization is high in case of multiple threads as compared to the utilization in case of multiple processes. Threads are also referred to as light-weight process.

1.2 Multi-Threaded program

A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. Thus, multi-threading is a specialized form of multi-tasking.

For example, a program may have three threads:

  • One handling the printing
  • One handling the editing
  • One downloading a file from the Internet.

All these threads might be running concurrently thus maximizing the CPU utilization.

1.3 Process-based Multi-tasking


Most of the operating systems allow you to run two or more programs at the same time. It is referred to as process-bases multi-tasking. For example, you can run a Java program and at the same time you may be editing a word document. The process-based multi-tasking ensures that there will be some program to be executed most of the time so it increases the CPU utilization.  

In process-based multi-tasking, a program is the smallest unit of code that can be dispatched by the scheduler.

1.4 Thread-based multi-tasking

Thread is the smallest unit of execution in a thread-based multi-tasking environment. A process can be divided into a number of threads executing concurrently. This allows us to handle more than one task concurrently in a single program. For example, you can edit a word document and at the same time print a portion of it or another document.

Thread-based multi-tasking improves CPU utilization just like process-based multi-tasking. But at the same time it effectively speeds up the execution of a single program by executing its different parts concurrently in separate threads.

Thus process based multi-tasking deals with the “big picture”, and thread-based multi-tasking handles the details.

1.5 The Java Thread Model

The Java run-time system depends on threads for many things, and all the class libraries are designed with multi-threading in mind. In fact, Java uses threads to enable the entire environment to be asynchronous. This helps reduce inefficiency by preventing the waste of CPU cycles.

Single threaded systems use an approach called an event loop with polling. In this model, a single thread of control runs in an infinite loop, polling a single event queue to decide what to do next. In a single threaded environment, when a thread blocks (that is, suspends execution) because it is waiting for some resource, the entire program stops running.

The benefit of Java's multi-threading is that the main loop/polling mechanism is eliminated.

When a thread blocks in Java program, only the single thread that is blocked pauses, all other threads continue to run.