Saturday, November 4, 2017

5--Thread_ 1.1

Thread

1)      Thread
(a)   Thread Basic
(b)   DeadLock conditions
(c)    Synchronization
2)      JVM, JDK,JRE
3)      General Java Question(Final, static, Abstract)
4)      Design Pattern
(a)   Singleton
(b)   Factory Design pattern
(c)    Abstract Factory Design pattern
5)      Serialization


volatile Java variable
Declaring a volatile Java variable means: The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory"; Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself.



Important points on Volatile keyword in Java

1. The volatile keyword in Java is only application to a variable and using volatile keyword with class and method is illegal.

2. 
volatile keyword in Java guarantees that value of the volatile variable will always be read from main memory and not from Thread's local cache.

3. In Java reads and writes are 
atomic for all variables declared using Java volatile keyword (including long and double variables).

4. Using the 
volatile keyword in Java on variables reduces the risk of memory consistency errors because any write to a volatile variable in Java establishes a happens-before relationship with subsequent reads of that same variable.

5. From Java 5 changes to a volatile variable are always visible to other threads. What's more, it also means that when a thread reads a volatile variable in Java, it sees not just the 
latest change to the volatile variable but also the side effects of the code that led up the change.

6. Reads and writes are atomic for reference variables are for most primitive variables (all types except long and double) even without the use of 
volatile keyword in Java.

7. An access to a volatile variable in Java never has a chance to block, since we are only doing a simple read or write, so unlike a synchronized block we will never hold on to any lock or wait for any 
lock.

8. 
Java volatile variable that is an object reference may be null.

9. 
Java volatile keyword doesn't mean atomic, its common misconception that after declaring volatile ++ will be atomic, to make the operation atomic you still need to ensure exclusive access using synchronized method or block in Java.

10. If a variable is not shared between multiple threads, you don't need to use volatile keyword with that variable.



 

Java Threads Examples

  • Threading is a facility to allow multiple tasks to run concurrently within a single process. Threads are independent, concurrent execution through a program, and each thread has its own stack.
  • In Java threads can be implemented in two ways. One is by 'Extending Thread Class' and the other way is by 'Implementing Runnable Interface'
  • Extending Thread Class is required to 'override run()' method. The run method contains the actual logic to be executed by thread.
  • Creation of thread object never starts execution, we need to call 'start()' method to run a thread. Examples gives you more details. Other methods supported by Threads are given below.
  • join(): It makes to wait for this thread to die. You can wait for a thread to finish by calling its join() method.
  • sleep(): It makes current executing thread to sleep for a specified interval of time. Time is in milli seconds.
  • yield(): It makes current executing thread object to pause temporarily and gives control to other thread to execute.
  • notify(): This method is inherited from Object class. This method wakes up a single thread that is waiting on this object's monitor to acquire lock.
  • notifyAll(): This method is inherited from Object class. This method wakes up all threads that are waiting on this object's monitor to acquire lock.
  • wait(): This method is inherited from Object class. This method makes current thread to wait until another thread invokes the notify() or the notifyAll() for this object.

1.3                     Life Cycle of a Thread:

A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. Following diagram shows complete life cycle of a thread.
Above-mentioned stages are explained here:
·        New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.
·        Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
·        Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.
·        Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
·        Terminated ( Dead ): A runnable thread enters the terminated state when it completes its task or otherwise terminates.

Thread Priorities:

Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled.
Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5).
Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and very much platform dependent.

Create Thread by Implementing Runnable Interface:

If your class is intended to be executed as a thread then you can achieve this by implementing Runnable interface. You will need to follow three basic steps:

Step 1:

As a first step you need to implement a run() method provided by Runnableinterface. This method provides entry point for the thread and you will put you complete business logic inside this method. Following is simple syntax of run() method:
publicvoid run()

1.3.1        Step 2:

At second step you will instantiate a Thread object using the following constructor:
Thread(RunnablethreadObj,StringthreadName);
Where, threadObj is an instance of a class that implements the Runnableinterface and threadName is the name given to the new thread.

1.3.2        Step 3

Once Thread object is created, you can start it by calling start( ) method, which executes a call to run( ) method. Following is simple syntax of start() method:
void start();
 

 

 

Example:

Here is an example that creates a new thread and starts it running:
classRunnableDemoimplementsRunnable{
privateThread t;
privateStringthreadName;
 
RunnableDemo(String name){
threadName= name;
System.out.println("Creating "+threadName);
}
publicvoid run(){
System.out.println("Running "+threadName);
try{
for(inti=4;i>0;i--){
System.out.println("Thread: "+threadName+", "+i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
}catch(InterruptedException e){
System.out.println("Thread "+threadName+" interrupted.");
}
System.out.println("Thread "+threadName+" exiting.");
}
 
publicvoid start ()
{
System.out.println("Starting "+threadName);
if(t ==null)
{
         t =newThread(this,threadName);
t.start();
}
}
 
}
 
publicclassTestThread{
publicstaticvoid main(Stringargs[]){
 
RunnableDemo R1 =newRunnableDemo("Thread-1");
R1.start();
 
RunnableDemo R2 =newRunnableDemo("Thread-2");
R2.start();
}
}
This would produce the following result:
CreatingThread-1
StartingThread-1
CreatingThread-2
StartingThread-2
RunningThread-1
Thread:Thread-1,4
RunningThread-2
Thread:Thread-2,4
Thread:Thread-1,3
Thread:Thread-2,3
Thread:Thread-1,2
Thread:Thread-2,2
Thread:Thread-1,1
Thread:Thread-2,1
ThreadThread-1 exiting.
ThreadThread-2 exiting.

 

Create Thread by Extending Thread Class:

The second way to create a thread is to create a new class that extendsThread class using the following two simple steps. This approach provides more flexibility in handling multiple threads created using available methods in Thread class.

Step 1

You will need to override run( ) method available in Thread class. This method provides entry point for the thread and you will put you complete business logic inside this method. Following is simple syntax of run() method:
publicvoid run()

Step 2                   

Once Thread object is created, you can start it by calling start( ) method, which executes a call to run( ) method. Following is simple syntax of start() method:
void start();

Example:

Here is the preceding program rewritten to extend Thread:
classThreadDemoextendsThread{
privateThread t;
privateStringthreadName;
 
ThreadDemo(String name){
threadName= name;
System.out.println("Creating "+threadName);
}
publicvoid run(){
System.out.println("Running "+threadName);
try{
for(inti=4;i>0;i--){
System.out.println("Thread: "+threadName+", "+i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
}catch(InterruptedException e){
System.out.println("Thread "+threadName+" interrupted.");
}
System.out.println("Thread "+threadName+" exiting.");
}
 
publicvoid start ()
{
System.out.println("Starting "+threadName);
if(t ==null)
{
         t =newThread(this,threadName);
t.start();
}
}
 
}
 
publicclassTestThread{
publicstaticvoid main(Stringargs[]){
 
ThreadDemo T1 =newThreadDemo("Thread-1");
T1.start();
 
ThreadDemo T2 =newThreadDemo("Thread-2");
T2.start();
}
}
This would produce the following result:
CreatingThread-1
StartingThread-1
CreatingThread-2
StartingThread-2
RunningThread-1
Thread:Thread-1,4
RunningThread-2
Thread:Thread-2,4
Thread:Thread-1,3
Thread:Thread-2,3
Thread:Thread-1,2
Thread:Thread-2,2
Thread:Thread-1,1
Thread:Thread-2,1
ThreadThread-1 exiting.
ThreadThread-2 exiting.

Thread Methods:

Following is the list of important methods available in the Thread class.
SN
Methods with Description
1
public void start()
Starts the thread in a separate path of execution, then invokes the run() method on this Thread object.
2
public void run()
If this Thread object was instantiated using a separate Runnable target, the run() method is invoked on that Runnable
object.
3
public final void setName(String name)
Changes the name of the Thread object. There is also a getName() method for retrieving the name.
4
public final void setPriority(int priority)
Sets the priority of this Thread object. The possible values are between 1 and 10.
5
public final void setDaemon(boolean on)
A parameter of true denotes this Thread as a daemon thread.
6
public final void join(long millisec)
The current thread invokes this method on a second thread, causing the current thread to block until the second thread terminates or the specified number of milliseconds passes.
7
public void interrupt()
Interrupts this thread, causing it to continue execution if it was blocked for any reason.
8
public final booleanisAlive()
Returns true if the thread is alive, which is any time after the thread has been started but before it runs to completion.
The previous methods are invoked on a particular Thread object. The following methods in the Thread class are static. Invoking one of the static methods performs the operation on the currently running thread.
SN
Methods with Description
1
public static void yield()
Causes the currently running thread to yield to any other threads of the same priority that are waiting to be scheduled.
2
public static void sleep(long millisec)
Causes the currently running thread to block for at least the specified number of milliseconds.
3
public static booleanholdsLock(Object x)
Returns true if the current thread holds the lock on the given Object.
4
public static Thread currentThread()
Returns a reference to the currently running thread, which is the thread that invokes this method.
5
public static void dumpStack()
Prints the stack trace for the currently running thread, which is useful when debugging a multithreaded application.

Example:

The following ThreadClassDemo program demonstrates some of these methods of the Thread class. Consider a class DisplayMessage which implementsRunnable:
// File Name : DisplayMessage.java
// Create a thread to implement Runnable
publicclassDisplayMessageimplementsRunnable
{
privateString message;
publicDisplayMessage(String message)
{
this.message= message;
}
public  void run()
{
while(true)
{
System.out.println(message);
}
}
}
Following is another class which extends Thread class:
// File Name : GuessANumber.java
// Create a thread to extentd Thread
public  class  GuessANumber  extends  Thread
{
private  int  number;
public  GuessANumber(int number)
{
this.number= number;
}
public void  run()
{
int counter =0;
int guess =0;
do
{
guess=(int)(Math.random()*100+1);
System.out.println(this.getName()
+" guesses "+ guess);
counter++;
}while(guess != number);
System.out.println("** Correct! "+this.getName()
+" in "+ counter +" guesses.**");
}
}
Following is the main program which makes use of above defined classes:
// File Name : ThreadClassDemo.java
public  class  ThreadClassDemo
{
public  static  void   main(String[]args)
{
Runnable hello =newDisplayMessage("Hello");
Thread thread1 =newThread(hello);
thread1.setDaemon(true);
thread1.setName("hello");
System.out.println("Starting hello thread...");
thread1.start();
 
Runnable bye =newDisplayMessage("Goodbye");
Thread thread2 =newThread(bye);
thread2.setPriority(Thread.MIN_PRIORITY);
thread2.setDaemon(true);
System.out.println("Starting goodbye thread...");
thread2.start();
 
System.out.println("Starting thread3...");
Thread thread3 =newGuessANumber(27);
thread3.start();
try
{
thread3.join();
}catch(InterruptedException e)
{
System.out.println("Thread interrupted.");
}
System.out.println("Starting thread4...");
Thread thread4 =newGuessANumber(75);
 
               thread4.start();
System.out.println("main() is ending...");
}
}
This would produce the following result. You can try this example again and again and you would get different result every time.
Starting hello thread...
Starting goodbye thread...
Hello
Hello
Hello
Hello
Hello
Hello
Goodbye
Goodbye
Goodbye
Goodbye
Goodbye
.......

1.4                     Major Java Multithreading Concepts:

While doing Multithreading programming in Java, you would need to have the following concepts very handy:
·        Handling thread deadlock
·        Major thread operations

Can we start a thread twice

No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.
Let's understand it by the example given below:
1.      public class TestThreadTwice1 extends Thread{  
2.       public void run(){  
3.         System.out.println("running...");  
4.       }  
5.       public static void main(String args[]){  
6.        TestThreadTwice1 t1=new TestThreadTwice1();  
7.        t1.start();  
8.        t1.start();  
9.       }  
10.  }  
       running
       Exception in thread "main" java.lang.IllegalThreadStateException

 

----------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------



Synchronization in Java

Synchronization in java is the capability to control the access of multiple threads to any shared resource.
Java Synchronization is better option where we want to allow only one thread to access the shared resource.
Why use Synchronization
The synchronization is mainly used to
  1. To prevent thread interference.
  2. To prevent consistency problem.


Types of Synchronization
There are two types of synchronization
  1. Process Synchronization
  2. Thread Synchronization
Here, we will discuss only thread synchronization.


Thread Synchronization
There are two types of thread synchronization mutual exclusive and inter-thread communication.
  1. Mutual Exclusive
    1. Synchronized method.
    2. Synchronized block.
    3. static synchronization.
  2. Cooperation (Inter-thread communication in java)


Mutual Exclusive
Mutual Exclusive helps keep threads from interfering with one another while sharing data. This can be done by three ways in java:
  1. by synchronized method
  2. by synchronized block
  3. by static synchronization


Concept of Lock in Java
Synchronization is built around an internal entity known as the lock or monitor. Every object has an lock associated with it. By convention, a thread that needs consistent access to an object's fields has to acquire the object's lock before accessing them, and then release the lock when it's done with them.
From Java 5 the package java.util.concurrent.locks contains several lock implementations.
Understanding the problem without Synchronization
In this example, there is no synchronization, so output is inconsistent. Let's see the example:
1.      Class Table{  
2.        
3.      void printTable(int n){//method not synchronized  
4.         for(int i=1;i<=5;i++){  
5.           System.out.println(n*i);  
6.           try{  
7.            Thread.sleep(400);  
8.           }catch(Exception e){System.out.println(e);}  
9.         }  
10.   }  
11.  }  
12.    
13.  class MyThread1 extends Thread{  
14.  Table t;  
15.  MyThread1(Table t){  
16.  this.t=t;  
17.  }  
18.  public void run(){  
19.  t.printTable(5);  
20.  }  
21.    
22.  }  
23.  class MyThread2 extends Thread{  
24.  Table t;  
25.  MyThread2(Table t){  
26.  this.t=t;  
27.  }  
28.  public void run(){  
29.  t.printTable(100);  
30.  }  
31.  }  
32.    
33.  class TestSynchronization1{  
34.  public static void main(String args[]){  
35.  Table obj = new Table();//only one object  
36.  MyThread1 t1=new MyThread1(obj);  
37.  MyThread2 t2=new MyThread2(obj);  
38.  t1.start();  
39.  t2.start();  
40.  }  
41.  }  
Output: 5
       100
       10
       200
       15
       300
       20
       400
       25
       500
 Java synchronized method
If you declare any method as synchronized, it is known as synchronized method.
Synchronized method is used to lock an object for any shared resource.
When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.
1.      //example of java synchronized method  
2.      class Table{  
3.       synchronized void printTable(int n){//synchronized method  
4.         for(int i=1;i<=5;i++){  
5.           System.out.println(n*i);  
6.           try{  
7.            Thread.sleep(400);  
8.           }catch(Exception e){System.out.println(e);}  
9.         }  
10.    
11.   }  
12.  }  
13.    
14.  class MyThread1 extends Thread{  
15.  Table t;  
16.  MyThread1(Table t){  
17.  this.t=t;  
18.  }  
19.  public void run(){  
20.  t.printTable(5);  
21.  }  
22.    
23.  }  
24.  class MyThread2 extends Thread{  
25.  Table t;  
26.  MyThread2(Table t){  
27.  this.t=t;  
28.  }  
29.  public void run(){  
30.  t.printTable(100);  
31.  }  
32.  }  
33.    
34.  public class TestSynchronization2{  
35.  public static void main(String args[]){  
36.  Table obj = new Table();//only one object  
37.  MyThread1 t1=new MyThread1(obj);  
38.  MyThread2 t2=new MyThread2(obj);  
39.  t1.start();  
40.  t2.start();  
41.  }  
42.  }  
Output: 5
       10
       15
       20
       25
       100
       200
       300
       400
       500
       


Example of synchronized method by using annonymous class
In this program, we have created the two threads by annonymous class, so less coding is required.
1.      //Program of synchronized method by using annonymous class  
2.      class Table{  
3.       synchronized void printTable(int n){//synchronized method  
4.         for(int i=1;i<=5;i++){  
5.           System.out.println(n*i);  
6.           try{  
7.            Thread.sleep(400);  
8.           }catch(Exception e){System.out.println(e);}  
9.         }  
10.    
11.   }  
12.  }  
13.    
14.  public class TestSynchronization3{  
15.  public static void main(String args[]){  
16.  final Table obj = new Table();//only one object  
17.    
18.  Thread t1=new Thread(){  
19.  public void run(){  
20.  obj.printTable(5);  
21.  }  
22.  };  
23.  Thread t2=new Thread(){  
24.  public void run(){  
25.  obj.printTable(100);  
26.  }  
27.  };  
28.    
29.  t1.start();  
30.  t2.start();  
31.  }  
32.  }  
Output: 5
       10
       15
       20
       25
       100
       200
       300
       400
       500
       
Difference between wait and sleep?
Let's see the important differences between wait and sleep methods.
wait()
sleep()
wait() method releases the lock
sleep() method doesn't release the lock.
is the method of Object class
is the method of Thread class
is the non-static method
is the static method
should be notified by notify() or notifyAll() methods
after the specified amount of time,
sleep is completed.



Concept behind putting wait(),notify() methods in Object class [duplicate]


I am just having hard time to understand concept behind putting wait() in object class For this questions sake consider as if wait() and notifyAll() are in thread class
In the Java language, you wait() on a particular instance of an Object – a monitor assigned to that object to be precise. If you want to send a signal to one thread that is waiting on that specific object instance then you call notify() on that object. If you want to send a signal to all threads that are waiting on that object instance, you use notifyAll() on that object.
If wait() and notify() were on the Thread instead then each thread would have to know the status of every other thread. How would thread1 know that thread2 was waiting for access to a particular resource? If thread1 needed to call thread2.notify() it would have to somehow find out that thread2 was waiting. There would need to be some mechanism for threads to register the resources or actions that they need so others could signal them when stuff was ready or available.
In Java, the object itself is the entity that is shared between threads which allows them to communicate with each other. The threads have no specific knowledge of each other and they can run asynchronously. They run and they lock, wait, and notify on the object that they want to get access to. They have no knowledge of other threads and don't need to know their status. They don't need to know that it is thread2 which is waiting for the resource – they just notify on the resource and whomever it is that is waiting (if anyone) will be notified.
In Java, we then use lock objects as synchronization, mutex, and communication points between threads. We synchronize on a lock object to get mutex access to an important code block and to synchronize memory. We wait on an object if we are waiting for some condition to change – some resource to become available. We notify on an object if we want to awaken sleeping threads.
// locks should be final objects so the object instance we are synchronizing on,
// never changes
private final Object lock = new Object();
...
// ensure that the thread has a mutex lock on some key code
synchronized (lock) {
    ...
    // i need to wait for other threads to finish with some resource
    // this releases the lock and waits on the associated monitor
    lock.wait();
    ...
    // i need to signal another thread that some state has changed and they can
    // awake and continue to run
    lock.notify();
}
There can be any number of lock objects in your program – each locking a particular resource or code segment. You might have 100 lock objects and only 4 threads. As the threads run the various parts of the program, they get exclusive access to one of the lock objects. Again, they don't have to know the running status of the other threads.
This allows you to scale up or down the number of threads running in your software as much as you want. You find that the 4 threads is blocking too much on outside resources, then you can increase the number. Pushing your battered server too hard then reduce the number of running threads. The lock objects ensure mutex and communication between the threads independent on how many threads are running.

package com.Thread;

class Process implements Runnable{

       @Override
       public void run() {

              int arr[] ={1,2,3,4,5,6};
             
              for(int i=0; i<arr.length;i++){
                    
                    
                     System.out.println("Numbers :" + arr[i]);
                     try {
                           Thread.sleep(1000);
                     } catch (InterruptedException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                     }
              }
             
       }
      
}

public class ThreadTest {

       public static void main(String[] args) {

              System.out.println("Ali");
              Thread th_1 = new Thread(new Process());
              th_1.start();
             
              Thread th_2 = new Thread(new Process());
              th_2.start();
             
       }

}
OUTPUT:-

Ali
Numbers :1
Numbers :1
Numbers :2
Numbers :2
Numbers :3
Numbers :3
Numbers :4
Numbers :4
Numbers :5
Numbers :5
Numbers :6
Numbers :6
New Thread Class
package com.Thread;

class ThreadClass_A extends Thread {

       public void run() {

              for (int i = 0; i < 5; i++) {

                     System.out.println("Thread Name :" + currentThread() + " " + i);
              }

       }

}

public class ThreadClassTest {

       public static void main(String[] args) {

              ThreadClass_A tca = new ThreadClass_A();
              tca.start();
              try {
                     tca.join();
              } catch (InterruptedException e) {
                     e.printStackTrace();
              }
              for (int i = 0; i < 5; i++) {

                     System.out.println("T Count "+ i);
              }

       }

}
OUTPUT:-
Thread Name :Thread[Thread-0,5,main] 0
Thread Name :Thread[Thread-0,5,main] 1
Thread Name :Thread[Thread-0,5,main] 2
Thread Name :Thread[Thread-0,5,main] 3
Thread Name :Thread[Thread-0,5,main] 4
T Count 0
T Count 1
T Count 2
T Count 3
T Count 4

New Class with Thread and Runnable()

package com.Thread;

public class ThreadRunnable {

       public static void main(String[] args) {

              Thread thread = new Thread(new Runnable(){

                     @Override
                     public void run() {
                           for(int i=0; i<5; i++){
                           System.out.println("Thread : "+ i);
                     }
                     }
              });
             
              thread.start();
       }
}
OUTPUT:-
Thread : 0
Thread : 1
Thread : 2
Thread : 3

Thread : 4



Thread Sleep

try {
Thread.sleep(5*60*1000); // Sleep for 5 minutes
} catch (InterruptedException ex) { }

Notice:- that the sleep() method can throw a checked InterruptedException
(you'll usually know if that is a possibility, since another thread has to explicitly do
the interrupting), so you must acknowledge the exception with a handle or declare.
Typically, you wrap calls to sleep() in a try/catch, as in the preceding code
One Thread Print number’s of Array and another thread Print sum of that elements of Array


package com.Thread;

public class ThreadTest_Array {

       public static void main(String[] args) {

              int arr[] = { 1, 2, 3, 4, 5 };
              Thread thread_1 = new Thread(new Runnable() {

                     @Override
                     public void run() {
                           for (int i = 0; i < arr.length; i++) {

                                  System.out.println("Element in array : " + arr[i]);
                           }

                     }

              });

              Thread thread_2 = new Thread(new Runnable() {

                     @Override
                     public void run() {
                           int sum = 0;
                           for (int i = 0; i < arr.length; i++) {

                                  sum = sum + arr[i];

                           }
                           System.out.println("Sum of elements in array : " + sum);
                     }

              });

              thread_1.start();
              thread_2.start();
             
              try {
                     thread_2.join();
              } catch (InterruptedException e) {
                     e.printStackTrace();
              }
       }

}
OUTPUT:-
Sum of elements in array : 15
Element in array : 1
Element in array : 2
Element in array : 3
Element in array : 4
Element in array : 5

Array read half element of Array and another thread read half part of array

package com.Thread;

public class HalfElementOfArrayOneThread {

       public static void main(String[] args) {

              int arr[] = {1,2,3,4,5,6};
              Thread thread_1 = new Thread(new Runnable(){

                     @Override
                     public void run() {
                          
                           for(int i=0;i<arr.length/2; i++){
                                  System.out.println("Element of Array-1 :"+ arr[i]);
                           }
                            SysSystem.out.println("---------------------");------");
                     }
                    
              });
              Thread thread_2 = new Thread(new Runnable(){

                     @Override
                     public void run() {
                          
                           for(int i=arr.length/2 ;i<arr.length; i++){
                                  System.out.println("Element of Array-2 :"+ arr[i]);
                           }
                          
                     }
                    
              });
             
              thread_1.start();
             
             
              try {
                     thread_1.join();
                     //thread_2.join();
              } catch (InterruptedException e) {
                     e.printStackTrace();
              }
              thread_2.start();
       }

}
OUTPUT:-
Element of Array-1 :1
Element of Array-1 :2
Element of Array-1 :3
---------------------
Element of Array-2 :4
Element of Array-2 :5
Element of Array-2 :6
-------------------------------------------------------------------------------------------------------
JVM , JDK,JRE
JVM
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed.
JVMs are available for many hardware and software platforms. JVM, JRE and JDK are platform dependent because configuration of each OS differs. But, Java is platform independent.
The JVM performs following main tasks:
·         Loads code
·         Verifies code
·         Executes code
·         Provides runtime environment


JRE
JRE is an acronym for Java Runtime Environment. It is used to provide runtime environment. It is the implementation of JVM.It physically exists. It contains set of libraries + other files that JVM uses at runtime.
Implementations of JVMs are also actively released by other companies besides Sun Micro Systems.

JDK

JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.
-------------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment