20 Mar 2013

Linux Commands.

Linux Commands can do lot more which are almost never possible in for a programming to do in windows I think!!!.

One thing about linux, never do anythink without really knowing what you are doing.Linux Will allow you to do anything if you are a root user.

These are the commands I recently explored on linux(02-02-2013)

1.fdisk - details about disks on the system.

2.fsck - Check file systems on the system.

3.ntfsundelete - to restore the deleted files.

4.scalpel -   Recover files or data fragments from a disk image.

5.export - to add  environment variable to linux.Ex:to add path /home/user/pathwherejars to your class path.
   export CLASSPATH=$CLASSPATH:"/home/user/pathwherejars"

6.env - to see all the environment variable to linux.

7.To find all the users on linux
    cat /etc/password | cut -d ":" -f1

8.To find userid for a specific username
    id -u "username"

9.To search for a string in the whole directory files.
    grep -R "StringToBeSearched" filepath

    -n - print the line number of the match
    -i - ignore case

10.To know the shell for linux

    ps -p $$ // last column gives me my shell

11.To Know machine architecture if it is 32 bit or 64

   #getconf LONG_BIT

12.To check the user (root) owned files.
 
   # find . -user root

13.Find -size +1000M
 
   # find -size +1000M
 
14.Check for words from English dictionary look command

   # look floor

15.web page over HTTP for the directory structure tree and can be accessed at port 8000 (http:ipaddress:8000)

   python -m SimpleHTTPServer

16. java -cp /data/Mine/Scripts/jars/mysql-connector-java-5.1.18-bin.jar:.

  Note the :. in the end of the cp path, to include the current directory.

17. netstat - command to know the network connections, ports, routing tables... on linux system.use "man netstat" on you linux system to know more.

18. Set date in linux system using the following command

     date -s "29 JUN 2014 23:55:00"

19.  sudo update-java-alternatives -s java-1.7.0-openjdk-amd64

     Update the java in your system with any version available in your system.

20. To find the OS distribution

     cat /etc/*release
221.

18 Mar 2013

Threading in Java

Well before going to threading in java, let us see what is a Thread.

Thread:Thread is a small process which will have same features as process but switching threads is simple and easy to the processor than switching processes,because process will have big tables associated with them for maintaining there states and lot more info needs to be save while switching and taking back the process but threads want to maintain such tables.

How do we create threads in Java?

Threads can be created in two ways in Java

1.Implement a Runnable Interface.
2.Extend Thread Class .

Implementing Runnable Interface appears to be more useful because as we know we can extend only one class in Java so if we extend Thread Class then we cannot extend any other Class so.

Knowing both exists is a good thing so let us learn both.

Example of creating a Thread through implementing Runnable interface.

      class Student implements Runnable
     {
         @Override
          public void run()
         {


             for( int i = 0 ; i < 10 ; i++ )
             {
                   System.out.println("Implementing Runnable interface");     
             }            
          }   
    
}


Runnable is a interface with only one method run() so we need to define that method in the implementing class (here Student).

Creating a Thread from the Threaded class(here Student).

      Student objStudent = new Student();//create an object of Class Student

      Thread threadStudent = new Thread(objStudent);//Pass object to Thread                                                                                          //constructor to make it a thread

How to start a Thread:

     threadStudent.start();

This will spawn a thread which will start executing method run() of Student class
asynchronously(term used to specify that the process wont happen in sequence but will happen parallely), that is now the statements in run() method will be executed parallely to statements following threadStudent.start().

Grouping threads


    We can Group threads while creating them, which will help us identify the threads by there groups.

     Create a thread group to which threads are added.

          ThreadGroup objStudGrp = new ThreadGroup("UGGroup");

     Create a thread with objStudGrp as thread group.

          Thread threadStudent = new Thread(objStudGrp,objStudent);

     
    We can find the count of Threads running in the current JVM  doing the following:

         Thread.activeCount();

    The above statement gives all the Threads running in the current JVM, but to differentiate  threads we created we can use the Thread Groups to find the count,

      objStudGrp.activeCount();

    We can make a thread wait, Say For Example:We created a two threads,

     Thread thread1 = new Thread(objStudGrp,new Student());

     Thread thread2 = new Thread(objStudGrp,new Student());
    
    start both the threads,

     thread1.start();
     thread2.start();

    Above statements starts the threads thread1 and thread2 i.e threads will execute what is there in run() method of class Student.If you want to make thread1 to wait for 10 seconds then you can do it using the following statement

   thread1.wait(10000); //thread1 waits for 10000 milliseconds i.e 10 seconds.

   If you want thread2 to wait till thread1 finishes then 

   thread1.start();//start both the threads
   thread2.start();

  
   synchronized( thread2 )
   {
         thread2.wait(); //make thread thread2 wait
   }

   // check if the thread thread1 is alive(still running)
   while( thread1.isAlive() )//returns true if the thread is started and not yet died.
   {
         Thread.sleep(1000);//let the current thread sleep for 1000 milliseconds before
                                       //checking again
   }

   
   //when the above  looping comes out then it implies thread1 is not alive anymore.
   //notify the thread2 to restart the execute
 
   thread2.notify();