Saturday, April 16, 2016

Java Threading - Three ways to create java threads

In programming, threading is a way of distributing our work. We can delegate a huge load of work in to several threads (i.e several processes). A thread will run as a separate operating system process which can run concurrently with other threads.

There are three ways of creating threads in java

1. Extend the thread class

Thread class has a run method and need override method with the code to run in a its own thread.
To execute code in a separate thread,
  • Instantiate an object of class which is extended by Thread class (Runner)
  • Invoke the start() method of Thread class.
    • start() method will go to Thread class and look for the run method and invoke that in a separate thread.

2. Implement Runnable interface

  • Implement Runnable interface in a class
  • Pass object of that class to a constructor of Thread class.
  • Invoke the start() method 




3. Instantiate a Thread object with a Runnable instance

Java Socket Programming - Simple Server And Client

In Java socket programming(Network programming) we basically study about how to implement programs that communicate over a network. When we use java to implement those kind of applications java.net package will be used because it contains collection of classes and interfaces that provide low level communication details. It provides support for TCP and UDP which are two major network protocols.


Process between client and a server


  • First client program creates a socket and try to connect to a socket in a server. That server socket should be already created and ready for client requests. 
  • After a connection is made server and client communicate by writing and reading from the socket.

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...