Saturday, June 16

Life Cycle of A Thread:

Threading concept is very important in Java Programing language. A thread is a sequential path of code execution within a program. And each thread has its own local variables, program counter and lifetime.

When you are programming with threads, understanding the life cycle of thread is very valuable. While a thread is alive, it is in one of several states. By invoking start() method, it doesn?t mean that the thread has access to CPU and start executing straight away. Several factors determine how it will proceed.



Different states of a thread are :

New state ? After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.
 
Runnable (Ready-to-run) state ? A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on the processor. 
 
Running state ? A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.
 
Dead state ? A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again.
Blocked - A thread can enter in this state because of waiting the resources that are hold by another thread.



Different states implementing Multiple-Threads are:

As we have seen different states that may be occur with the single thread. A running thread can enter to any non-runnable state, depending on the circumstances. A thread cannot enters directly to the running state from non-runnable state, firstly it goes to runnable state. Now lets understand the some non-runnable states which may be occur handling the multithreads.
Sleeping ? On this state, the thread is still alive but it is not runnable, it might be return to runnable state later, if a particular event occurs. On this state a thread sleeps for a specified amount of time. You can use the method sleep( ) to stop the running state of a thread.
  
   static void sleep(long millisecond) throws InterruptedException
Waiting for Notification ? A thread waits for notification from another thread. The thread sends back to runnable state after sending notification from another thread.
 
   final void wait(long timeout) throws InterruptedException
   final void wait(long timeout, int nanos) throws InterruptedException
   final void wait() throws InterruptedException
 
Blocked on I/O ? The thread waits for completion of blocking operation. A thread can enter on this state because of waiting I/O resource. In that case the thread sends back to runnable state after availability of resources.
 
Blocked for joint completion ? The thread can come on this state because of waiting the completion of another thread.
  
Blocked for lock acquisition ? The thread can come on this state because of waiting to acquire the lock of an object.
 Methods that can be applied apply on a Thread:

Some Important Methods defined in java.lang.Thread are shown in the table:

 Method  Return Type  Description
 currentThread( )Thread  Returns an object reference to the thread in which it is invoked.
 getName( )  String  Retrieve the name of the thread object or instance.
 start( )  void  Start the thread by calling its run method.
 run( )          void  This method is the entry point to execute thread, like the main method for applications.
 sleep( )  void  Suspends a thread for a specified amount of time (in milliseconds).
  isAlive( )  boolean  This method is used to determine the thread is running or not.
 activeCount( )  int  This method returns the number of active threads in a particular thread group and all its subgroups.
 interrupt( )  void  The method interrupt the threads on which it is invoked.
 yield( )  void  By invoking this method the current thread pause its execution temporarily and allow other threads to execute.
 join( )  void  This method and  join(long millisec) Throws InterruptedException.  These two methods are invoked on a thread. These are not returned until either the thread has completed or it is timed out respectively.

How to rotate the log files(Zero Out) without having to restart the services:

Syntax: date > logfile

This will 'empty' the file and insert as the first line the output from the date command. If you want a completely empty file, don't enter date

just > logfile This works great on apache and other web server logs, without ever stopping the service

Different ways to take the Threaddump:

In Solaris/Linux …etc Unix Based Boxes you need to findout the Servers Process ID…by running the following command from the Same box where the Server is running: 

Option 1: ps -ef | grep java qProcessbox 20650 1 0 Mar 26 ? 104:51 /opt/app/qProcessbox/java/jdk1.5.0_22/bin/java 

Option 2: Now use kill -3 20650 Syntax: kill -3 The OutPut of the Thread Dump will be generated in the Server STDOUT. 

Option 3. Simplest option to take Thread Dump is : Login to AdminConsole—>Server —> Monitoring —> Threads 

Option 4. Collecting Thread Dumps Using Jstack utility Open a Command or Shell prompt and then Move inside the \bin directory and then run the JStack.exe by passing the Process id of your Server Instance… Syntax: Jstack -l  

Option 5. Taking the JVM Details (Heap size and Jdk Details including OS details) including Thread Dump:
java weblogic.Admin -url t3://ServerHostName:7001 -username weblogic -password weblogic GET -pretty -type JVMRuntime
Weblogic Admin command to get the ServerRuntime status
java weblogic.Admin -url t3://ServerHostName:7001 -username weblogic -password weblogic GET -pretty -type ServerRuntime

File permission Related:

To change file permissions for all the files in a directory 
$ chmod 755 * 

To copy the file having spaces in its file name 
EX: 1)Filename is Supplier Key Contacts.xls 
Here there are spaces between file name. 
cp "Supplier Key Contacts.xls" "Supplier Key Contacts.xls_bkp"  

To find and replace a string in one or more files in a directory 
perl -pi -e 's/old_string/new_string/g' file_pattern 
EX:- perl -pi -e 's/18791/7001/g' *.sh 

High Cpu Usage on Solaris

Command1: prstat -s cpu -n 5 
Will list the top 5 process consuming the CPU on the server 
Try to identify what type of process it is, whether is OS or application related process. Based on the process proceed with the corrective measures. 

Command2: prstat -t 
Will list the cpu utilization wrt the users 

Alternatievely you can also use: 
Command3: prstat -s cpu -a -n 8 
The output will display the Top 8 consumers of CPU


 We can follow below steps as assuming one of the wls managed server/jvm causing high cpu or slow performance on linux,

1. Find out which JVM is using more cpu or slow performance jvm by using top command

2. Run the below command to see which threads in the JVM's are using high cpu/slow performance,

top -Hp 

Note the thread id's which are causing more cpu usage or slow performance

3. Take a multiple threads dumps with 5 to 10 seconds interval using kill -3 

so we can see whether the same threads are using more cpu/slow performance at all the time,because some of threads takes more cpu for few seconds only,so taking multiple threads dumps will help us to get the threads which are using the cpu constantly.

4. In the threads dumps, you can search for the thread id's (if you are using the sun jdk then thread id is on hexa and that needs to be converted to decimal and look for the decimal on the thread dump but if u are using jrockit then u can directly search for the thread id in the dump

5. Once you have stack strace of the threads which are causing the high cpu/slow performance , u can see who is causing the issue ,may be a bad app code, third party library, or may be an slow network,or slow data base respone or Garbage collector thread. etc.. based on the issue we need to work on it to get it fixed.



How to solve the High CPU Utilization issue?
  • Rajasekhar Reddy Naredla before restarting the server we need verify the below tings. 1. top command –> to know the high CPU consuming process.
    2. ps -ef|grep –> to know details of the process consuming high CPU.
    3. ls -lrt /proc//fd

    or ls -lrt /proc//fd |grep log —-> to know the files accessed by the process. To know the log files and other related files.
  • Maheshkumar Subbaiyan We can follow below steps as assuming one of the wls managed server/jvm causing high cpu or slow performance on linux,

    1. Find out which JVM is using more cpu or slow performance jvm by using top command


    2. Run the below command to see which threads in the JVM's are using high cpu/slow performance,

    top -Hp

    Note the thread id's which are causing more cpu usage or slow performance

    3. Take a multiple threads dumps with 5 to 10 seconds interval using kill -3

    so we can see whether the same threads are using more cpu/slow performance at all the time,because some of threads takes more cpu for few seconds only,so taking multiple threads dumps will help us to get the threads which are using the cpu constantly.

    4. In the threads dumps, you can search for the thread id's (if you are using the sun jdk then thread id is on hexa and that needs to be converted to decimal and look for the decimal on the thread dump but if u are using jrockit then u can directly search for the thread id in the dump

    5. Once you have stack strace of the threads which are causing the high cpu/slow performance , u can see who is causing the issue ,may be a bad app code, third party library, or may be an slow network,or slow data base respone or Garbage collector thread. etc.. based on the issue we need to work on it to get it fixed.

Solaris Virtual Memory Statistics:

Solaris Virtual Memory combines physical Memory with avaliable swap
Server-wl@root# df -kh swap
Filesystem size used avail capacity Mounted on swap
144G       56K 144G 1% /var/run

Server-wl@root# top | grep -i swap
CPU states: 99.9% idle, 0.0% user, 0.1% kernel, 0.0% iowait, 0.0% swap Memory: 40G phys mem, 105G free mem, 59G total swap, 59G free swap


To check memory on solaris
bash-3.00$ prtconf -vp | grep -i mem 

To check No of processors running on solaris
bash-3.00$ psrinfo -pv 

Find Command

1)Find command to check biggest files in a directory
 find . -type f -exec ls -s {} \; | sort -n -r | head -5

2)Find Command to get the recent modified data
--------------------------------------------------------
-mtime +60 ----find 60 days ago modified data
-mtime -60-----find 60 days below modified data.
-mtime 60 ------find modified data exactly 60th day.
--------------------------------------------------------
Ex:
To find the last modified data under root folder

find / -mtime -1-----find last 1day modified data
find / -iname "*.txt" -mtime -60----- find tha last 60 days modified data.

3)How to find and remove old files on Unix server

To find old files before 90 days
find {directory to search in} -atime +90

TO remove files before 90 days
rm 'find {directory to search in} -atime +90'

Eg:-find < APP-DIR > /pdm/ -atime +90
It displays files used before 90 days in above directory

rm 'find
< APP-DIR > /pdm/ -atime +90'
It removes files before 90 days in above directory

Command Prompt Shortcuts:

1. Clear the screen - instead of using command 'clear' CTRL
$ CTRL  + l 
2. Move Cursor to Start-Of-Line
$ CTRL  + a 
3. Move Cursor to End-Of-Line
$ CTRL  + e 
4. Quit from current shell
$  CTRL  + d