2016
Java Notes For Java Beginner

To execute the java program in multitasking way, this concept is called as multi-threading.
      Multi-threading is used to overcome the idle time of CPU.

What is the procedure to create thread? And how it is execute?.
Inherit the java.lang.Thread class in user defined class. so our class becomes thread class. override the public void run() method thread class in our class. Create the object of our class. This is the new born stage of thread. Active this object using start() method of thread class. It will implicitly calls the overrides run() method.

Note : the program execution and time sharing to the threads are totally depends upon local operating system and the processor.

class A extends Thread
{
 public void run()
 {
 for(int i=1;i<=50;i++);
 System.out.println("i is "+i);
 }
}
class B extends Thread
{
 public void run()
 {
 for(int j=1;j<=50;j++);
 System.out.println("j is "+j);
 }
 
 public static void main(String args[])
 {
 A a1=new A();
 B b1=new B();
 
 a1.start();
 b1.start();
 
 for(int k=1;k<=50;k++)
 System.out.println("k is "+k)
 }
}
Another procedure to create and execute the Threads.       Implements the java.lang.Runable interface into user defined class, override it's only one method public void run() in our class. Still this is not the thread object, because our class is not extending the thread class.       So create the separate object of java.lang.Thread class and assign the user defined object of this thread object call the start() method using thread class object.       It will be implicitly calls the override run() method of respective object classes.
class AA extends object implements runnable
{
 public void run()
 {
 for(int i=1;i<=50;i++);
 System.out.println("i is "+i);
 }
}
class BB implements runnable
{
 public void run()
 {
 for(int j=1;j<=50;j++);
 System.out.println("j is "+j);
 }
 
 public static void main(String args[])
 {
 AA a1=new AA();
 BB b1=new BB();
 
 Thread f1=new Thread(a1);
 Thread f2=new Thread(b1);
 
 t1.start();
 t2.start();
 }
}

Thread controlling methods:

Methods which are used to manage the execution controlling of threads at running position.

1) sleep()

This method is used to interrupt the running thread for the given the interval in milliseconds. this interrupted thread will completing the time interval. This method throws interval. This method throws Interrupted Exception so we have to handle it at calling time.
class A extends Thread
{
 public void run()
 {
 for(int j=1;j<=50;j++);
 {
  try{
   if(j==15)
   thread.sleep(500);
  }
  catch(Exception e)
  {}
 System.out.println("j is "+j);
 }
 }
}
class B extends Thread
{
 public void run()
 {
 for(int i=1;i<=50;i++);
 System.out.println("i is "+i);
 }
 
 public static void main(String args[])
 {
 A a1=new A();
 B b1=new B();
 
 a1.start();
 b1.start();
 }
}

2) setPriority()

This method is used to set the execution of threads. It is just a request to local operating system. There are three type of priority:
  1. Minimum priority => 1
  2. Normal priority => 5
  3. Maximum priority => 10
A a1=new A();
B b1=new B();
C c1=new C();

a1.setPriority(10);
b1.setPriority(7);
c1.setPriority(5);

a1.start();
b1.start();
c1.start();

3) join()

This method is used to join or connect or connect one thread to other, so that the other thread will complete it's execution and normal goes into dead stage.
What is the procedure to kill any thread ?.
Use stop() method to kill the thread abnormally, but it is deprecated from java library. and it's replacement method join().
Write a program to join one thread to another using join() method.
class D extends Thread
{
 public D extends Thread
 {
  for(int i=1;i<=5000;i++)
  System.out.println("i is "+i);
 }
}
class E extends Thread
{
 D d1;
 E(D d1) //pass the reference from
 {
  this.d1=d1;
 }
 public void run()
 {
 for(int j=1;j<=50;j++)
  {
   try{
    if(j==15)
    d1.join();
   }
   catch(Exception e);
   {}
   
   System.out.println("j is "+j);
  }
 }
 
 public static void main(String args[])
 {
  D d1=new D();
  E e1=new E()d1; //pass reference to constructor
  
  d1.start();
  e1.start();
 }
}

Synchronization :

To execute the thread based program in process way, this concept is called as synchronization.
Keyword "synchronized" is used to define such methods.
class Test
{
 Synchronized public void fun1(String args[])
 {
  System.out.println(" [ Hello ");
  try
  {
   Thread.sleep(1000);
  }
  catch(Exception e1)
  {}
  System.out.println(" [ "+msg);
  try
  {
   Thread.sleep(1000);
  }
  catch(Exception e)
  {}
  System.out.println(" ] world ] ");
 }
}
class AB extends Thread
{
 Test t1;
 AB(Test t1)
 {
  this.t1=t1;
 }
 
 public void run()
 {
  t1.fun1("java");
 }
}
class BC extends Thread
{
 Test t1;
 BC(Test t1)
 {
  this.t1=t1;
 }
 
 public void run()
 {
  t1.fun1("C++");
 }
 
 public static void main(String args[])
 {
  Test t1=new Test();
  
  AB a1=new AB(t1);
  BC b1=new BC(t1);
  
  a1.start();
  b1.start();
 }
}
Java Notes For Java Beginner
java packageThe set of built in classes stored under certain name and used as a library in java application, is called as package.
The super most package of JAVA hierarchy is "java".

java.lang package :

This package include all the basic classes which are required every java application.

Examples :
  • object
  • string
  • System
  • Exception
  • Thread
  • etc...

Note : This package is by default allocated to every java program.

java.io package :

This package are used to handle the input and output operation between editor-console, client-server and database handling program.
Examples :
  • DataInoutStream
  • BufferedReader
  • fileOutputStream
  • fileReader
  • etc...

java.net package :

This package contains all the network related classes and interfaces. They are used to handle the networking in client-server architecture.

Examples :
  • Socket
  • ServerSocket
  • INetAddress
  • DatagramPackage
  • etc...

java.sql package :

This package have data handling classes and interface.

Examples :
  • class:
    • DriverManager
  • Interfaces:
  • Connection
  • Statement
  • ResultSet
  • etc...

java.awt package :

The classes of this package are used to represent the graphical output of java application.

Examples :
  • Button
  • Lable
  • Textfield
  • frame
  • Panel
  • Checkbox
  • etc...



java.awt.event package:

The interfaces of this package are used to handle the events on graphical components.

Examples :
  • ActionListener
  • TextListener
  • WindowsListener
  • MouseListener
  • etc...


keyword "import" is used to include any of the above package in java application.

Example:

import java.io.*;
This statement will include all the classes and interfaces of this package ("*" mean all the classes)
import java.fileReader;
This statement will include any fileReader class in program other classes are not included.
import java.awt.*;
This statement will inlcude only awt classes.
It cannot import the event package. If we want to import event package element then we have to write.
import java.awt.event.*;

How to create and use it in another application ?
Declare the keyword 'package' in the heading selection of package class.
Declare the fully fully qualified path of this class that where to store it in memory (Hard disk).
Declare all the elements as public in and with class. Save and compile this application in working folder.
Then create the folder as mention with package keyword. Cut the .class file of this class and paste it into the created package folder. This is the package creation process.
Create another java application in working folder. Include the package using import keyword and fully qualified path of package in new application, Now we can with it's elements.
//Package Code
package Abc.Outer.Inner.Pack1;
public class A //public class important
{
 public void fun1()
 {
  System.out.println("Hello package class method");
 }
}

Working :

my working folder
D:\xyz
save and compile

Create folders as mention with package keyword in working folder.
D:\xyz\Abc\Outer\Inner\pack1
cute and paste the A.class file into pack1 folder.

Now file location will be
D:\xyz\Abc\Outer\Inner\pack1\A.class

Create another java application in which this package class should be imported.

//Program code
class B
{
 public void fun2()
 {
  System.out.println("Hello fun2");
 }
 public static void main(String args[])
 {
  A a1=new A();
  a1.fun1();
  B b1=new B();
  a1.fun2();
 }
}


Another process to create package and store the .class file into it.

Use the -d attribute with javac command to create the directories. It creates the specified packages or directories with package keyword.

The "."
Directories should be created in working folders. And then the .class file of package class is stored in specified folder.

JAVAC -d . A.JAVA
-d for directors
. for working folder