<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4097251789842337972</id><updated>2024-09-11T12:02:25.270-07:00</updated><category term="TechNology"/><category term="java"/><category term="Social Service"/><category term="linux"/><category term="FTP"/><category term="oracle"/><title type='text'>Hunter</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>20</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-903526140966032434</id><published>2013-12-08T21:26:00.003-08:00</published><updated>2013-12-08T21:26:29.689-08:00</updated><title type='text'>Java useful command</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;-To run java program in debug mode&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; pass&amp;nbsp;&lt;i&gt;-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=&amp;lt;port_NO&amp;gt; &lt;/i&gt;as JVM arguments&amp;nbsp;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/903526140966032434/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2013/12/java-useful-command.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/903526140966032434'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/903526140966032434'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2013/12/java-useful-command.html' title='Java useful command'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-7020804886925990345</id><published>2013-12-05T23:26:00.006-08:00</published><updated>2013-12-05T23:26:50.679-08:00</updated><title type='text'>Windows useful commands and default directory structure common software.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;To start service in window OS from command line.&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;net start/stop/pause/continue &amp;lt;&amp;lt;service_name&amp;gt;&amp;gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;To start Clear case service in Windows.&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;net start&amp;nbsp;&lt;b&gt;albd.&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/7020804886925990345/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2013/12/windows-useful-commands-and-default.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7020804886925990345'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7020804886925990345'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2013/12/windows-useful-commands-and-default.html' title='Windows useful commands and default directory structure common software.'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-7869893507041323029</id><published>2013-11-26T23:28:00.001-08:00</published><updated>2013-11-26T23:34:38.697-08:00</updated><title type='text'>Most used Commands and Standered file directory path in Linux</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #cc0000;&quot;&gt;To modify host name and IP address in Linux&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
/etc/hosts file&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
create bond( /etc/sysconfig/network-scripts/ifcfg-bond0)&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
DEVICE=bond0&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
ONBOOT=yes&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
USERCTL=no&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
BOOTPROTO=none&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
BONDING_OPTS=&quot;primary=eth0&quot;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
IPADDR=xx.xx.xxx.xx&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
NETMASK=xxx.xxx.xxx.x&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
GATEWAY=xx.xx.xxx.x&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
BROADCAST=xx.xx.xxx.xxx&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
configure net work(/etc/sysconfig/network-scripts/ifcfg-eth0&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
DEVICE=eth0&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
ONBOOT=yes&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
USERCTL=no&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
BOOTPROTO=none&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
SLAVE=yes&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
MASTER=bond0&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
change the domine name&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
/etc/sysconfig/network(hostname)&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
/etc/hosts&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
service network restart&lt;/div&gt;
&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #b45f06;&quot;&gt;To delete user in&amp;nbsp;Linux&amp;nbsp;&lt;/span&gt;&lt;/h2&gt;
&lt;span style=&quot;background-color: #eeeeee; color: #111111; font-family: Consolas, &#39;Andale Mono&#39;, Monaco, Courier, &#39;Courier New&#39;, Verdana, sans-serif; font-size: 11.818181991577148px; line-height: 17.982954025268555px;&quot;&gt;userdel -r userName(-r used to delete along with home directory)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #eeeeee; color: #111111; font-family: Consolas, &#39;Andale Mono&#39;, Monaco, Courier, &#39;Courier New&#39;, Verdana, sans-serif; font-size: 11.818181991577148px; line-height: 17.982954025268555px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/7869893507041323029/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2013/11/standered-file-directory-path-in-linux.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7869893507041323029'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7869893507041323029'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2013/11/standered-file-directory-path-in-linux.html' title='Most used Commands and Standered file directory path in Linux'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-7514422028432050295</id><published>2013-09-16T23:30:00.002-07:00</published><updated>2013-09-16T23:30:28.340-07:00</updated><title type='text'>12 Little Known Laws of Karma (That Will Change Your Life) </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;What is Karma? Karma is the Sanskrit word for action. It is equivalent to Newton&#39;s law of ‘every action must have a reaction’. When we think, speak or act we initiate a force that will react accordingly. This returning force maybe modified, changed or suspended, but most people will not be able eradicate it.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;img alt=&quot;12 Little Known Laws of Karma (That Will Change Your Life)&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimfus4IIWyplspxC8xeuYAzObjjBcbCL5gIk-F0NxxId6c0kUWjL06TEcS-xF-bxVLazmsU-6wr3CSBOirAKB0VD6LSLlGK1wviMSkIhGgO-pQNA1EEcAKxKxOMt_X_Ub5_HWkqhpWWRU/s1600/Karma+-+going+round.jpg&quot; /&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;This law of cause and effect is not punishment, but is wholly for the sake of education or learning. A person may not escape the consequences of his actions, but he will suffer only if he himself has made the conditions ripe for his suffering. Ignorance of the law is no excuse whether the laws are man-made or universal. To stop being afraid and to start being empowered in the worlds of karma and reincarnation, here is what you need to know about karmic laws.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;THE GREAT LAW&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- &quot;As you sow, so shall you reap&quot;. This is also known as the &quot;Law of Cause and Effect&quot;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- Whatever we put out in the Universe is what comes back to us.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- If what we want is Happiness, Peace, Love, Friendship... Then we should BE Happy, Peaceful, Loving and a True Friend.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;THE LAW OF CREATION&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- Life doesn&#39;t just HAPPEN, it requires our participation.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- We are one with the Universe, both inside and out.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- Whatever surrounds us gives us clues to our inner state.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- BE yourself, and surround yourself with what you want to have present in your Life.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;THE LAW OF HUMILITY&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- What you refuse to accept, will continue for you.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- If what we see is an enemy, or someone with a character trait that we find to be negative, then we ourselves are not focused on a higher level of existence.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;THE LAW OF GROWTH&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- &quot;Wherever you go, there you are&quot;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- For us to GROW in Spirit, it is we who must change - and not the people, places or things around us. - The only given we have in our lives is OURSELVES and that is the only factor we have control over. - When we change who and what we are within our heart our life follows suit and changes too.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;THE LAW OF RESPONSIBILITY&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- Whenever there is something wrong in my life, there is something wrong in me.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- We mirror what surrounds us - and what surrounds us mirrors us; this is a Universal Truth.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- We must take responsibility what is in our life.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;THE LAW OF CONNECTION&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- Even if something we do seems inconsequential, it is very important that it gets done as everything in the Universe is connected.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- Each step leads to the next step, and so forth and so on.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- Someone must do the initial work to get a job done.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- Neither the first step nor the last are of greater significance,&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- As they were both needed to accomplish the task.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- Past-Present-Future they are all connected...&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;THE LAW OF FOCUS&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- You can not think of two things at the same time.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- When our focus is on Spiritual Values, it is impossible for us to have lower thoughts such as greed or anger.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;THE LAW OF GIVING AND HOSPITALITY&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- If you believe something to be true,then sometime in your life you will be called upon to demonstrate that particular truth.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- Here is where we put what we CLAIM that we have learned, into actual PRACTICE.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;THE LAW OF HERE AND NOW&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- Looking backward to examine what was, prevents us from being totally in the HERE AND NOW.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- Old thoughts, old patterns of behavior, old dreams...&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- Prevent us from having new ones.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;THE LAW OF CHANGE&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- History repeats itself until we learn the lessons that we need to change our path.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;THE LAW OF PATIENCE AND REWARD&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- All Rewards require initial toil.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- Rewards of lasting value require patient and persistent toil. -&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;True joy follows doing what we&#39;re suppose to be doing, and waiting for the reward to come in on its own time.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;THE LAW OF SIGNIFICANCE AND INSPIRATION&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- You get back from something whatever YOU have put into it.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- The true value of something is a direct result of the energy and intent that is put into it.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- Every personal contribution is also a contribution to the Whole.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;- Lack luster contributions have no impact on the Whole, nor do they work to diminish it.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;&amp;nbsp;- Loving contributions bring life to, and inspire, the Whole.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/ol&gt;
&lt;span style=&quot;background-color: #e6e6e6; color: #444444; font-family: &#39;Open Sans&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 23px; text-align: justify;&quot;&gt;Sources: Bliss Returned and Raven Emrys&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/7514422028432050295/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2013/09/12-little-known-laws-of-karma-that-will.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7514422028432050295'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7514422028432050295'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2013/09/12-little-known-laws-of-karma-that-will.html' title='12 Little Known Laws of Karma (That Will Change Your Life) '/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimfus4IIWyplspxC8xeuYAzObjjBcbCL5gIk-F0NxxId6c0kUWjL06TEcS-xF-bxVLazmsU-6wr3CSBOirAKB0VD6LSLlGK1wviMSkIhGgO-pQNA1EEcAKxKxOMt_X_Ub5_HWkqhpWWRU/s72-c/Karma+-+going+round.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-8474191629224588307</id><published>2013-02-25T00:32:00.000-08:00</published><updated>2013-02-25T00:32:54.997-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="TechNology"/><title type='text'>Java technology, IBM style: Garbage collection policies, Part 1</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;em style=&quot;background-color: white; color: #666666; font-family: verdana, sans-serif; font-size: 1.1em; font-style: normal; font-weight: bold;&quot;&gt;Differing policies provide flexibility&lt;/em&gt;&lt;br /&gt;
&lt;strong style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/strong&gt;
&lt;strong style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 12px;&quot;&gt;Summary:&lt;/strong&gt;&lt;span style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 12px;&quot;&gt;&amp;nbsp; One of the great benefits of the Java™ platform is that it takes care of much of the work of garbage collection for you, but there are occasions when you still want to tweak the way garbage collection takes place. With the latest Java technology implementation from IBM®, you can choose among several garbage collection policies to help you get the most out of your application. In this second article in the series&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 12px;&quot;&gt;Java technology, IBM style&lt;/i&gt;&lt;span style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 12px;&quot;&gt;, Java developer Mattias Persson explores the available options and details the situations in which each might be appropriate.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
Garbage collection (GC) in the IBM Developer Kit for the Java 5.0 Platform (IBM SDK) can be configured using four different policies. This article, the first of two parts on GC, introduces the different garbage collection policies and discusses their general characteristics. You should have a basic understanding of garbage collection in the Java platform before you begin. Part 2 presents a quantitative approach to choosing a policy, along with with some examples.&lt;/div&gt;
&lt;div class=&quot;ibm-container ibm-alt-header dw-container-sidebar&quot; style=&quot;background-color: white; float: right; margin: 0px 0px 1.2em 10px; width: 320px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;sidebar1&quot;&gt;&lt;/a&gt;&lt;h2 style=&quot;background-color: #f7f8fc; background-image: none; background-position: 0px -545px; background-repeat: repeat no-repeat; border: 1px solid rgb(204, 204, 204); font-family: arial, sans-serif; font-size: 0.75em; height: 15px; margin: 0px; padding: 3px 5px;&quot;&gt;
About the series&lt;/h2&gt;
&lt;div class=&quot;ibm-container-body&quot; style=&quot;background-image: url(http://1.www.s81c.com/i/v16/t/container-gradient.gif); background-position: 0px 100%; background-repeat: repeat no-repeat; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; font-family: arial, sans-serif; font-size: 0.76em; padding: 5px;&quot;&gt;
&lt;div style=&quot;font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
The&amp;nbsp;&lt;i&gt;&lt;a href=&quot;http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=java+technology+ibm+style:&quot; style=&quot;color: #996699;&quot;&gt;Java technology, IBM style&lt;/a&gt;&lt;/i&gt;&amp;nbsp;series takes a look at the latest releases of the IBM implementations of the Java platform. You&#39;ll learn how IBM has implemented some of the advances built into version 5.0 of the Java platform, and you&#39;ll find out how to use some of the value-added features built into new releases from IBM.&lt;/div&gt;
&lt;div style=&quot;font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
Contact the authors individually with comments or questions about their articles. To comment on the series as a whole, you may contact series lead&amp;nbsp;&lt;a href=&quot;mailto:baileyc@uk.ibm.com&quot; style=&quot;color: #996699;&quot;&gt;Chris Bailey&lt;/a&gt;. For more on the concepts discussed here and links where you can download the latest IBM releases, see&amp;nbsp;&lt;a href=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/#resources&quot; style=&quot;color: #996699;&quot;&gt;Resources&lt;/a&gt;.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;1&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;Why different GC policies?&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
The availability of differing GC policies gives you increased capabilities. There are many different algorithms available for GC, each of which has advantages and drawbacks, depending on the type of workload. (If you are unfamiliar with the general topic of GC algorithms, see&amp;nbsp;&lt;a href=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/#resources&quot; style=&quot;color: #996699;&quot;&gt;Resources&lt;/a&gt;&amp;nbsp;for further reading.) In the IBM SDK 5.0, you can configure the garbage collector with one of four&amp;nbsp;&lt;i&gt;policies&lt;/i&gt;, each of which uses its own algorithm. The default policy is sufficient for most applications. If you do not have any particular performance requirements for your application, then the content of this article (and the next) may not be interesting to you; you can run the IBM SDK 5.0 without changing the GC policy. However, if your application requires optimal performance or if you are generally concerned with the length of GC pauses, then read on. You will see that the latest release from IBM has even more choice than its predecessors.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
So why doesn&#39;t the IBM implementation of the Java runtime make the choice automatically for you? This isn&#39;t always possible. It&#39;s difficult for the runtime to understand your needs. In some situations, you may want to run the application with high throughput; in others, you might prefer to reduce the pause times.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
Table 1 lists the available policies and explains when you should use each one. The following sections describe the characteristics of each policy in more detail.&lt;/div&gt;
&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;table1&quot; style=&quot;background-color: white;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;Table 1. GC policies in the IBM SDK 5.0&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;ibm-data-table&quot; style=&quot;background-color: white; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-width: 0px 1px 1px; color: black; width: 100%px;&quot; summary=&quot;Table 1. GC policies in the IBM SDK 5.0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th style=&quot;background-color: #f7f8fc; background-image: url(http://1.www.s81c.com/i/v16/t/text-tabs-bg.gif); background-repeat: repeat no-repeat; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Policy&lt;/th&gt;&lt;th style=&quot;background-color: #f7f8fc; background-image: url(http://1.www.s81c.com/i/v16/t/text-tabs-bg.gif); background-repeat: repeat no-repeat; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Option&lt;/th&gt;&lt;th style=&quot;background-color: #f7f8fc; background-image: url(http://1.www.s81c.com/i/v16/t/text-tabs-bg.gif); background-repeat: repeat no-repeat; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tb-row&quot; style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Optimize for throughput&lt;/td&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: small !important;&quot;&gt;-Xgcpolicy:optthruput&lt;/code&gt;(optional)&lt;/td&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;The default policy. It is typically used for applications where raw throughput is more important than short GC pauses. The application is stopped each time that garbage is collected.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tb-row&quot; style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Optimize for pause time&lt;/td&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: small !important;&quot;&gt;-Xgcpolicy:optavgpause&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Trades high throughput for shorter GC pauses by performing some of the garbage collection concurrently. The application is paused for shorter periods.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tb-row&quot; style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Generational concurrent&lt;/td&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: small !important;&quot;&gt;-Xgcpolicy:gencon&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Handles short-lived objects differently than objects that are long-lived. Applications that have many short-lived objects can see shorter pause times with this policy while still producing good throughput.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tb-row&quot; style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Subpooling&lt;/td&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: small !important;&quot;&gt;-Xgcpolicy:subpool&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Uses an algorithm similar to the default policy&#39;s but employs an allocation strategy that is more suitable for multiprocessor machines. We recommend this policy for SMP machines with 16 or more processors. This policy is only available on IBM pSeries® and zSeries® platforms. Applications that need to scale on large machines can benefit from this policy.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;div class=&quot;ibm-container ibm-alt-header dw-container-sidebar&quot; style=&quot;background-color: white; float: right; margin: 0px 0px 1.2em 10px; width: 320px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;sidebar2&quot;&gt;&lt;/a&gt;&lt;h2 style=&quot;background-color: #f7f8fc; background-image: none; background-position: 0px -545px; background-repeat: repeat no-repeat; border: 1px solid rgb(204, 204, 204); font-family: arial, sans-serif; font-size: 0.75em; height: 15px; margin: 0px; padding: 3px 5px;&quot;&gt;
Some terms defined&lt;/h2&gt;
&lt;div class=&quot;ibm-container-body&quot; style=&quot;background-image: url(http://1.www.s81c.com/i/v16/t/container-gradient.gif); background-position: 0px 100%; background-repeat: repeat no-repeat; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; font-family: arial, sans-serif; font-size: 0.76em; padding: 5px;&quot;&gt;
&lt;div style=&quot;font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;b style=&quot;font-size: 1em;&quot;&gt;Throughput&lt;/b&gt;&amp;nbsp;is the amount of data processed by an application. Throughput must be measured with an application-specific metric.&lt;/div&gt;
&lt;div style=&quot;font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;b style=&quot;font-size: 1em;&quot;&gt;Pause time&lt;/b&gt;&amp;nbsp;is the duration of time in which the garbage collector has paused all application threads to collect the heap.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
In this article, I use the abbreviations from the command-line options detailed in Table 1 to denote these policies:&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;optthruput&lt;/code&gt;&amp;nbsp;for Optimize for throughput,&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;optavgpause&lt;/code&gt;&amp;nbsp;for Optimize for pause time,&lt;code style=&quot;font-size: small !important;&quot;&gt;gencon&lt;/code&gt;&amp;nbsp;for Generational concurrent, and&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;subpool&lt;/code&gt;&amp;nbsp;for Subpooling.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;2&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;When should I consider a non-default GC policy?&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;i&gt;I advise that you always start with the default GC policy.&lt;/i&gt;&amp;nbsp;Before you move away from the default, you need to understand the circumstances under which you should explore an alternative policy. Table 2 presents a set of reasons that could apply:&lt;/div&gt;
&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;table2&quot; style=&quot;background-color: white;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;Table 2. Reasons for switching to an alternative GC policy&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;ibm-data-table&quot; style=&quot;background-color: white; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-width: 0px 1px 1px; color: black; width: 100%px;&quot; summary=&quot;Table 2. Reasons for switching to an alternative GC policy&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th style=&quot;background-color: #f7f8fc; background-image: url(http://1.www.s81c.com/i/v16/t/text-tabs-bg.gif); background-repeat: repeat no-repeat; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Switch to&lt;/th&gt;&lt;th style=&quot;background-color: #f7f8fc; background-image: url(http://1.www.s81c.com/i/v16/t/text-tabs-bg.gif); background-repeat: repeat no-repeat; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;Reasons&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: small !important;&quot;&gt;optavgpause&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;&lt;ul style=&quot;font-size: 1em; margin-bottom: 0px; margin-top: 0px; padding-bottom: 5px; padding-right: 5px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;My application cannot tolerate the length of the GC pauses. A degradation in performance is acceptable as long as the GC pause time is reduced.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;I am running on a 64-bit platform and use a very large heap -- more than 3 or 4GB.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;My application is a GUI application and I&#39;m concerned about the user response times.&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: small !important;&quot;&gt;gencon&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;&lt;ul style=&quot;font-size: 1em; margin-bottom: 0px; margin-top: 0px; padding-bottom: 5px; padding-right: 5px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;My application allocates many short-lived objects.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;The heap space is fragmented.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;My application is transaction-based (that is, objects in the transaction don&#39;t survive beyond the transaction commit).&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: small !important;&quot;&gt;subpool&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.7em; padding: 8px 5px; vertical-align: top;&quot;&gt;&lt;ul style=&quot;font-size: 1em; margin-bottom: 0px; margin-top: 0px; padding-bottom: 5px; padding-right: 5px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;I have scalability problems on large multiprocessor machine.&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
Let me stress that the reasons mentioned in Table 2 are not&amp;nbsp;&lt;i&gt;sufficient&lt;/i&gt;&amp;nbsp;to conclude that the alternative policies will perform better; they are simply hints. In all cases, you should run the application and measure throughput and/or response times in combination with GC pause times. The next part of this series shows examples of this sort of testing.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
The remaining sections of this article describe in more detail the differences between the GC policies.&lt;/div&gt;
&lt;div class=&quot;ibm-alternate-rule&quot; style=&quot;background-color: white; background-image: url(http://1.www.s81c.com/i/solid.gif); background-repeat: repeat no-repeat; clear: both; height: 1px;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;ibm-ind-link ibm-back-to-top&quot; style=&quot;background-color: white; clear: both; font-family: arial, sans-serif; font-size: 0.76em; height: 15px; padding: 5px; text-align: right;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;3&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;optthruput&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;ibm-container ibm-alt-header dw-container-sidebar&quot; style=&quot;background-color: white; float: right; margin: 0px 0px 1.2em 10px; width: 320px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;sidebar3&quot;&gt;&lt;/a&gt;&lt;h2 style=&quot;background-color: #f7f8fc; background-image: none; background-position: 0px -545px; background-repeat: repeat no-repeat; border: 1px solid rgb(204, 204, 204); font-family: arial, sans-serif; font-size: 0.75em; height: 15px; margin: 0px; padding: 3px 5px;&quot;&gt;
Signs of heap fragmentation&lt;/h2&gt;
&lt;div class=&quot;ibm-container-body&quot; style=&quot;background-image: url(http://1.www.s81c.com/i/v16/t/container-gradient.gif); background-position: 0px 100%; background-repeat: repeat no-repeat; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; font-family: arial, sans-serif; font-size: 0.76em; padding: 5px;&quot;&gt;
&lt;div style=&quot;font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
The most common sign of heap fragmentation is the presence of premature allocation failures. This is seen in verbose garbage collection (&lt;code style=&quot;font-size: small !important;&quot;&gt;-Xverbose:gc&lt;/code&gt;) as GC being triggered even though there is free space on the heap. Another sign is the presence of small thread allocation caches (see the subsection &quot;&lt;a href=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/#3.1&quot; style=&quot;color: #996699;&quot;&gt;Heap lock and thread allocation caches&lt;/a&gt;&quot;). You can use&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;-Xtgc:freelist&lt;/code&gt;&amp;nbsp;to determine the average size. Both the options are explained in the IBM SDK 5 Diagnostics Guide available in&amp;nbsp;&lt;a href=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/#resources&quot; style=&quot;color: #996699;&quot;&gt;Resources&lt;/a&gt;.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;code style=&quot;font-size: small !important;&quot;&gt;optthruput&lt;/code&gt;&amp;nbsp;is the default policy. It is a tracing collector, referred to as a&amp;nbsp;&lt;i&gt;mark-sweep-compact&lt;/i&gt;&amp;nbsp;collector. The mark and the sweep phases always run during GC, but compaction only happens under certain circumstances. The mark phase finds and marks all live objects. The sweep phase removes all unmarked objects. A third and optional step is&amp;nbsp;&lt;i&gt;compaction.&lt;/i&gt;&amp;nbsp;There are various circumstances under which compaction might occur; the most common one is the system&#39;s inability to reclaim enough free space.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
Fragmentation occurs when objects are allocated and freed so often that only small chunks of free space are left in the heap. There may be a large amount of free space overall in the heap, but the contiguous regions are small, causing allocation failures.&lt;i&gt;Compaction&lt;/i&gt;&amp;nbsp;moves all the objects down to the beginning of the heap and aligns them so that no space exists between them. This removes fragmentation from the heap but is an expensive task, so it is only performed if necessary.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
Figure 1 shows an outline of the heap layout after the different phases: mark, sweep, and compaction. The dark regions represent objects and the bright regions are free space.&lt;/div&gt;
&lt;div class=&quot;ibm-container ibm-alt-header dw-container-sidebar&quot; style=&quot;background-color: white; float: right; margin: 0px 0px 1.2em 10px; width: 320px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;sidebar4&quot;&gt;&lt;/a&gt;&lt;h2 style=&quot;background-color: #f7f8fc; background-image: none; background-position: 0px -545px; background-repeat: repeat no-repeat; border: 1px solid rgb(204, 204, 204); font-family: arial, sans-serif; font-size: 0.75em; height: 15px; margin: 0px; padding: 3px 5px;&quot;&gt;
Mark and sweep&lt;/h2&gt;
&lt;div class=&quot;ibm-container-body&quot; style=&quot;background-image: url(http://1.www.s81c.com/i/v16/t/container-gradient.gif); background-position: 0px 100%; background-repeat: repeat no-repeat; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; font-family: arial, sans-serif; font-size: 0.76em; padding: 5px;&quot;&gt;
&lt;div style=&quot;font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
The&amp;nbsp;&lt;i&gt;mark&lt;/i&gt;&amp;nbsp;phase traverses all objects that can be referenced from thread stacks, statics, interned strings, and JNI references. As part of this process, we create a mark bit vector that defines the beginning of all live objects.&lt;/div&gt;
&lt;div style=&quot;font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
The&amp;nbsp;&lt;i&gt;sweep&lt;/i&gt;&amp;nbsp;phase uses the mark bit vector generated by the mark phase to identify the chunks of heap storage that can be reclaimed for future allocations; these chunks are added to the free list.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;figure1&quot; style=&quot;background-color: white;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;Figure 1. Heap layout before and after garbage collection&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;img alt=&quot;Heap layout before and after garbage collection&quot; height=&quot;164&quot; src=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/fig1.gif&quot; style=&quot;background-color: white; border: 0px; padding: 0.3em 5px 0.7em;&quot; width=&quot;478&quot; /&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
The details of how the different GC phases work is beyond the scope of this article; my focus is on ensuring you understand the runtime characteristics. I encourage you to read the Diagnostics Guide (see&amp;nbsp;&lt;a href=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/#resources&quot; style=&quot;color: #996699;&quot;&gt;Resources&lt;/a&gt;) for more information.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
Figure 2 illustrates how the execution time is distributed between the application thread (or&amp;nbsp;&lt;i&gt;mutator&lt;/i&gt;) and GC threads. The horizontal axis is elapsed time and the vertical axis contains the threads, where&amp;nbsp;&lt;i&gt;n&lt;/i&gt;&amp;nbsp;represents the number of processors on the machine. For this illustration, assume that the application is using one thread per processor. GC is illustrated by the blue boxes, which show stopped mutators and GC threads running. These collections consume 100 percent of the CPU&#39;s resources, and the mutator threads are idle. The figure overgeneralizes a bit so that we can compare this policy to other policies in this article. In actuality, GC duration and frequency vary depending on application and workload.&lt;/div&gt;
&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;figure2&quot; style=&quot;background-color: white;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;Figure 2. Distribution of CPU time between mutators and GC threads in the optthruput policy&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;img alt=&quot;Distribution of CPU time between mutators and GC threads in the optthruput policy&quot; height=&quot;215&quot; src=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/fig2.gif&quot; style=&quot;background-color: white; border: 0px; padding: 0.3em 5px 0.7em;&quot; width=&quot;550&quot; /&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;div class=&quot;ibm-container ibm-alt-header dw-container-sidebar&quot; style=&quot;background-color: white; float: right; margin: 0px 0px 1.2em 10px; width: 320px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;sidebar5&quot;&gt;&lt;/a&gt;&lt;h2 style=&quot;background-color: #f7f8fc; background-image: none; background-position: 0px -545px; background-repeat: repeat no-repeat; border: 1px solid rgb(204, 204, 204); font-family: arial, sans-serif; font-size: 0.75em; height: 15px; margin: 0px; padding: 3px 5px;&quot;&gt;
Mutator vs. GC threads&lt;/h2&gt;
&lt;div class=&quot;ibm-container-body&quot; style=&quot;background-image: url(http://1.www.s81c.com/i/v16/t/container-gradient.gif); background-position: 0px 100%; background-repeat: repeat no-repeat; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; font-family: arial, sans-serif; font-size: 0.76em; padding: 5px;&quot;&gt;
&lt;div style=&quot;font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
A&amp;nbsp;&lt;i&gt;mutator&lt;/i&gt;&amp;nbsp;thread is the application program that allocates objects. Another word for mutator is&lt;i&gt;application.&lt;/i&gt;&amp;nbsp;A GC thread is part of the memory management and performs the garbage collection.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;3.1&quot;&gt;&lt;span class=&quot;smalltitle&quot; style=&quot;font-size: 1.2em; font-weight: bold;&quot;&gt;Heap lock and thread allocation caches&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
The&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;optthruput&lt;/code&gt;&amp;nbsp;policy uses a contiguous heap area that all threads in the application share. A thread needs exclusive access to the heap for reserving space for new objects. This lock, called the&lt;i&gt;heap lock,&lt;/i&gt;&amp;nbsp;ensures that only one thread at a time can allocate an object. On machines with multiple CPUs, this lock can cause scaling problems because multiple allocation requests can occur at the same time, but each needs to have exclusive access to the heap lock.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
To reduce this problem, each thread reserves a small chunk of memory called a&amp;nbsp;&lt;i&gt;thread allocation cache&lt;/i&gt;&amp;nbsp;(also referred to as&lt;i&gt;thread local heap,&lt;/i&gt;&amp;nbsp;or TLH). This piece of storage is exclusive to a thread, so while allocating from it, the heap lock is not used. When the allocation cache is full, the thread goes back to the heap and asks for a new one, using the heap lock.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
Fragmentation of the heap can prevent the thread from getting large TLHs, so a TLH fills quickly, causing the application thread to come back frequently to the heap for a new one. In this situation, the heap lock becomes a bottleneck; in such situations, the&lt;code style=&quot;font-size: small !important;&quot;&gt;gencon&lt;/code&gt;&amp;nbsp;or&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;subpool&lt;/code&gt;&amp;nbsp;policies can provide good alternatives.&lt;/div&gt;
&lt;div class=&quot;ibm-alternate-rule&quot; style=&quot;background-color: white; background-image: url(http://1.www.s81c.com/i/solid.gif); background-repeat: repeat no-repeat; clear: both; height: 1px;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;ibm-ind-link ibm-back-to-top&quot; style=&quot;background-color: white; clear: both; font-family: arial, sans-serif; font-size: 0.76em; height: 15px; padding: 5px; text-align: right;&quot;&gt;
&lt;a class=&quot;ibm-anchor-up-link&quot; href=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/#ibm-pcon&quot; style=&quot;background-image: url(http://1.www.s81c.com/i/v16/icons/u_bold.gif); background-position: 0px -1px; background-repeat: no-repeat no-repeat; color: #996699; display: inline; font-weight: bold; margin: 0px; padding: 0px 0px 0px 18px; text-decoration: initial;&quot;&gt;Back to top&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;4&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;optavgpause&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
For many applications, throughput is not as important as fast response time. Consider an application that processes work items that cannot take longer than 100 milliseconds to complete. With GC pauses in the 100 millisecond range, you get items that cannot be processed within this timeframe. A problem with garbage collection is that the pause time increases the maximum time it takes to process an item. Large heap sizes (available on 64-bit platforms) increase this effect because more objects are processed.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;code style=&quot;font-size: small !important;&quot;&gt;optavgpause&lt;/code&gt;&amp;nbsp;is an alternative GC policy designed to keep pauses to a minimum. It does not guarantee a particular pause time, but pauses are shorter than those produced by the default GC policy. The idea is to perform some garbage collection work concurrently while the application is running. This is done in two places:&lt;/div&gt;
&lt;ul style=&quot;background-color: white; font-size: 0.76em; margin-bottom: 0px; margin-top: 0px; padding-bottom: 5px; padding-right: 5px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;&lt;b style=&quot;font-size: 1em;&quot;&gt;Concurrent mark and sweep&lt;/b&gt;: Before the heap is filled up, each mutator helps out and mark sobjects (concurrent mark). There is still a stop-the-world GC, but the pause is significantly shorter. After GC, the mutator threads help out and sweep objects (concurrent sweep).&lt;br /&gt;&lt;/li&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;&lt;b style=&quot;font-size: 1em;&quot;&gt;Background GC thread&lt;/b&gt;: One (or more) low-priority background GC threads perform marking while the application is idle.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
There is a throughput performance penalty of 5 to 10 percent over the default GC policy, depending on the application.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
Figure 3 illustrates how the execution time can be divided between GC threads and mutator threads using&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;optavgpause&lt;/code&gt;. The background tracing thread is not shown because it should not affect application performance.&lt;/div&gt;
&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;figure3&quot; style=&quot;background-color: white;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;Figure 3. Distribution of CPU time between mutators and GC threads in the optavgpause policy&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;img alt=&quot;Distribution of CPU time between mutators and GC threads in the optavgpause policy&quot; height=&quot;241&quot; src=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/fig3.gif&quot; style=&quot;background-color: white; border: 0px; padding: 0.3em 5px 0.7em;&quot; width=&quot;542&quot; /&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
The gray areas in the figure imply that concurrent tracing is enabled and that each mutator thread has to give up some of its processing time. Each concurrent phase is followed by a full garbage collection that completes the marking and sweeping that did not happen during the concurrent phase. The pause that results from this should be much shorter than the normal GC seen in&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;optthruput&lt;/code&gt;, as indicated by a smaller box on the timescale in Figure 3. The gap between the end of the GC and the start of the concurrent phase vary, but during this phase, there is no significant performance impact.&lt;/div&gt;
&lt;div class=&quot;ibm-alternate-rule&quot; style=&quot;background-color: white; background-image: url(http://1.www.s81c.com/i/solid.gif); background-repeat: repeat no-repeat; clear: both; height: 1px;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;ibm-ind-link ibm-back-to-top&quot; style=&quot;background-color: white; clear: both; font-family: arial, sans-serif; font-size: 0.76em; height: 15px; padding: 5px; text-align: right;&quot;&gt;
&lt;a class=&quot;ibm-anchor-up-link&quot; href=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/#ibm-pcon&quot; style=&quot;background-image: url(http://1.www.s81c.com/i/v16/icons/u_bold.gif); background-position: 0px -1px; background-repeat: no-repeat no-repeat; color: #996699; display: inline; font-weight: bold; margin: 0px; padding: 0px 0px 0px 18px; text-decoration: initial;&quot;&gt;Back to top&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;5&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;gencon&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
A generational garbage collection strategy considers the lifetime of objects and places them in separate areas of the heap. In this way, it tries to overcome the drawbacks of a single heap in applications where most objects&amp;nbsp;&lt;i&gt;die young&lt;/i&gt;&amp;nbsp;-- that is, where they do not survive many garbage collections.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
With generational GC, objects that tend to survive for a long time are treated differently from short-lived objects. The heap is split into a&amp;nbsp;&lt;i&gt;nursery&lt;/i&gt;&amp;nbsp;and a&amp;nbsp;&lt;i&gt;tenured area,&lt;/i&gt;&amp;nbsp;as illustrated in Figure 4. Objects are created in the nursery and, if they live long enough, are promoted to the tenured area. Objects are promoted after having survived a certain number of garbage collections. The idea is that most objects are short-lived; by collecting the nursery frequently, these objects can be freed up without paying the cost of collecting the entire heap. The tenured area is garbage collected less often.&lt;/div&gt;
&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;figure4&quot; style=&quot;background-color: white;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;Figure 4. New and old area in gencon GC&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;img alt=&quot;New and old area in gencon garbage collection&quot; height=&quot;59&quot; src=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/fig4.gif&quot; style=&quot;background-color: white; border: 0px; padding: 0.3em 5px 0.7em;&quot; width=&quot;513&quot; /&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
As you can see in Figure 4, the nursery is in turn split into two spaces:&amp;nbsp;&lt;i&gt;allocate&lt;/i&gt;&amp;nbsp;and&amp;nbsp;&lt;i&gt;survivor&lt;/i&gt;. Objects are allocated into the allocate space and, when that fills up, live objects are copied into the survivor space or into the tenured space, depending on their age. The spaces in the nursery then switch use, with allocate becoming survivor and survivor becoming allocate. The space occupied by dead objects can simply be overwritten by new allocations. Nursery collection is called a&amp;nbsp;&lt;i&gt;scavenge;&lt;/i&gt;&amp;nbsp;Figure 5 illustrates what happens during this process:&lt;/div&gt;
&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;figure5&quot; style=&quot;background-color: white;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;Figure 5. Example of heap layout before and after GC&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;img alt=&quot;Example of heap layout before and after GC&quot; height=&quot;152&quot; src=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/fig5.gif&quot; style=&quot;background-color: white; border: 0px; padding: 0.3em 5px 0.7em;&quot; width=&quot;492&quot; /&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
When the allocate space is full, garbage collection is triggered. Live objects are then traced and copied into the survivor space. This process is really inexpensive if most of the objects are dead. Furthermore, objects that have reached a copy threshold count are promoted into the tenured space. The object is then said to be&amp;nbsp;&lt;i&gt;tenured.&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
As the name&amp;nbsp;&lt;i&gt;Generational concurrent&lt;/i&gt;&amp;nbsp;implies, the&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;gencon&lt;/code&gt;&amp;nbsp;policy has a concurrent aspect to it. The tenured space is concurrently marked with an approach similar to the one used in the&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;optavgpause&lt;/code&gt;&amp;nbsp;policy, except without concurrent sweep. All allocations pay a small throughput tax during the concurrent phase. With this approach, the pause time incurred from the tenure space collections is kept small.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
Figure 6 shows how the execution time maps out when running&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;gencon&lt;/code&gt;&amp;nbsp;GC:&lt;/div&gt;
&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;figure6&quot; style=&quot;background-color: white;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;Figure 6. Distribution of CPU time between mutators and GC threads in gencon&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;img alt=&quot;Distribution of CPU time between mutators and GC threads in gencon&quot; height=&quot;229&quot; src=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/fig6.gif&quot; style=&quot;background-color: white; border: 0px; padding: 0.3em 5px 0.7em;&quot; width=&quot;542&quot; /&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
A scavenge is short (shown by the small red boxes). Gray indicates that concurrent tracing starts followed by a collection of the tenured space, some of which happens concurrently. This is called a&amp;nbsp;&lt;i&gt;global collection&lt;/i&gt;, and it includes both a scavenge and a tenure space collection. How often a global collection occurs depends on the heap sizes and object lifetimes. The tenured space collection should be relatively quick because most of it has been collected concurrently.&lt;/div&gt;
&lt;div class=&quot;ibm-alternate-rule&quot; style=&quot;background-color: white; background-image: url(http://1.www.s81c.com/i/solid.gif); background-repeat: repeat no-repeat; clear: both; height: 1px;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;ibm-ind-link ibm-back-to-top&quot; style=&quot;background-color: white; clear: both; font-family: arial, sans-serif; font-size: 0.76em; height: 15px; padding: 5px; text-align: right;&quot;&gt;
&lt;a class=&quot;ibm-anchor-up-link&quot; href=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/#ibm-pcon&quot; style=&quot;background-image: url(http://1.www.s81c.com/i/v16/icons/u_bold.gif); background-position: 0px -1px; background-repeat: no-repeat no-repeat; color: #996699; display: inline; font-weight: bold; margin: 0px; padding: 0px 0px 0px 18px; text-decoration: initial;&quot;&gt;Back to top&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;6&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;subpool&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
The&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;subpool&lt;/code&gt;&amp;nbsp;policy can help increase performance on multiprocessor systems. As I mentioned earlier, this policy is available only on IBM pSeries and zSeries machines. The heap layout is the same as that for the&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;optthruput&lt;/code&gt;&amp;nbsp;policy, but the structure of the free list is different. Rather than having one free list for the entire heap, there are multiple lists, known as&amp;nbsp;&lt;i&gt;subpools.&lt;/i&gt;&amp;nbsp;Each pool has an associated size by which the pools are ordered. An allocation request of a certain size can quickly be satisfied by going to the pool with that size. Atomic (platform-dependent) high-performing instructions are used to pop a free list entry off the list, avoiding serialized access. Figure 7 shows how the free chunks of storage are organized by size:&lt;/div&gt;
&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;figure7&quot; style=&quot;background-color: white;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;Figure 7. Subpool free chunks ordered by size&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;img alt=&quot;Subpool free chunks ordered by size&quot; height=&quot;242&quot; src=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/fig7.gif&quot; style=&quot;background-color: white; border: 0px; padding: 0.3em 5px 0.7em;&quot; width=&quot;157&quot; /&gt;&lt;br style=&quot;background-color: white;&quot; /&gt;&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
When the JVMs start or when a compaction has occurred, the subpools are not used because there are large areas of the heap free. In these situations, each processor gets its own dedicated mini-heap to satisfy requests. When the first garbage collection occurs, the sweep phase starts populating the subpools, and subsequent allocations mainly use subpools.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
The&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;subpool&lt;/code&gt;&amp;nbsp;policy can reduce the time it takes to allocate objects. Atomic instructions ensure that allocations happen without acquiring a global heap lock. Mini-heaps local to a processor increase efficiency because cache interference is reduced. This has a direct effect on scalability, especially on multiprocessor systems. On platforms where&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;subpool&lt;/code&gt;&amp;nbsp;is not available, generational GC can provide similar benefits.&lt;/div&gt;
&lt;div class=&quot;ibm-alternate-rule&quot; style=&quot;background-color: white; background-image: url(http://1.www.s81c.com/i/solid.gif); background-repeat: repeat no-repeat; clear: both; height: 1px;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;ibm-ind-link ibm-back-to-top&quot; style=&quot;background-color: white; clear: both; font-family: arial, sans-serif; font-size: 0.76em; height: 15px; padding: 5px; text-align: right;&quot;&gt;
&lt;a class=&quot;ibm-anchor-up-link&quot; href=&quot;http://www.ibm.com/developerworks/library/j-ibmjava2/#ibm-pcon&quot; style=&quot;background-image: url(http://1.www.s81c.com/i/v16/icons/u_bold.gif); background-position: 0px -1px; background-repeat: no-repeat no-repeat; color: #996699; display: inline; font-weight: bold; margin: 0px; padding: 0px 0px 0px 18px; text-decoration: initial;&quot;&gt;Back to top&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;7&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 0.76em; padding: 0.3em 5px 0.7em;&quot;&gt;
This article has highlighted the different GC policy choices available in the IBM SDK 5.0 and some of their characteristics. The default policy is sufficient for most applications; however, in some situations, other policies perform better. I have described some general scenarios in which you should consider switching to&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;optavgpause&lt;/code&gt;,&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;gencon&lt;/code&gt;, or&amp;nbsp;&lt;code style=&quot;font-size: small !important;&quot;&gt;subpool&lt;/code&gt;. Measuring application performance when evaluating a policy is very important, and Part 2 demonstrates this evaluation process in more detail.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/8474191629224588307/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2013/02/java-technology-ibm-style-garbage.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/8474191629224588307'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/8474191629224588307'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2013/02/java-technology-ibm-style-garbage.html' title='Java technology, IBM style: Garbage collection policies, Part 1'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-4766444519926292828</id><published>2012-10-16T00:32:00.001-07:00</published><updated>2012-10-16T00:32:33.106-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="TechNology"/><title type='text'>clone() and the Cloneable Interface in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px;&quot;&gt;Most of the methods defined by&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px; margin: 0px; padding: 0px;&quot;&gt;Object&amp;nbsp;&lt;/b&gt;&lt;span style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px;&quot;&gt;are discussed elsewhere in this book. However, one deserves special attention:&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px; margin: 0px; padding: 0px;&quot;&gt;clone( )&lt;/b&gt;&lt;span style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px;&quot;&gt;. The&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px; margin: 0px; padding: 0px;&quot;&gt;clone( )&amp;nbsp;&lt;/b&gt;&lt;span style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px;&quot;&gt;method generates a duplicate copy of the object on which it is called. Only classes that implement the&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px; margin: 0px; padding: 0px;&quot;&gt;Cloneable&amp;nbsp;&lt;/b&gt;&lt;span style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px;&quot;&gt;interface can be cloned.&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px; margin: 10px 15px; padding: 0px;&quot;&gt;
The&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Cloneable&amp;nbsp;&lt;/b&gt;interface defines no members. It is used to indicate that a class allows a bitwise copy of an object (that is, a&amp;nbsp;&lt;i style=&quot;margin: 0px; padding: 0px;&quot;&gt;clone&lt;/i&gt;) to be made. If you try to call&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;clone( )&amp;nbsp;&lt;/b&gt;on a class that does not implement&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Cloneable&lt;/b&gt;, a&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;CloneNotSupportedException&amp;nbsp;&lt;/b&gt;is thrown. When a clone is made, the constructor for the object being cloned is&amp;nbsp;&lt;i style=&quot;margin: 0px; padding: 0px;&quot;&gt;not&amp;nbsp;&lt;/i&gt;called. A clone is simply an exact copy of the original.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px; margin: 10px 15px; padding: 0px;&quot;&gt;
Cloning is a potentially dangerous action, because it can cause unintended side effects. For example, if the object being cloned contains a reference variable called&amp;nbsp;&lt;i style=&quot;margin: 0px; padding: 0px;&quot;&gt;obRef,&amp;nbsp;&lt;/i&gt;then when the clone is made,&amp;nbsp;&lt;i style=&quot;margin: 0px; padding: 0px;&quot;&gt;obRef&lt;/i&gt;in the clone will refer to the same object as does&amp;nbsp;&lt;i style=&quot;margin: 0px; padding: 0px;&quot;&gt;obRef&amp;nbsp;&lt;/i&gt;in the original. If the clone makes a change to the contents of the object referred to by&amp;nbsp;&lt;i style=&quot;margin: 0px; padding: 0px;&quot;&gt;obRef,&amp;nbsp;&lt;/i&gt;then it will be changed for the original object, too. Here is another example. If an object opens an I/O stream and is then cloned, two objects will be capable of operating on the same stream. Further, if one of these objects closes the stream, the other object might still attempt to write to it, causing an error.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px; margin: 10px 15px; padding: 0px;&quot;&gt;
Because cloning can cause problems,&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;clone( )&amp;nbsp;&lt;/b&gt;is declared as&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;protected&amp;nbsp;&lt;/b&gt;inside&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Object&lt;/b&gt;. This means that it must either be called from within a method defined by the class that implements&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Cloneable&lt;/b&gt;, or it must be explicitly overridden by that class so that it is public. Let&#39;s look at an example of each approach.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px; margin: 10px 15px; padding: 0px;&quot;&gt;
The following program implements&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Cloneable&amp;nbsp;&lt;/b&gt;and defines the method&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;cloneTest( )&lt;/b&gt;, which calls&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;clone( )&amp;nbsp;&lt;/b&gt;in&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Object&lt;/b&gt;:&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px; margin: 10px 15px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: navy; font-family: Arial; font-size: 13px; line-height: 17.266666412353516px; margin: 10px 15px; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &#39;Trebuchet MS&#39;, Tahoma, sans-serif; font-size: 16px; font-weight: bold; line-height: 24px; margin: 10px 15px; padding: 0px;&quot;&gt;
// Demonstrate the clone() method.&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;class TestClone implements Cloneable {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;int a;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;double b;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;// This method calls Object&#39;s clone().&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;TestClone cloneTest() {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;try {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;// call clone in Object.&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;return (TestClone) super.clone();&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;} catch(CloneNotSupportedException e) {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;System.out.println(&quot;Cloning not allowed.&quot;);&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;return this;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;}&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;}&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;}&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &#39;Trebuchet MS&#39;, Tahoma, sans-serif; font-size: 16px; font-weight: bold; line-height: 24px; margin: 10px 15px; padding: 0px;&quot;&gt;
class CloneDemo {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;public static void main(String args[]) {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;TestClone x1 = new TestClone();&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;TestClone x2;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;x1.a = 10;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;x1.b = 20.98;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;x2 = x1.cloneTest(); // clone x1&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;System.out.println(&quot;x1: &quot; + x1.a + &quot; &quot; + x1.b);&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;System.out.println(&quot;x2: &quot; + x2.a + &quot; &quot; + x2.b);&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;}&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;}&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &#39;Trebuchet MS&#39;, Tahoma, sans-serif; font-size: 16px; font-weight: bold; line-height: 24px; margin: 10px 15px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #666666; font-family: &#39;Trebuchet MS&#39;, Tahoma, sans-serif; font-size: 16px; font-weight: bold; line-height: 24px; margin: 10px 15px; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: navy; font-family: Arial; font-size: 13px; font-weight: normal; line-height: 17.266666412353516px; margin: 10px 15px; padding: 0px;&quot;&gt;
Here, the method&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;cloneTest( )&amp;nbsp;&lt;/b&gt;calls&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;clone( )&amp;nbsp;&lt;/b&gt;in&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Object&amp;nbsp;&lt;/b&gt;and returns the result. Notice that the object returned by&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;clone( )&amp;nbsp;&lt;/b&gt;must be cast into its appropriate type (&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;TestClone&lt;/b&gt;). The following example overrides&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;clone( )&amp;nbsp;&lt;/b&gt;so that it can be called from code outside of its class. To do this, its access specifier must be&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;public&lt;/b&gt;, as shown here:&lt;/div&gt;
&lt;blockquote style=&quot;background-color: #fafafa; background-image: url(http://www.java-samples.com/images/quote.gif); background-position: 3px 5px; background-repeat: no-repeat no-repeat; border: 1px solid rgb(238, 238, 238); font-size: 1.2em; line-height: 1.5em; margin: 15px; padding: 0px 0px 0px 20px;&quot;&gt;
&lt;div style=&quot;margin: 10px 15px; padding: 0px;&quot;&gt;
// Override the clone() method.&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;class TestClone implements Cloneable {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;int a;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;double b;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;// clone() is now overridden and is public.&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;public Object clone() {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;try {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;// call clone in Object.&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;return super.clone();&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;} catch(CloneNotSupportedException e) {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;System.out.println(&quot;Cloning not allowed.&quot;);&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;return this;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;}&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;}&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;}&lt;/div&gt;
&lt;div style=&quot;margin: 10px 15px; padding: 0px;&quot;&gt;
class CloneDemo2 {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;public static void main(String args[]) {&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;TestClone x1 = new TestClone();&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;TestClone x2;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;x1.a = 10;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;x1.b = 20.98;&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;// here, clone() is called directly.&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;x2 = (TestClone) x1.clone();&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;System.out.println(&quot;x1: &quot; + x1.a + &quot; &quot; + x1.b);&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;System.out.println(&quot;x2: &quot; + x2.a + &quot; &quot; + x2.b);&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;}&amp;nbsp;&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;}&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div style=&quot;color: navy; font-family: Arial; font-size: 13px; font-weight: normal; line-height: 17.266666412353516px; margin: 10px 15px; padding: 0px;&quot;&gt;
The side effects caused by cloning are sometimes difficult to see at first. It is easy to think that a class is safe for cloning when it actually is not. In general, you should not implement&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Cloneable&amp;nbsp;&lt;/b&gt;for any class without good reason.&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/4766444519926292828/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/10/clone-and-cloneable-interface-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/4766444519926292828'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/4766444519926292828'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/10/clone-and-cloneable-interface-in-java.html' title='clone() and the Cloneable Interface in Java'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-8119926506256259884</id><published>2012-10-08T22:01:00.001-07:00</published><updated>2012-10-08T22:01:58.814-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="oracle"/><category scheme="http://www.blogger.com/atom/ns#" term="TechNology"/><title type='text'>Analytic functions by Example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
This article provides a clear, thorough concept of analytic functions and its various options by a series of simple yet concept building examples. The article is intended for SQL coders, who for might be not be using analytic functions due to unfamiliarity with its cryptic syntax or uncertainty about its logic of operation. Often I see that people tend to reinvent the feature provided by analytic functions by native join and sub-query SQL. This article assumes familiarity with basic Oracle SQL, sub-query, join and group function from the reader. Based on that familiarity, it builds the concept of analytic functions through a series of examples.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
It is true that whatever an analytic function does can be done by native SQL, with join and sub-queries. But the same routine done by analytic function is always faster, or at least as fast, when compared to native SQL. Moreover, I am not considering here the amount of time that is spent in coding the native SQLs, testing, debugging and tuning them.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The general syntax of analytic function is:&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Function(arg1,..., argn) OVER ( [PARTITION BY &amp;lt;...&amp;gt;] [ORDER BY &amp;lt;....&amp;gt;] [&amp;lt;window_clause&amp;gt;] )&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&amp;lt;window_clause&amp;gt; is like &quot;ROW &amp;lt;?&amp;gt;&quot; or &quot;RANK &amp;lt;?&amp;gt;&quot;&lt;br /&gt;All the keywords will be dealt in details as we walk through the examples. The script for creating the schema (SCOTT) on which the example queries of this article are run can be obtained in ORACLE_HOME/sqlplus/demo/demobld.sql of any standard Oracle installation.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;How_are_analytic_functions_different_from_group_or_aggregate_functions&quot;&gt;How are analytic functions different from group or aggregate functions?&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;bb-code-block&quot; style=&quot;background-color: #f8fbfc; border-color: rgb(191, 208, 217); border-style: solid; border-width: 1px 1px 1px 4px; clear: both; font-family: &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace; font-size: 0.75em; line-height: 1.5em; min-height: 3em; overflow: auto; padding: 0.5em; width: 1049px;&quot;&gt;SELECT deptno,
COUNT(*) DEPT_COUNT
FROM emp
WHERE deptno IN (20, 30)
GROUP BY deptno;

DEPTNO &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DEPT_COUNT &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
---------------------- ---------------------- 
20 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;
30 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;

2 rows selected&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; font-weight: bold; line-height: 19px;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
Query-1&lt;/div&gt;
&lt;/span&gt;&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
Consider the&amp;nbsp;&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-1&lt;/span&gt;&amp;nbsp;and its result.&amp;nbsp;&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-1&lt;/span&gt;&amp;nbsp;returns departments and their employee count. Most importantly it groups the records into departments in accordance with the GROUP BY clause. As such any non-&quot;group by&quot; column is not allowed in the select clause.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;bb-code-block&quot; style=&quot;background-color: #f8fbfc; border-color: rgb(191, 208, 217); border-style: solid; border-width: 1px 1px 1px 4px; clear: both; font-family: &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace; font-size: 0.75em; line-height: 1.5em; min-height: 3em; overflow: auto; padding: 0.5em; width: 1049px;&quot;&gt;SELECT empno, deptno, 
COUNT(*) OVER (PARTITION BY 
deptno) DEPT_COUNT
FROM emp
WHERE deptno IN (20, 30);

     EMPNO     DEPTNO DEPT_COUNT
---------- ---------- ----------
      7369         20          5
      7566         20          5
      7788         20          5
      7902         20          5
      7876         20          5
      7499         30          6
      7900         30          6
      7844         30          6
      7698         30          6
      7654         30          6
      7521         30          6

11 rows selected.&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; font-weight: bold; line-height: 19px;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
Query-2&lt;/div&gt;
&lt;/span&gt;&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
Now consider the analytic function query (&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-2&lt;/span&gt;) and its result. Note the repeating values of DEPT_COUNT column.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
This brings out the main difference between aggregate and analytic functions. Though analytic functions give aggregate result they do not group the result set. They return the group value multiple times with each record. As such any other non-&quot;group by&quot; column or expression can be present in the select clause, for example, the column EMPNO in&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-2&lt;/span&gt;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
Analytic functions are computed after all joins, WHERE clause, GROUP BY and HAVING are computed on the query. The main ORDER BY clause of the query operates after the analytic functions. So analytic functions can only appear in the select list and in the main ORDER BY clause of the query.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
In absence of any PARTITION or &amp;lt;window_clause&amp;gt; inside the OVER( ) portion, the function acts on entire record set returned by the where clause. Note the results of&amp;nbsp;&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-3&lt;/span&gt;&amp;nbsp;and compare it with the result of aggregate function query&amp;nbsp;&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-4&lt;/span&gt;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;bb-code-block&quot; style=&quot;background-color: #f8fbfc; border-color: rgb(191, 208, 217); border-style: solid; border-width: 1px 1px 1px 4px; clear: both; font-family: &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace; font-size: 0.75em; line-height: 1.5em; min-height: 3em; overflow: auto; padding: 0.5em; width: 1049px;&quot;&gt;SELECT empno, deptno, 
COUNT(*) OVER ( ) CNT
FROM emp
WHERE deptno IN (10, 20)
ORDER BY 2, 1;

     EMPNO     DEPTNO        CNT
---------- ---------- ----------
      7782         10          8
      7839         10          8
      7934         10          8
      7369         20          8
      7566         20          8
      7788         20          8
      7876         20          8
      7902         20          8&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; font-weight: bold; line-height: 19px;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
Query-3&lt;/div&gt;
&lt;/span&gt;&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;bb-code-block&quot; style=&quot;background-color: #f8fbfc; border-color: rgb(191, 208, 217); border-style: solid; border-width: 1px 1px 1px 4px; clear: both; font-family: &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace; font-size: 0.75em; line-height: 1.5em; min-height: 3em; overflow: auto; padding: 0.5em; width: 1049px;&quot;&gt;SELECT COUNT(*) FROM emp
WHERE deptno IN (10, 20);

  COUNT(*)
----------
         8&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; font-weight: bold; line-height: 19px;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
Query-4&lt;/div&gt;
&lt;/span&gt;&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;How_to_break_the_result_set_in_groups_or_partitions&quot;&gt;How to break the result set in groups or partitions?&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
It might be obvious from the previous example that the clause PARTITION BY is used to break the result set into groups. PARTITION BY can take any non-analytic SQL expression.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
Some functions support the &amp;lt;window_clause&amp;gt; inside the partition to further limit the records they act on. In the absence of any &amp;lt;window_clause&amp;gt; analytic functions are computed on all the records of the partition clause.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The functions SUM, COUNT, AVG, MIN, MAX are the common analytic functions the result of which does not depend on the order of the records.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
Functions like LEAD, LAG, RANK, DENSE_RANK, ROW_NUMBER, FIRST, FIRST VALUE, LAST, LAST VALUE depends on order of records. In the next example we will see how to specify that.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;How_to_specify_the_order_of_the_records_in_the_partition&quot;&gt;How to specify the order of the records in the partition?&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The answer is simple, by the &quot;ORDER BY&quot; clause inside the OVER( ) clause. This is different from the ORDER BY clause of the main query which comes after WHERE. In this section we go ahead and introduce each of the very useful functions LEAD, LAG, RANK, DENSE_RANK, ROW_NUMBER, FIRST, FIRST VALUE, LAST, LAST VALUE and show how each depend on the order of the record.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The general syntax of specifying the ORDER BY clause in analytic function is:&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;ORDER BY &amp;lt;sql_expr&amp;gt; [ASC or DESC] NULLS [FIRST or LAST]&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The syntax is self-explanatory.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;ROW_NUMBER_RANK_and_DENSE_RANK&quot;&gt;ROW_NUMBER, RANK and DENSE_RANK&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
All the above three functions assign integer values to the rows depending on their order. That is the reason of clubbing them together.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;ROW_NUMBER( )&lt;/span&gt;&amp;nbsp;gives a running serial number to a partition of records. It is very useful in reporting, especially in places where different partitions have their own serial numbers. In&amp;nbsp;&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-5&lt;/span&gt;, the function ROW_NUMBER( ) is used to give separate sets of running serial to employees of departments 10 and 20 based on their HIREDATE.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;bb-code-block&quot; style=&quot;background-color: #f8fbfc; border-color: rgb(191, 208, 217); border-style: solid; border-width: 1px 1px 1px 4px; clear: both; font-family: &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace; font-size: 0.75em; line-height: 1.5em; min-height: 3em; overflow: auto; padding: 0.5em; width: 1049px;&quot;&gt;SELECT empno, deptno, hiredate,
ROW_NUMBER( ) OVER (PARTITION BY
deptno ORDER BY hiredate
NULLS LAST) SRLNO
FROM emp
WHERE deptno IN (10, 20)
ORDER BY deptno, SRLNO;

EMPNO  DEPTNO HIREDATE       SRLNO
------ ------- --------- ----------
  7782      10 09-JUN-81          1
  7839      10 17-NOV-81          2
  7934      10 23-JAN-82          3
  7369      20 17-DEC-80          1
  7566      20 02-APR-81          2
  7902      20 03-DEC-81          3
  7788      20 09-DEC-82          4
  7876      20 12-JAN-83          5

8 rows selected.&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; font-weight: bold; line-height: 19px;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
Query-5 (ROW_NUMBER example)&lt;/div&gt;
&lt;/span&gt;&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
RANK and DENSE_RANK both provide rank to the records based on some column value or expression. In case of a tie of 2 records at position N, RANK declares 2 positions N and skips position N+1 and gives position N+2 to the next record. While DENSE_RANK declares 2 positions N but does not skip position N+1.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-6&lt;/span&gt;&amp;nbsp;shows the usage of both RANK and DENSE_RANK. For DEPTNO 20 there are two contenders for the first position (EMPNO 7788 and 7902). Both RANK and DENSE_RANK declares them as joint toppers. RANK skips the next value that is 2 and next employee EMPNO 7566 is given the position 3. For DENSE_RANK there are no such gaps.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;bb-code-block&quot; style=&quot;background-color: #f8fbfc; border-color: rgb(191, 208, 217); border-style: solid; border-width: 1px 1px 1px 4px; clear: both; font-family: &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace; font-size: 0.75em; line-height: 1.5em; min-height: 3em; overflow: auto; padding: 0.5em; width: 1049px;&quot;&gt;SELECT empno, deptno, sal,
RANK() OVER (PARTITION BY deptno
ORDER BY sal DESC NULLS LAST) RANK,
DENSE_RANK() OVER (PARTITION BY
deptno ORDER BY sal DESC NULLS
LAST) DENSE_RANK
FROM emp
WHERE deptno IN (10, 20)
ORDER BY 2, RANK;

EMPNO  DEPTNO   SAL  RANK DENSE_RANK
------ ------- ----- ----- ----------
  7839      10  5000     1          1
  7782      10  2450     2          2
  7934      10  1300     3          3
  7788      20  3000     1          1
  7902      20  3000     1          1
  7566      20  2975     3          2
  7876      20  1100     4          3
  7369      20   800     5          4

8 rows selected.&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; font-weight: bold; line-height: 19px;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
Query-6 (RANK and DENSE_RANK example)&lt;/div&gt;
&lt;/span&gt;&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;LEAD_and_LAG&quot;&gt;LEAD and LAG&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
LEAD has the ability to compute an expression on the next rows (rows which are going to come after the current row) and return the value to the current row. The general syntax of LEAD is shown below:&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;LEAD (&amp;lt;sql_expr&amp;gt;, &amp;lt;offset&amp;gt;, &amp;lt;default&amp;gt;) OVER (&amp;lt;analytic_clause&amp;gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&amp;lt;sql_expr&amp;gt; is the expression to compute from the leading row.&lt;br /&gt;&amp;lt;offset&amp;gt; is the index of the leading row relative to the current row.&lt;br /&gt;&amp;lt;offset&amp;gt; is a positive integer with default 1.&lt;br /&gt;&amp;lt;default&amp;gt; is the value to return if the &amp;lt;offset&amp;gt; points to a row outside the partition range.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The syntax of LAG is similar except that the offset for LAG goes into the previous rows.&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-7&lt;/span&gt;&amp;nbsp;and its result show simple usage of LAG and LEAD function.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;bb-code-block&quot; style=&quot;background-color: #f8fbfc; border-color: rgb(191, 208, 217); border-style: solid; border-width: 1px 1px 1px 4px; clear: both; font-family: &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace; font-size: 0.75em; line-height: 1.5em; min-height: 3em; overflow: auto; padding: 0.5em; width: 1049px;&quot;&gt;SELECT deptno, empno, sal,
LEAD(sal, 1, 0) OVER (PARTITION BY dept ORDER BY sal DESC NULLS LAST) NEXT_LOWER_SAL,
LAG(sal, 1, 0) OVER (PARTITION BY dept ORDER BY sal DESC NULLS LAST) PREV_HIGHER_SAL
FROM emp
WHERE deptno IN (10, 20)
ORDER BY deptno, sal DESC;

 DEPTNO  EMPNO   SAL NEXT_LOWER_SAL PREV_HIGHER_SAL
------- ------ ----- -------------- ---------------
     10   7839  5000           2450               0
     10   7782  2450           1300            5000
     10   7934  1300              0            2450
     20   7788  3000           3000               0
     20   7902  3000           2975            3000
     20   7566  2975           1100            3000
     20   7876  1100            800            2975
     20   7369   800              0            1100

8 rows selected.&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; font-weight: bold; line-height: 19px;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
Query-7 (LEAD and LAG)&lt;/div&gt;
&lt;/span&gt;&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;FIRST_VALUE_and_LAST_VALUE_function&quot;&gt;FIRST VALUE and LAST VALUE function&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The general syntax is:&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;FIRST_VALUE(&amp;lt;sql_expr&amp;gt;) OVER (&amp;lt;analytic_clause&amp;gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The FIRST_VALUE analytic function picks the first record from the partition after doing the ORDER BY. The &amp;lt;sql_expr&amp;gt; is computed on the columns of this first record and results are returned. The LAST_VALUE function is used in similar context except that it acts on the last record of the partition.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;bb-code-block&quot; style=&quot;background-color: #f8fbfc; border-color: rgb(191, 208, 217); border-style: solid; border-width: 1px 1px 1px 4px; clear: both; font-family: &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace; font-size: 0.75em; line-height: 1.5em; min-height: 3em; overflow: auto; padding: 0.5em; width: 1049px;&quot;&gt;-- How many days after the first hire of each department were the next
-- employees hired?

SELECT empno, deptno, hiredate ? FIRST_VALUE(hiredate)
OVER (PARTITION BY deptno ORDER BY hiredate) DAY_GAP
FROM emp
WHERE deptno IN (20, 30)
ORDER BY deptno, DAY_GAP;

     EMPNO     DEPTNO    DAY_GAP
---------- ---------- ----------
      7369         20          0
      7566         20        106
      7902         20        351
      7788         20        722
      7876         20        756
      7499         30          0
      7521         30          2
      7698         30         70
      7844         30        200
      7654         30        220
      7900         30        286

11 rows selected.&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; font-weight: bold; line-height: 19px;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
Query-8 (FIRST_VALUE)&lt;/div&gt;
&lt;/span&gt;&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;FIRST_and_LAST_function&quot;&gt;FIRST and LAST function&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The FIRST function (or more properly KEEP FIRST function) is used in a very special situation. Suppose we rank a group of record and found several records in the first rank. Now we want to apply an aggregate function on the records of the first rank. KEEP FIRST enables that.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The general syntax is:&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Function( ) KEEP (DENSE_RANK FIRST ORDER BY &amp;lt;expr&amp;gt;) OVER (&amp;lt;partitioning_clause&amp;gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
Please note that FIRST and LAST are the only functions that deviate from the general syntax of analytic functions. They do not have the ORDER BY inside the OVER clause. Neither do they support any &amp;lt;window&amp;gt; clause. The ranking done in FIRST and LAST is always DENSE_RANK. The query below shows the usage of FIRST function. The LAST function is used in similar context to perform computations on last ranked records.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;bb-code-block&quot; style=&quot;background-color: #f8fbfc; border-color: rgb(191, 208, 217); border-style: solid; border-width: 1px 1px 1px 4px; clear: both; font-family: &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace; font-size: 0.75em; line-height: 1.5em; min-height: 3em; overflow: auto; padding: 0.5em; width: 1049px;&quot;&gt;-- How each employee&#39;s salary compare with the average salary of the first
-- year hires of their department?

SELECT empno, deptno, TO_CHAR(hiredate,&#39;YYYY&#39;) HIRE_YR, sal,
TRUNC(
AVG(sal) KEEP (DENSE_RANK FIRST
ORDER BY TO_CHAR(hiredate,&#39;YYYY&#39;) )
OVER (PARTITION BY deptno)
     ) AVG_SAL_YR1_HIRE
FROM emp
WHERE deptno IN (20, 10)
ORDER BY deptno, empno, HIRE_YR;

     EMPNO     DEPTNO HIRE        SAL AVG_SAL_YR1_HIRE
---------- ---------- ---- ---------- ----------------
      7782         10 1981       2450             3725
      7839         10 1981       5000             3725
      7934         10 1982       1300             3725
      7369         20 1980        800              800
      7566         20 1981       2975              800
      7788         20 1982       3000              800
      7876         20 1983       1100              800
      7902         20 1981       3000              800

8 rows selected.&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; font-weight: bold; line-height: 19px;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
Query-9 (KEEP FIRST)&lt;/div&gt;
&lt;/span&gt;&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;How_to_specify_the_Window_clause_ROW_type_or_RANGE_type_windows&quot;&gt;How to specify the Window clause (ROW type or RANGE type windows)?&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
Some analytic functions (AVG, COUNT, FIRST_VALUE, LAST_VALUE, MAX, MIN and SUM among the ones we discussed) can take a window clause to further sub-partition the result and apply the analytic function. An important feature of the windowing clause is that it is dynamic in nature.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The general syntax of the &amp;lt;window_clause&amp;gt; is&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;[ROW or RANGE] BETWEEN &amp;lt;start_expr&amp;gt; AND &amp;lt;end_expr&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&amp;lt;start_expr&amp;gt; can be any one of the following&amp;nbsp;&lt;/div&gt;
&lt;ol style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;
&lt;li&gt;UNBOUNDED PECEDING&lt;/li&gt;
&lt;li&gt;CURRENT ROW&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&amp;lt;&lt;span style=&quot;font-weight: bold;&quot;&gt;sql_expr&lt;/span&gt;&amp;gt; PRECEDING or FOLLOWING.&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;ol style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;&amp;lt;end_expr&amp;gt; can be any one of the following
&lt;li&gt;UNBOUNDED FOLLOWING or&lt;/li&gt;
&lt;li&gt;CURRENT ROW or&lt;/li&gt;
&lt;li&gt;&amp;lt;&lt;span style=&quot;font-weight: bold;&quot;&gt;sql_expr&lt;/span&gt;&amp;gt; PRECEDING or FOLLOWING.&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
For ROW type windows the definition is in terms of row numbers before or after the current row. So for ROW type windows&amp;nbsp;&lt;span style=&quot;font-weight: bold;&quot;&gt;&amp;lt;sql_expr&amp;gt;&lt;/span&gt;&amp;nbsp;must evaluate to a positive integer.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
For RANGE type windows the definition is in terms of values before or after the current ORDER. We will take this up in details latter.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The ROW or RANGE window cannot appear together in one OVER clause. The window clause is defined in terms of the current row. But may or may not include the current row. The start point of the window and the end point of the window can finish before the current row or after the current row. Only start point cannot come after the end point of the window. In case any point of the window is undefined the default is UNBOUNDED PRECEDING for &amp;lt;start_expr&amp;gt; and UNBOUNDED FOLLOWING for &amp;lt;end_expr&amp;gt;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
If the end point is the current row, syntax only in terms of the start point can be can be&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;[ROW or RANGE] [&amp;lt;start_expr&amp;gt; PRECEDING or UNBOUNDED PRECEDING ]&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
[ROW or RANGE] CURRENT ROW is also allowed but this is redundant. In this case the function behaves as a single-row function and acts only on the current row.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;ROW_Type_Windows&quot;&gt;ROW Type Windows&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
For analytic functions with ROW type windows, the general syntax is:&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Function( ) OVER (PARTITIN BY &amp;lt;expr1&amp;gt; ORDER BY &amp;lt;expr2,..&amp;gt; ROWS BETWEEN &amp;lt;start_expr&amp;gt; AND &amp;lt;end_expr&amp;gt;)&lt;/span&gt;&lt;br /&gt;or&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Function( ) OVER (PARTITON BY &amp;lt;expr1&amp;gt; ORDER BY &amp;lt;expr2,..&amp;gt; ROWS [&amp;lt;start_expr&amp;gt; PRECEDING or UNBOUNDED PRECEDING]&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
For ROW type windows the windowing clause is in terms of record numbers.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The query&amp;nbsp;&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-10&lt;/span&gt;&amp;nbsp;has no apparent real life description (&lt;span style=&quot;font-style: italic;&quot;&gt;except column FROM_PU_C&lt;/span&gt;) but the various windowing clause are illustrated by a COUNT(*) function. The count simply shows the number of rows inside the window definition. Note the build up of the count for each column for the YEAR 1981.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The column FROM_P3_TO_F1 shows an example where start point of the window is before the current row and end point of the window is after current row. This is a 5 row window; it shows values less than 5 during the beginning and end.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;bb-code-block&quot; style=&quot;background-color: #f8fbfc; border-color: rgb(191, 208, 217); border-style: solid; border-width: 1px 1px 1px 4px; clear: both; font-family: &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace; font-size: 0.75em; line-height: 1.5em; min-height: 3em; overflow: auto; padding: 0.5em; width: 1049px;&quot;&gt;-- The query below has no apparent real life description (except 
-- column FROM_PU_C) but is remarkable in illustrating the various windowing
-- clause by a COUNT(*) function.
 
SELECT empno, deptno, TO_CHAR(hiredate, &#39;YYYY&#39;) YEAR,
COUNT(*) OVER (PARTITION BY TO_CHAR(hiredate, &#39;YYYY&#39;)
ORDER BY hiredate ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) FROM_P3_TO_F1,
COUNT(*) OVER (PARTITION BY TO_CHAR(hiredate, &#39;YYYY&#39;)
ORDER BY hiredate ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM_PU_TO_C,
COUNT(*) OVER (PARTITION BY TO_CHAR(hiredate, &#39;YYYY&#39;)
ORDER BY hiredate ROWS &lt;span style=&quot;font-weight: bold;&quot;&gt;BETWEEN 3 PRECEDING AND 1 PRECEDING&lt;/span&gt;) FROM_P2_TO_P1,
COUNT(*) OVER (PARTITION BY TO_CHAR(hiredate, &#39;YYYY&#39;)
ORDER BY hiredate ROWS &lt;span style=&quot;font-weight: bold;&quot;&gt;BETWEEN 1 FOLLOWING AND 3 FOLLOWING&lt;/span&gt;) FROM_F1_TO_F3
FROM emp
ORDEDR BY hiredate

 EMPNO  DEPTNO YEAR FROM_P3_TO_F1 FROM_PU_TO_C FROM_P2_TO_P1 FROM_F1_TO_F3
------ ------- ---- ------------- ------------ ------------- -------------
  7369      20 1980             1            1             0             0
  &amp;lt;font bgcolor=yellow&amp;gt;7499      30 1981             2            1             0             3
  7521      30 1981             3            2             1             3
  7566      20 1981             4            3             2             3
  7698      30 1981             5            4             3             3
  7782      10 1981             5            5             3             3
  7844      30 1981             5            6             3             3
  7654      30 1981             5            7             3             3
  7839      10 1981             5            8             3             2
  7900      30 1981             5            9             3             1
  7902      20 1981             4           10             3             0&amp;lt;/font&amp;gt;
  7934      10 1982             2            1             0             1
  7788      20 1982             2            2             1             0
  7876      20 1983             1            1             0             0

14 rows selected.&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; font-weight: bold; line-height: 19px;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
Query-10 (ROW type windowing example)&lt;/div&gt;
&lt;/span&gt;&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The column FROM_PU_TO_CURR shows an example where start point of the window is before the current row and end point of the window is the current row. This column only has some real world significance. It can be thought of as the yearly employee build-up of the organization as each employee is getting hired.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The column FROM_P2_TO_P1 shows an example where start point of the window is before the current row and end point of the window is before the current row. This is a 3 row window and the count remains constant after it has got 3 previous rows.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The column FROM_F1_TO_F3 shows an example where start point of the window is after the current row and end point of the window is after the current row. This is a reverse of the previous column. Note how the count declines during the end.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;RANGE_Windows&quot;&gt;RANGE Windows&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
For RANGE windows the general syntax is same as that of ROW:&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Function( ) OVER (PARTITION BY &amp;lt;expr1&amp;gt; ORDER BY &amp;lt;expr2&amp;gt; RANGE BETWEEN &amp;lt;start_expr&amp;gt; AND &amp;lt;end_expr&amp;gt;)&lt;br /&gt;or&lt;br /&gt;Function( ) OVER (PARTITION BY &amp;lt;expr1&amp;gt; ORDER BY &amp;lt;expr2&amp;gt; RANGE [&amp;lt;start_expr&amp;gt; PRECEDING or UNBOUNDED PRECEDING]&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
For &amp;lt;start_expr&amp;gt; or &amp;lt;end_expr&amp;gt; we can use UNBOUNDED PECEDING, CURRENT ROW or&amp;nbsp;&lt;span style=&quot;font-weight: bold;&quot;&gt;&amp;lt;sql_expr&amp;gt;&lt;/span&gt;&amp;nbsp;PRECEDING or FOLLOWING. However for RANGE type windows &amp;lt;sql_expr&amp;gt; must evaluate to value compatible with ORDER BY expression &amp;lt;expr1&amp;gt;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;&amp;lt;sql_expr&amp;gt;&lt;/span&gt;&amp;nbsp;is a logical offset. It must be a constant or expression that evaluates to a positive numeric value or an interval literal. Only one ORDER BY expression is allowed.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
If&amp;nbsp;&lt;span style=&quot;font-weight: bold;&quot;&gt;&amp;lt;sql_expr&amp;gt;&lt;/span&gt;&amp;nbsp;evaluates to a numeric value, then the ORDER BY expr must be a NUMBER or DATE datatype. If &amp;lt;sql_expr&amp;gt; evaluates to an interval value, then the ORDER BY expr must be a DATE datatype.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
Note the example (&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-11&lt;/span&gt;) below which uses RANGE windowing. The important thing here is that the size of the window in terms of the number of records can vary.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;pre class=&quot;bb-code-block&quot; style=&quot;background-color: #f8fbfc; border-color: rgb(191, 208, 217); border-style: solid; border-width: 1px 1px 1px 4px; clear: both; font-family: &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace; font-size: 0.75em; line-height: 1.5em; min-height: 3em; overflow: auto; padding: 0.5em; width: 1049px;&quot;&gt;-- For each employee give the count of employees getting half more that their 
-- salary and also the count of employees in the departments 20 and 30 getting half 
-- less than their salary.
 
SELECT deptno, empno, sal,
Count(*) OVER (PARTITION BY deptno ORDER BY sal RANGE
BETWEEN UNBOUNDED PRECEDING AND (sal/2) PRECEDING) CNT_LT_HALF,
COUNT(*) OVER (PARTITION BY deptno ORDER BY sal RANGE
BETWEEN (sal/2) FOLLOWING AND UNBOUNDED FOLLOWING) CNT_MT_HALF
FROM emp
WHERE deptno IN (20, 30)
ORDER BY deptno, sal

 DEPTNO  EMPNO   SAL CNT_LT_HALF CNT_MT_HALF
------- ------ ----- ----------- -----------
     20   7369   800           0           3
     20   7876  1100           0           3
     20   7566  2975           2           0
     20   7788  3000           2           0
     20   7902  3000           2           0
     30   7900   950           0           3
     30   7521  1250           0           1
     30   7654  1250           0           1
     30   7844  1500           0           1
     30   7499  1600           0           1
     30   7698  2850           3           0

11 rows selected.&lt;/pre&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; font-weight: bold; line-height: 19px;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
Query-11 (RANGE type windowing example)&lt;/div&gt;
&lt;/span&gt;&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;Order_of_computation_and_performance_tips&quot;&gt;Order of computation and performance tips&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
Defining the PARTITOIN BY and ORDER BY clauses on indexed columns (ordered in accordance with the PARTITION CLAUSE and then the ORDER BY clause in analytic function) will provide optimum performance. For&amp;nbsp;&lt;span style=&quot;font-weight: bold;&quot;&gt;Query-5&lt;/span&gt;, for example, a composite index on (deptno, hiredate) columns will prove effective.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
It is advisable to always use CBO for queries using analytic functions. The tables and indexes should be analyzed and optimizer mode should be CHOOSE.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
Even in absence of indexes analytic functions provide acceptable performance but need to do sorting for computing partition and order by clause. If the query contains multiple analytic functions, sorting and partitioning on two different columns should be avoided if they are both not indexed.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h3 style=&quot;background-color: white; color: #6699cc; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 1.5em; margin: 0px;&quot;&gt;
&lt;a href=&quot;&quot; name=&quot;Conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #333333; font-family: &#39;Trebuchet MS&#39;, Geneva, Arial, Helvetica, SunSans-Regular, Verdana, sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 1.2em; padding: 0px;&quot;&gt;
The aim of this article is not to make the reader try analytic functions forcibly in every other complex SQL. It is meant for a SQL coder, who has been avoiding analytic functions till now, even in complex analytic queries and reinventing the same feature much painstakingly by native SQL and join query. Its job is done if such a person finds analytic functions clear, understandable and usable after going through the article, and starts using them.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/8119926506256259884/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/10/analytic-functions-by-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/8119926506256259884'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/8119926506256259884'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/10/analytic-functions-by-example.html' title='Analytic functions by Example'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-1683885191626646963</id><published>2012-04-30T01:39:00.001-07:00</published><updated>2012-04-30T01:40:06.161-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="TechNology"/><title type='text'>Jmeter Sampler Client</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;a href=&quot;http://jmeter.apache.org/download_jmeter.cgi&quot; target=&quot;_blank&quot;&gt;Download&lt;/a&gt; &amp;nbsp;latest Jmeter binaries&amp;nbsp; and &lt;a href=&quot;http://jmeter.apache.org/usermanual/get-started.html#install&quot; target=&quot;_blank&quot;&gt;install &lt;/a&gt;as for instruction given.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
You can use some Java development tools like eclipse for developing client program.&lt;/div&gt;
&lt;div&gt;
i am just creating simple sample program using eclipse because it provide lot of utility like auto compile, create jar etc.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Lets start with creating simple java class &lt;span style=&quot;color: blue;&quot;&gt;JmeterTestClient.java &lt;/span&gt;inside jmeter.sampler either by implementing&amp;nbsp;JavaSamplerClient(org.apache.jmeter.protocol.java.sampler.JavaSamplerClient) or extending&amp;nbsp;&lt;code style=&quot;background-attachment: initial; background-clip: initial; background-color: #eeeeee; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; font-size: 14px; line-height: 18px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 5px; padding-right: 5px; padding-top: 1px; vertical-align: baseline;&quot;&gt;AbstractJavaSamplerClient(org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient&lt;/code&gt;&lt;code style=&quot;background-attachment: initial; background-clip: initial; background-color: #eeeeee; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 14px; line-height: 18px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 5px; padding-right: 5px; padding-top: 1px; vertical-align: baseline;&quot;&gt;&lt;span style=&quot;font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;background-attachment: initial; background-clip: initial; background-color: #eeeeee; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 14px; line-height: 18px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 5px; padding-right: 5px; padding-top: 1px; vertical-align: baseline;&quot;&gt;&lt;span style=&quot;font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;
&lt;div&gt;
&lt;code style=&quot;background-attachment: initial; background-clip: initial; background-color: #eeeeee; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; line-height: 18px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 5px; padding-right: 5px; padding-top: 1px; vertical-align: baseline;&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;you need&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;span style=&quot;font-family: inherit; line-height: 18px;&quot;&gt;ApacheJMeter_java.jar&amp;nbsp;ApacheJMeter_core.jar for compilation only. You can find both jars at &amp;lt;&amp;lt;Installation_dir&amp;gt;&amp;gt;\jakarta-jmeter-2.4\lib\ext&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 14px; line-height: 18px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;text-align: -webkit-auto;&quot;&gt;When possible, Java tests should extend&amp;nbsp;&lt;/span&gt;&lt;code&gt;AbstractJavaSamplerClient&lt;/code&gt;&lt;span style=&quot;text-align: -webkit-auto;&quot;&gt;&amp;nbsp;rather than implementing JavaSamplerClient directly. This should protect your tests from future changes to the interface. While it may be necessary to make changes to the JavaSamplerClient interface from time to time (therefore requiring changes to any implementations of this interface), we intend to make this abstract class provide reasonable default implementations of any new methods so that subclasses do not necessarily need to be updated for new versions. Implementing JavaSamplerClient directly will continue to be supported for cases where extending this class is not possible (for example, when the client class is already a subclass of some other class).&lt;/span&gt; &lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;text-align: -webkit-auto;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
This interface defines the interactions between the JavaSampler and external Java programs which can be executed by JMeter. Any Java class which wants to be executed as a JMeter test must implement this interface (either directly or indirectly through AbstractJavaSamplerClient).&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
JMeter will create one instance of a JavaSamplerClient implementation for each user/thread in the test. Additional instances may be created for internal use by JMeter (for example, to find out what parameters are supported by the client).&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
When the test is started, setupTest() will be called on each thread&#39;s JavaSamplerClient instance to initialize the client. Then runTest() will be called for each iteration of the test. Finally, teardownTest() will be called to allow the client to do any necessary clean-up&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
Compile the above java file and export as jar and put inside&amp;nbsp;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 18px; text-align: left;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;line-height: 18px; text-align: left;&quot;&gt;Installation_dir&amp;gt;&amp;gt;\jakarta-jmeter-2.4\lib\ext &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif; line-height: 18px;&quot;&gt;start jmeter , select testplan creat new thread group and select thread group add java request sampler . select JmeterTestClient &amp;nbsp;from classname drop down . save your test plan and run.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 18px;&quot;&gt;in my example i printed some text are you can see in console ,&lt;/span&gt;&lt;span style=&quot;line-height: 18px;&quot;&gt;You can set loop count in thread group.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 18px;&quot;&gt;The sample code is.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 18px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;package jmeter.sampler;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;import org.apache.jmeter.config.Arguments;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;import org.apache.jmeter.samplers.SampleResult;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;public class JmeterTestClient implements JavaSamplerClient {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int count;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;@Override&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public Arguments getDefaultParameters() {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;count = 10;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;System.out.println(&quot;inside default&quot;);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;return null;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;@Override&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public SampleResult runTest(JavaSamplerContext arg0) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;System.out.println(&quot;Inside Run test:&quot; + count++);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;return null;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;@Override&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public void setupTest(JavaSamplerContext arg0) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;count++;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;System.out.println(&quot;inside Setup&quot;);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;@Override&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public void teardownTest(JavaSamplerContext arg0) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;System.out.println(&quot;inside tear Down:&quot; + count);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; line-height: 18px;&quot;&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: x-small;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: Arial, Helvetica, sans-serif; line-height: 18px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/1683885191626646963/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/jmeter-sampler-client.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/1683885191626646963'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/1683885191626646963'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/jmeter-sampler-client.html' title='Jmeter Sampler Client'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-2616947111230076607</id><published>2012-04-26T21:55:00.000-07:00</published><updated>2012-04-26T22:29:25.316-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="TechNology"/><title type='text'>Understanding how EJB calls operate in WebSphere Application Server V6.1</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;strong style=&quot; font-family: arial, sans-serif; font-size: 12px;&quot;&gt;Summary:&lt;/strong&gt;&lt;span style=&quot; font-family: arial, sans-serif; font-size: 12px;&quot;&gt;&amp;nbsp; Many applications use Enterprise JavaBeans™ (EJB) deployed in an IBM® WebSphere ® Application Server EJB container. Communication to these EJB components requires the use of concepts like Internet Inter-ORB Protocol (IIOP) communication, JNDI look-ups, and workload management. This interaction can be complex and is often a black box for many WebSphere users. This article provides a basic understanding of how EJB communication works in the context of WebSphere Application Server. Communication into and out of the ORB is addressed, along with the definitions of basic call patterns and components involved in making calls to EJB components deployed in the EJB container. A basic understanding of J2EE™ principles and general administrative knowledge of WebSphere Application Server is assumed.&amp;nbsp;&lt;/span&gt; &lt;br /&gt;
&lt;span style=&quot; font-family: arial, sans-serif; font-size: 12px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;Introduction&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;For many WebSphere users, EJB communications are a black box. That is, we understand that calling method &#39;X&#39; will return the desired result from the EJB business logic. Under normal operating conditions, this knowledge may be sufficient as it is not usually necessary to understand the inner workings of the ORB. However, when performance problems or load balancing issues arise, in-depth knowledge of the ORB can greatly enhance the the problem determination effort and reduce time to resolution. As with any WebSphere component, understanding the mechanism of operation for the ORB and its related communication protocols can yield further expertise when troubleshooting it.&lt;/div&gt;&lt;div class=&quot;ibm-alternate-rule&quot; style=&quot;background-attachment: initial; background-clip: initial;  background-image: url(http://1.www.s81c.com/i/solid.gif); background-origin: initial; background-repeat: repeat no-repeat; clear: both; height: 1px;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;ibm-ind-link ibm-back-to-top&quot; style=&quot; clear: both; font-family: arial, sans-serif; font-size: 0.76em; height: 15px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: right;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;The ORB&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;The ORB is the central part of CORBA, or Common Object Request Broker Architecture, which aims to make remote objects usable like local objects in object-oriented, distributed applications. In addition, ORB lets these objects communicate with each other regardless of the specific platforms, techniques, and languages that are used to implement those objects.&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;CORBA is an open, vendor-neutral specification for distributed computing created by the Object Management Group (OMG), a consortium of over 600 software vendors and other companies promoting open, standard interfaces. CORBA and its Interface Definition Language (IDL) provides a common format for representing an object that can be distributed to applications written in Java™, C, C++, and other languages and running on different operating systems and machines.&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;The ORB (Object Request Broker) plays an integral part in the application server runtime. The ORB is implemented as part of the Java Virtual Machine (JVM). IBM WebSphere Application Server includes additional code (generally referred to as the extension ORB) that manages the ORB instance, and also provides the plug-in framework for object services (such as security). This IBM ORB implementation is not the same as the Sun™ ORB; the code base is different altogether. It is the foundation on which the EJB container depends to broker communication for Java Remote Method Invocation (RMI) interactions. RMI is Java’s traditional method of performing remote communications and uses a non-standardized protocol, named Java Remote Method Protocol (JRMP), to communicate between Java objects. At the most basic level, RMI is just Java’s object-oriented version of Remote Procedure Calls (RPCs). WebSphere Application Server uses the ORB to facilitate client/server RMI communication, as well as to communicate between components.&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;The ORB manages inbound and outbound requests for remote Java objects, like EJB components. ORB provides a framework for clients to locate EJBs on the server and invoke operations on those as if they were local. (This concept is referred to as local-remote transparency.) The relationship of the ORB with the EJB container can be described with Figure 1.&lt;/div&gt;&lt;br  /&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;Figure 1. ORB and the EJB container&lt;/b&gt;&lt;br /&gt;&lt;img alt=&quot;Figure 1.  ORB and the EJB container&quot; height=&quot;276&quot; src=&quot;http://www.ibm.com/developerworks/websphere/techjournal/0807_pape/images/image001.gif&quot; style=&quot; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot; width=&quot;466&quot; /&gt;&lt;span &gt;&amp;nbsp;&lt;/span&gt;&lt;br  /&gt;&lt;div class=&quot;ibm-alternate-rule&quot; style=&quot;background-attachment: initial; background-clip: initial;  background-image: url(http://1.www.s81c.com/i/solid.gif); background-origin: initial; background-repeat: repeat no-repeat; clear: both; height: 1px;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;ibm-ind-link ibm-back-to-top&quot; style=&quot; clear: both; font-family: arial, sans-serif; font-size: 0.76em; height: 15px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: right;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;Inter-ORB communication&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;Communication between ORBs is accomplished via the IIOP, which is a concrete implementation of the General Inter-Orb Protocol (GIOP) abstract reference specification. The Interoperable Object Reference (IOR) is simply a string representation of the CORBA or RMI-IIOP object reference in a format that ORBs can understand. You can think of this as a normal Java object reference used in everyday coding. IIOP maps GIOP to the TCP/IP communication stack.&lt;/div&gt;&lt;div style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;The GIOP protocol specifies several different types of messages, such as request messages, reply messages, locate request messages, locate reply messages, and fragment messages. The request and reply message types are self-explanatory, as they deal with the request and response interaction between client and server applications. The locate request and fragment message types might not be as familiar to you unless you have needed to read through GIOP tracing to solve a problem:&lt;/div&gt;&lt;ul style=&quot; font-size: 0.76em; margin-bottom: 0px; margin-top: 0px; padding-bottom: 5px; padding-right: 5px; padding-top: 0px;&quot;&gt;&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;The locate request message type is used to verify that the ORB understands and houses the requested remote object. If the ORB does not know the object requested in the locate request message, it will attempt to provide details (for example, an OBJECT_FORWARD which points to a server that does house the object) on where the client can find the requested object.&lt;/li&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;The fragment message is a continuation of the previous message delivered to the ORB.&lt;/li&gt;
&lt;/ul&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;Processing large GIOP messages can cause problems for the ORB. GIOP messages can be fragmented into manageable pieces for easier and faster processing. Custom properties can be set in WebSphere Application Server that specify the exact size in which to fragment a message (or whether or not to fragment messages at all). By default, WebSphere Application Server will fragment GIOP messages at 1024 bytes.&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;In the most basic of interaction scenarios, the client and the server ORBs communicate directly with each other to satisfy application requests for remote resources. However, in more complex interactions, such as clustered servers with workload management features, processes other than the client and server can become involved (for the purpose of this article) in the NodeAgent process.&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;Each application server contains an ORB that listens for incoming requests. The ORB has three or more ports that are associated with it: the ORB listener port, the bootstrap port, and the security ports. The ORB listener port is the actual port that the ORB listens on for incoming IIOP requests if ORB security is not enabled. The bootstrap port is always an active, well-advertised port, and always returns an indirect IOR containing the secured and unsecured listener ports. A well-constructed client should always bootstrap to the server in which the remote resource resides (or to a multiple host provider URL that points to the cluster if the resource resides in a cluster). There are three other ports associated with the ORB and security: the CSIv2 Client Authentication SSL port, the CSIv2 SSL port, and the SAS SSL port. (Details of these ports is beyond the scope of this article and will not be covered here.) ORB listener ports are, by default, dynamically determined from the platform’s ephemeral port range at run time. However, these ports can be set to a specific static port for various reasons, such as firewall rules, business policies, and so on.&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;When an ORB client requests a reference to a remote object, the remote ORB will reply with an Interoperable Object Reference (IOR), which is simply a string representation of the CORBA or RMI-IIOP object reference in a format that ORBs can understand, and at the very basic level contains host and port information for the server on which the object can be found. The IOR can be thought of as a typical Java object reference used in everyday coding.&lt;/div&gt;&lt;div style=&quot; font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: arial, sans-serif; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em;&quot;&gt;&lt;b&gt;How is the NodeAgent involved?&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;First, the NodeAgent process is only involved in an ORB transaction in the WebSphere Application Server Network Deployment product; it is not present in the base application server offering. The NodeAgent process houses a service called the location service daemon, which enables application servers to register their services and remote objects by providing the daemon with direct IORs pointing to the said application server. Later, when a locate request comes into the location service daemon, it uses the registration information to provide a direct IOR back to a client that points to the registered application server’s ORB listener ports. This registration/IOR information could be thought of as a sort of routing table that is used by the daemon to locate EJBs and other resources found on the various application servers managed by a particular NodeAgent.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Before a managed application server can start properly, a NodeAgent must be available locally on the node. It is possible to configure the application servers such that the dependency on the NodeAgent process (actually the location service daemon service) is gone. However, this setup involves carrying out static routing for IIOP requests, and thus WebSphere Application Server’s workload management services would be nullified. This static routing setup should be avoided unless truly required. (Consult IBM Support before making this assessment on your own.)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;In the event that the NodeAgent process dies or must be taken down or restarted during the operation of an application server on the same node with EJBs deployed on it, exceptions might be thrown in the server logs once the NodeAgent is unavailable. These messages are due to the fact that, once the NodeAgent is removed from the process, the IIOP communication is altered in a way that prevents both ORBs (client and server) from communicating correctly. However, as long as clients already have direct IORs to their desired servers, have already created their EJBHome object, and are making remote calls to that EJB, the NodeAgent is unlikely to be involved in the IIOP communication after that point.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Ideally, incoming locate requests to the location service daemon for EJB-related objects will not come in until all the application servers on a particular node have completed their registration with the location service daemon on the NodeAgent. When locate requests come in for objects on servers that have not yet registered or are temporarily down (and hence no longer registered), a CORBA.OBJECT_NOT_EXIST exception will be returned to the client. In this case, the status of the application server needs to be checked to ensure they are all up and running and have registered with the location service daemon.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;ibm-alternate-rule&quot; style=&quot;background-attachment: initial; background-clip: initial; background-origin: initial; clear: both; font-family: &#39;Times New Roman&#39;; font-size: medium; height: 1px;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;ibm-ind-link ibm-back-to-top&quot; style=&quot;clear: both; font-size: 0.76em; height: 15px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: right;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;Understanding the ORB call pattern&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;In order for high level application functionality (like EJB components) to work, inter-ORB communication must occur. There are two basic scenarios that we can observe:&lt;/div&gt;&lt;ul style=&quot;font-family: &#39;Times New Roman&#39;; font-size: 0.76em; margin-bottom: 0px; margin-top: 0px; padding-bottom: 5px; padding-right: 5px; padding-top: 0px;&quot;&gt;&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;A standalone application server (WebSphere Application Server, not Network Deployment)&lt;/li&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;A clustered application server in a WebSphere Application Server Network Deployment environment.&lt;/li&gt;
&lt;/ul&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;The next sections describe these scenarios.&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span class=&quot;smalltitle&quot; style=&quot;font-size: 1.2em; font-weight: bold;&quot;&gt;WebSphere Application Server base instance&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;In this scenario, the EJB component and its client are contained in the separate application servers. The client code to make the call might look like this:&lt;/div&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;N10100&quot; style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;Listing 1&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style=&quot;color: black; font-family: &#39;Times New Roman&#39;; font-size: 0.76em; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;code-outline&quot; style=&quot;background-color: rgb(247, 247, 247) !important; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.75em; padding-bottom: 5px; padding-left: 2px; padding-right: 2px; padding-top: 2px;&quot;&gt;&lt;pre class=&quot;displaycode&quot; style=&quot;background-attachment: initial !important; background-clip: initial !important; background-image: initial !important; background-origin: initial !important; font-family: &#39;Andale Mono&#39;, &#39;Lucida Console&#39;, Monaco, Liberation, fixed, monospace; font-size: 11px; overflow-x: auto; overflow-y: auto; width: 694px;&quot;&gt;Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,&quot;com.ibm.websphere.naming
.WsnInitialContextFactory&quot;);
env.put(Context.PROVIDER_URL,&quot;corbaloc::isador:2810&quot;);
&lt;strong&gt;Context ctx = new InitialContext(env);
TestEJBHome home = (TestEJBHome)
PortableRemoteObject.narrow(ctx.lookup(&quot;ejb/ejbs/TestEJBHome&quot;),
 TestEJBHome.class);
TestEJB bean = home.create();&lt;/strong&gt;
            &lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;The code in bold text are the lines that result in remote ORB calls. When put into motion, you can follow the events in Figure 2, where the events are listed, in sequence, from top to bottom.&lt;/div&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;Figure 2. ORB event calls in base instance&lt;/b&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;span style=&quot;border-color: initial; border-image: initial; border-style: initial; padding-bottom: 0.7em; padding-top: 0.3em;&quot;&gt;&lt;img alt=&quot;Figure 2. ORB event calls in base instance&quot; height=&quot;413&quot; src=&quot;http://www.ibm.com/developerworks/websphere/techjournal/0807_pape/images/image002.gif&quot; style=&quot;border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: &#39;Times New Roman&#39;; font-size: medium; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot; width=&quot;572&quot; /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;Figure 2 is divided into three parts, each part corresponding to a portion of the Java code in Listing 1:&lt;/div&gt;&lt;ul style=&quot;font-family: &#39;Times New Roman&#39;; font-size: 0.76em; margin-bottom: 0px; margin-top: 0px; padding-bottom: 5px; padding-right: 5px; padding-top: 0px;&quot;&gt;&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;The top section contains the events that result from the InitialContext creation on the client.&lt;/li&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;The middle section contains the events that result from the InitialContext.lookup() method.&lt;/li&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;The bottom section contains the events that result from the creation of the EJB instance.&lt;/li&gt;
&lt;/ul&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;From this diagram, you can see that the process of obtaining an initial context from the server involves sending a locate request message to the ORB (client and server ORB are the same here) on the ORB’s bootstrap port. The resulting reply message contains a direct IOR (with host and TCP/IP and SSL port information) that points the client to the server containing the requested object. Next, the client issues another locate request message, but this time to the ORB’s listener port. After this, the client requests some information from the ORB to establish an initial context.&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;After the InitialContext value has been obtained, the client code then does a lookup in the JNDI namespace to determine how to get a reference to the desired remote object (WLMTestEJB). To accomplish this, the client issues yet another locate request aimed at the ORB’s listener port. The ORB replies back to the client via a locate reply message stating that the object is indeed contained locally. In response to the locate reply, the client then requests that the ORB resolve the object and return a reference to the client. Under the covers, this entails the ORB using a tie class to connect the real Java object with the request that came to the ORB. When the client has the reference, it uses a “narrow” call to marshal the generic object returned from the ORB into the desired object type.&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;The last thing the code does is create an instance of the remote EJB. To complete the process, the client issues a locate request to the remote ORB, to which the ORB replies with a locate reply stating that the object is present. Finally, the client then invokes the create() method on the remote object. When the process is complete in the ORB, the ORB marshals the response back to the client. All data that goes into or leaves the ORB (like method parameters and method return values) are serialized and deserialized using a format called common data representation (CDR). As such, all values passed into the ORB must implement the serializable interface.&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;The locate request messages used in this base application server scenario are simple locate requests that are processed by the ORB itself. This is different from the next scenario because the location service daemon plug-in (attached to the ORB instance) handles messages responding to the locate requests.&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span class=&quot;smalltitle&quot; style=&quot;font-size: 1.2em; font-weight: bold;&quot;&gt;WebSphere Application Server Network Deployment clustered instance&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;In this scenario, the major difference is that the client ORB and server ORB will almost always be two different JVM instances, often located on completely separate hosts. The process will appear much the same, but you will notice the involvement of the location service daemon server contained in the NodeAgent. Listing 2 shows the code involved to make the call.&lt;/div&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;N10141&quot; style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;Listing 2&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style=&quot;color: black; font-family: &#39;Times New Roman&#39;; font-size: 0.76em; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;code-outline&quot; style=&quot;background-color: rgb(247, 247, 247) !important; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: arial, sans-serif; font-size: 0.75em; padding-bottom: 5px; padding-left: 2px; padding-right: 2px; padding-top: 2px;&quot;&gt;&lt;pre class=&quot;displaycode&quot; style=&quot;background-attachment: initial !important; background-clip: initial !important; background-image: initial !important; background-origin: initial !important; font-family: &#39;Andale Mono&#39;, &#39;Lucida Console&#39;, Monaco, Liberation, fixed, monospace; font-size: 11px; overflow-x: auto; overflow-y: auto; width: 694px;&quot;&gt;Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,&quot;com.ibm.websphere.naming
.WsnInitialContextFactory&quot;);
env.put(Context.PROVIDER_URL,&quot;corbaloc::boris:9811,:natasha
:9812&quot;);
&lt;strong&gt;Context ctx = new InitialContext(env);
TestEJBHome home = (TestEJBHome)
PortableRemoteObject.narrow(ctx.lookup(&quot;ejb/ejbs/TestEJBHome&quot;),
 TestEJBHome.class);
TestEJB bean = home.create();&lt;/strong&gt;
            &lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;The code only changed from the previous example in only one place: the values placed in the PROVIDER_URL key of the environment Hashtable object. In this case, the developer included multiple corbaloc URLs, each of which points to a separate server’s bootstrap port. This change introduces some fault tolerance and failover to the process as the code now has two paths from which the InitialContext object can be obtained. As in the previous scenario, the code in bold text is responsible for generating the remote ORB requests. Figure 3 illustrates the sequence of events for this scenario, formatted the same as the previous example.&lt;/div&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;a href=&quot;&quot; name=&quot;N1014F&quot; style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot;&gt;&lt;b style=&quot;font-family: arial, sans-serif; font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;Figure 3. ORB event calls in clustered instance&lt;/b&gt;&lt;/a&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;span style=&quot;border-color: initial; border-image: initial; border-style: initial; padding-bottom: 0.7em; padding-top: 0.3em;&quot;&gt;&lt;img alt=&quot;Figure 3. ORB event calls in clustered instance&quot; height=&quot;374&quot; src=&quot;http://www.ibm.com/developerworks/websphere/techjournal/0807_pape/images/image003.gif&quot; style=&quot;border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: &#39;Times New Roman&#39;; font-size: medium; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot; width=&quot;572&quot; /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;; font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br style=&quot;font-family: &#39;Times New Roman&#39;; font-size: medium;&quot; /&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;For the creation of the InitialContext object in the top section, there are no obvious differences between this scenario and the first one. However, with the addition of WebSphere Application Server workload management in the picture, there are slight differences.&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;First, during the creation of the InitialContext (top section), a list of IORs that point to each of the cell’s location service daemon clusters are prepared and returned to the client ORB during the getProperties() portion of the conversation. When the client ORB makes further calls to the server ORB, the workload management plug-in on the client side iterates over the list and issues a resolve_complete_info request. This first request is routed to the location service daemon service in the NodeAgent (shown as the first call in the middle section), where the desired cluster data is sent back to the client in the form of a direct IOR pointing to the destination server.&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;In the bottom section of the diagram, you can see that the locate request message issued in the first scenario is not needed and the create() method is invoked instead, immediately without having to process the additional locate request because workload management has already “located” an appropriate application server.&lt;/div&gt;&lt;div class=&quot;ibm-alternate-rule&quot; style=&quot;background-attachment: initial; background-clip: initial; background-origin: initial; clear: both; font-family: &#39;Times New Roman&#39;; font-size: medium; height: 1px;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;ibm-ind-link ibm-back-to-top&quot; style=&quot;clear: both; font-size: 0.76em; height: 15px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: right;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;Summary&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;This article defined the ORB and how it provides the application server runtime with vital functionality needed to service clients making remote requests to an EJB server. The high level sequence of events that make up inter-ORB communications was also explained, along with the role that the NodeAgent plays in the part of ORB communication between clients and servers.&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;There is much more information available on these topics. If you wish to continue your reading, you might want to look into these subjects:&lt;/div&gt;&lt;ul style=&quot;font-family: &#39;Times New Roman&#39;; font-size: 0.76em; margin-bottom: 0px; margin-top: 0px; padding-bottom: 5px; padding-right: 5px; padding-top: 0px;&quot;&gt;&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;GIOP protocol&lt;/li&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;Problem determination for the ORB/EJB container&lt;/li&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;RMI over IIOP&lt;/li&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;Java Naming and Directory Interface (JNDI)&lt;/li&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;EJB clustering&lt;/li&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;WebSphere workload management (WLM)&lt;/li&gt;
&lt;li style=&quot;font-family: arial, sans-serif; margin-bottom: 0px; margin-top: 0px; padding-bottom: 3px; padding-right: 5px; padding-top: 0px;&quot;&gt;CORBA and the OMG.&lt;/li&gt;
&lt;/ul&gt;&lt;div class=&quot;ibm-alternate-rule&quot; style=&quot;background-attachment: initial; background-clip: initial; background-origin: initial; clear: both; font-family: &#39;Times New Roman&#39;; font-size: medium; height: 1px;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;ibm-ind-link ibm-back-to-top&quot; style=&quot;clear: both; font-size: 0.76em; height: 15px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: right;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;&lt;span class=&quot;atitle&quot; style=&quot;font-size: 1.5em; font-weight: bold;&quot;&gt;Acknowledgements&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-size: 0.76em; padding-bottom: 0.7em; padding-left: 5px; padding-right: 5px; padding-top: 0.3em;&quot;&gt;The authors extend their thanks to Paul Bullis and Claudia Barrett for providing guidance and clarification on the topics discussed in this article.&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;ibm-alternate-rule&quot; style=&quot;background-attachment: initial; background-clip: initial;  background-image: url(http://1.www.s81c.com/i/solid.gif); background-origin: initial; background-repeat: repeat no-repeat; clear: both; height: 1px;&quot;&gt;&lt;/div&gt;&lt;br class=&quot;Apple-interchange-newline&quot; /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/2616947111230076607/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/understanding-how-ejb-calls-operate-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/2616947111230076607'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/2616947111230076607'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/understanding-how-ejb-calls-operate-in.html' title='Understanding how EJB calls operate in WebSphere Application Server V6.1'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-7283547471271059211</id><published>2012-04-18T02:52:00.002-07:00</published><updated>2012-04-18T02:53:10.767-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="FTP"/><category scheme="http://www.blogger.com/atom/ns#" term="linux"/><category scheme="http://www.blogger.com/atom/ns#" term="TechNology"/><title type='text'>FTP Configuration on a Firewall</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
This page gives a detailed description on what TCP activity goes on during a FTP session. If you are looking for answers to connection problems caused by routers and firewalls, you should look&amp;nbsp;here.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
Whenever an FTP client creates a connection to an FTP server at port 21, and starts to request data, the server will create a data link back from it&#39;s own port 20 to the port specified in the original connection by the client through a PORT command (non-passive connection, the most usual).&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
However, the client can instead of sending a PORT command, send a PASV command, which means the server is to send a message back to the client with a listening port number, so the client can itself create the data link (passive connection).&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
This gives in all 4 different combinations to take into account when configuring a port oriented firewall:&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
1) FTP server doing non-passive connection.&lt;br /&gt;2) FTP server doing passive connection.&lt;br /&gt;3) FTP client doing non-passive connection.&lt;br /&gt;4) FTP client doing passive connection.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
The easy way is to simply allow FTP servers and clients unlimited access through your firewall, but if you like to limit their access to &quot;known&quot; ports, you have to understand the 4 different scenarios.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
1) The FTP server should be allowed to accept TCP connections to port 21, and to make TCP connections from port 20 to any (remote ephemeral) port.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
2) The FTP server should be allowed to accept TCP connections to port 21, AND to accept TCP connections to any ephemeral port as well!&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
3) The FTP client should be allowed to make TCP connections to port 21, and to accept TCP connections from port 20 to any ephemeral port.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
4) The FTP client should be allowed to make TCP connections to port 21, and to make TCP connections to any other (remote ephemeral) port as well!&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
Schematically:&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
Non Passive&lt;br /&gt;&lt;img src=&quot;http://www.mdjnet.dk/img/nonpasv.gif&quot; title=&quot;Non Passive&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
Passive&lt;br /&gt;&lt;img src=&quot;http://www.mdjnet.dk/img/pasv.gif&quot; title=&quot;Passive&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
An ephemeral (or dynamic) port is a temporary, short lived port, assigned to the application by the IP stack, taken from a specified pool.&amp;nbsp;Mike Gleason&amp;nbsp;has a wonderful page describing this principle in relation to FTP and firewalls as well. The range of ports used as ephemeral ports is both rather large, and can vary from system to system, both adding to the complexity of configuring a firewall correctly. Whenever an application makes a TCP connection, it is (usually) assigned a port by the IP stack as a &quot;return address&quot;, meaning a TCP connection can have any port as its origin, and that is not a problem for firewalls at all, as they mostly check the destination port to figure out what to do. When FTP is involved, ephemeral ports are used as destination ports too, and that complicates the matter drastically.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;/div&gt;
&lt;h2 style=&quot;text-align: -webkit-auto;&quot;&gt;
Server Side&lt;/h2&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
If you have an FTP server, you should configure both case 1) and 2). 1) is easy enough, it is case 2) that needs consideration. It should be obvious that you trust your FTP server not to do any hanky panky, so allowing it to accept TCP connections to any ephemeral port should NOT be a problem. (In windows, the ephemeral port range is by default 1024-5000, but the upper range can be changed.)&lt;br /&gt;Sometimes, the FTP server can itself be assigned a range to use for PASV connections, as is the case with&amp;nbsp;&lt;a href=&quot;http://www.cerberusftp.com/&quot;&gt;Cerberus&lt;/a&gt;&amp;nbsp;that I am using. This makes it possible to define a range of ports that are not too difficult to configure in the firewall without any problems. When configuring this range, you must be carefull not to choose too narrow a range. First of all, several ports can be used by the same connection, second, you should be able to handle several clients at the same time, and third, an ephemeral port has a rather long latency period (minutes), before Windows allows that port to be reused, so you can very quickly run out of ports! Make sure you select a port range of at least several hundred ports. If you are having problems downloading and uploading to the server, but not connecting, this range is the first thing to check, if you are sure your firewall is configured correctly.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
If your server is behind a&amp;nbsp;&lt;a href=&quot;http://www.mdjnet.dk/router.html&quot;&gt;router&lt;/a&gt;, you will have to configure the router as well. Note that routing port 21 is not enough, the chosen range of ports for PASV connections must also be routed correctly. Of course, all outgoing connections must be possible as well.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;/div&gt;
&lt;h2 style=&quot;text-align: -webkit-auto;&quot;&gt;
Client Side&lt;/h2&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
You probably also want to be able to act as an FTP client. This time, both case 3) and 4) pose problems: In case 3), you want to allow any incoming connection to your FTP client from port 20. This sounds acceptable, but in some cases could be very dangerous, as we shall see in a moment. In case 4), the FTP client should be allowed to do connections to any port (returned from an FTP server), and this also sounds acceptable, but again, it may not be, as we shall see.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
There are 3 basic cases, that need to be configured.&lt;br /&gt;a) You use a simple FTP client program.&lt;br /&gt;b) You use a browser like Internet Explorer as FTP client.&lt;br /&gt;c) You connect through a proxy server running&amp;nbsp;&lt;a href=&quot;http://www.proxyplus.cz/&quot;&gt;ProxyPlus&lt;/a&gt;, for instance, like I am myself.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
In case a), it is not a problem to allow it to accept connections on any port from port 20. There is of course a risc of a hacking attempt from port 20, but it requires a very lucky punch to hit your port while it is active, and even more luck to actually fool the FTP program, so I consider that risc remote indeed. But you also need to allow the client to make TCP connections to any other port. Again, we must assume you trust your FTP client, so this should not be a problem either.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
Case b), and the trouble begins... Again, the chance of someone finding an open port waiting for connections from port 20 is remote (case 3), but do you trust the Internet Explorer or any other browser to allow it to connect to just any port (case 4)? Especially in these days, when browsers are running ActiveX components and scripts of all kinds. You might argue, that any malicious script or component would of course choose to report back on port 80, as this port is of course always open for the browser to connect to. Still, I don&#39;t like the idea, so think about it before allowing your browser to do passive connection. (In Internet Explorer 6&amp;nbsp;&lt;i&gt;SP1&lt;/i&gt;, this can be configured in the Options dialog, Advanced, Browsing.) Lucky for me, I am using ProxyPlus as a proxy, so my browser cannot connect to just any machine, and it is not a big problem with Kerio and Tiny personal firewall to configure it to ask for accept on new connection types. That way I can accept the connection explicitly, whenever I need to do FTP, which is not often, without giving the firewall any definite rule.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
Case c) is the really difficult one! The proxy server need to be acting as an FTP server to the clients, but that is not a big problem as clients have known addresses, so these addresses can just be given the required permissions. But on the outside, the job is not all that simple.&lt;br /&gt;Case 3) requires it to accept connections on any port from port 20, but as my proxy server has a lot of other ports open for other cases, the chance of a hacking attempt against it from a port 20 is much higher, opposed to the case of the client, which only had &quot;stupid&quot; ports open, waiting for connections from port 20. The ProxyPlus application is also offering a LOT of other services that could be abused. Of course, I have configured my proxy to only perform services when requested from &quot;secure&quot; clients, but I don&#39;t like the idea of just opening all ports! Also, only the ephemeral port range is relevant, but that range does indeed include ports actually used for some of the proxy server&#39;s actual services.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
One way to do it is to create rules DISALLOWING connections from the outside to these sensitive ports BEFORE the rule allowing connections to any ephemeral port. Another way is to reconfigure the proxy server&#39;s service ports to fall outside the ephemeral port range, so the dangerous rule allowing connections does not interfere with the real service ports.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
If you are behind a&amp;nbsp;&lt;a href=&quot;http://www.mdjnet.dk/router.html&quot;&gt;router&lt;/a&gt;, you can choose to route the entire ephemeral port range back to your client machine, or you can choose to do PASV FTP connections, which does not require any odd incoming ports open, but only outgoing connections.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
I hope this discussion has given you enough information to decide for yourself how to do it.&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/7283547471271059211/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/ftp-configuration-on-firewall.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7283547471271059211'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7283547471271059211'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/ftp-configuration-on-firewall.html' title='FTP Configuration on a Firewall'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-5936359718544737051</id><published>2012-04-18T02:24:00.002-07:00</published><updated>2012-04-29T23:52:28.326-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="linux"/><category scheme="http://www.blogger.com/atom/ns#" term="TechNology"/><title type='text'>Getting started with iptables in Linux</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;Linux machines are known to be pretty secure. Studies have shown that Linux has been designed in a secure manner. Yet, despite all the security features that come bundled with a Linux installation, you need to configure these features correctly to make them work for you. I’ll guide you through the process of setting up of one of the tools that help secure your machine – the firewall. We will use the iptables firewall for this exercise. I am assuming that you are using a server running Red Hat Enterprise Linux 4 or similar. However, most of the steps should work fine on other Linux distributions as well. In this article we will setup a firewall on a Linux server running the Apache Web Server, FTP, and SSH.&lt;span id=&quot;more-1830&quot; style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial;  background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;Let us first see what ports these applications use and which of them need to have a port open on the firewall.&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;The Apache web server runs on port 80 by default. Apache is going to server all our web content on this port, therefore we need to keep this port open on the firewall. The SSH service runs on port 22. We need to be able to remotely connect to our server to work, so we keep it open. FTP runs on port 21 and it too needs the port to be open to communication.&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial;  background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;Next, make sure you have iptables installed. Run this command as the root user:&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;#&amp;nbsp;&lt;strong style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;rpm -qa | grep iptables&lt;/strong&gt;&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial;  background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;If you have iptables installed the system should give you the version of iptables you have installed. In case you don’t you can try something like the following to get it and start it:&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial;  background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;#&amp;nbsp;&lt;strong style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;yum install iptables&lt;/strong&gt;&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial;  background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;#&amp;nbsp;&lt;strong style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;/etc/init.d/iptables start&lt;/strong&gt;&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;To check what kind of configuration iptables is currently running with:&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;#&amp;nbsp;&lt;strong style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;iptables –list&lt;/strong&gt;&lt;br /&gt;
Chain INPUT (policy ACCEPT)&lt;br /&gt;
target prot opt source destination&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;Chain FORWARD (policy ACCEPT)&lt;br /&gt;
target prot opt source destination&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;Chain OUTPUT (policy ACCEPT)&lt;br /&gt;
target prot opt source destination&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial;  background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;This command will list out all the firewall rules that have been set currently. I will proceed with the assumption that you do not have any firewall rules in your iptables configuration. Let’s now configure the firewall to allow open communication on the ports 80 for your web server, 22 for SSH, and port 21 for FTP. We’ll also make sure that we block communication to any port other than specified.&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial;  background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;Here’s a firewall script configuration script. Create a new file and call it&amp;nbsp;&lt;strong style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;iptable-firewall.sh&lt;/strong&gt;. Copy the following text into it:&lt;/div&gt;&lt;blockquote style=&quot;background-attachment: initial; background-clip: initial;  background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-color: rgb(233, 234, 234); border-left-style: solid; border-left-width: 2px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 9px; margin-left: 9px; margin-right: 9px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 10px; padding-right: 0px; padding-top: 0px; quotes: none; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;#!/bin/sh&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;ANY=”0/0″&lt;br /&gt;
OPEN_PORTS=”21 22 80″&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;iptables -P INPUT ACCEPT&lt;br /&gt;
iptables -P FORWARD ACCEPT&lt;br /&gt;
iptables -P OUTPUT ACCEPT&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;# Flush (-F) all specific rules&lt;br /&gt;
iptables -F INPUT&lt;br /&gt;
iptables -F FORWARD&lt;br /&gt;
iptables -F OUTPUT&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;for port in $OPEN_PORTS&lt;br /&gt;
do&lt;br /&gt;
iptables -A INPUT -i eth0 -p tcp -s $ANY -d $ANY –destination-port $port –syn -j ACCEPT&lt;br /&gt;
iptables -A INPUT -i eth1 -p tcp -s $ANY -d $ANY –destination-port $port –syn -j ACCEPT&lt;br /&gt;
done&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;iptables -A INPUT -i eth1 -p icmp -s $ANY -d $ANY -j ACCEPT&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;#Allow any related/established connections&lt;br /&gt;
iptables -A INPUT -i eth0 -m state –state ESTABLISHED,RELATED -j ACCEPT&lt;br /&gt;
iptables -A INPUT -i eth1 -m state –state ESTABLISHED,RELATED -j ACCEPT&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;#Kill everything else&lt;br /&gt;
iptables -A INPUT -i eth0 -j DROP&lt;br /&gt;
iptables -A INPUT -i eth1 -j DROP&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;#write for boot&lt;br /&gt;
iptables-save &amp;gt; /etc/sysconfig/iptables&lt;/div&gt;&lt;/blockquote&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;Now save the above file, grant it executable permissions and then run it:&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial;  background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;#&amp;nbsp;&lt;strong style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;chmod +x iptable-firewall.sh&lt;/strong&gt;&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;#&amp;nbsp;&lt;strong style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;./iptable-firewall.sh&lt;/strong&gt;&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial;  background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;Now check your firewall rules:&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial;background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;#&amp;nbsp;&lt;strong style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;iptables –list&lt;/strong&gt;&lt;/div&gt;&lt;div style=&quot;background-attachment: initial; background-clip: initial;  background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #51555c; font-family: helvetica, arial, sans-serif; font-size: 13px; line-height: 18px; margin-bottom: 18px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;All your firewall rules should now be set. Your server is now secure. To make any modification or additions to this set of rules, edit the line where the&amp;nbsp;&lt;strong style=&quot;background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;OPEN_PORTS&lt;/strong&gt;parameter is defined and add or remove ports form the list. Remember to run the script again after making any changes to it.&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/5936359718544737051/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/getting-started-with-iptables-in-linux.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/5936359718544737051'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/5936359718544737051'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/getting-started-with-iptables-in-linux.html' title='Getting started with iptables in Linux'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-7114261823604941324</id><published>2012-04-18T00:18:00.002-07:00</published><updated>2012-04-18T02:25:30.184-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="linux"/><category scheme="http://www.blogger.com/atom/ns#" term="TechNology"/><title type='text'>Linux Firewalls Using iptables</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;h1&gt;

&lt;/h1&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;

&lt;span class=&quot;mw-headline&quot; id=&quot;Introduction&quot;&gt;Introduction&lt;/span&gt;&lt;/h1&gt;
Network security is a primary consideration in any decision to host a website as the threats are becoming more widespread and persistent every day. One means of providing additional protection is to invest in a firewall. Though prices are always falling, in some cases you may be able to create a comparable unit using the Linux iptables package on an existing server for little or no additional expenditure.&lt;br /&gt;
&lt;br /&gt;
This chapter shows how to convert a Linux server into:&lt;br /&gt;
A firewall while simultaneously being your home website&#39;s mail, web and DNS server.&lt;br /&gt;
A router that will use NAT and port forwarding to both protect your home network and have another web server on your home network while sharing the public IP address of your firewall.&lt;br /&gt;
Creating an iptables firewall script requires many steps, but with the aid of the sample tutorials, you should be able to complete a configuration relatively quickly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;

&lt;span class=&quot;mw-headline&quot; id=&quot;What_Is_iptables.3F&quot;&gt;What Is iptables?&lt;/span&gt;&lt;/h1&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Originally, the most popular firewall/NAT package running on Linux was ipchains, but it had a number of shortcomings. To rectify this, the Netfilter organization decided to create a new product called iptables, giving it such improvements as:&lt;/div&gt;
&lt;ul style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Better integration with the Linux kernel with the capability of loading iptables-specific kernel modules designed for improved speed and reliability.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Stateful packet inspection. This means that the firewall keeps track of each connection passing through it and in certain cases will view the contents of data flows in an attempt to anticipate the next action of certain protocols. This is an important feature in the support of active FTP and DNS, as well as many other network services.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Filtering packets based on a MAC address and the values of the flags in the TCP header. This is helpful in preventing attacks using malformed packets and in restricting access from locally attached servers to other networks in spite of their IP addresses.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;System logging that provides the option of adjusting the level of detail of the reporting.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Better network address translation.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Support for transparent integration with such Web proxy programs as Squid.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;A rate limiting feature that helps iptables block some types of denial of service (DoS) attacks.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Considered a faster and more secure alternative to ipchains, iptables has become the default firewall package installed under RedHat and Fedora Linux.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;/div&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;

&lt;span class=&quot;mw-headline&quot; id=&quot;Download_And_Install_The_Iptables_Package&quot;&gt;Download And Install The Iptables Package&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Before you begin, you need to make sure that the iptables software RPM is installed. (See Chapter 6, &quot;&lt;a href=&quot;http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch06_:_Installing_Linux_Software&quot; style=&quot;background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; color: #5a3696;&quot; title=&quot;Quick HOWTO : Ch06 : Installing Linux Software&quot;&gt;Installing Linux Software&lt;/a&gt;&quot;, if you need a refresher.) When searching for the RPMs, remember that the filename usually starts with the software package name by a version number, as in iptables-1.2.9-1.0.i386.rpm.&lt;/div&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;

&lt;span class=&quot;mw-headline&quot; id=&quot;How_To_Start_iptables&quot;&gt;How To Start iptables&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
You can start, stop, and restart iptables after booting by using the commands:&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;[root@bigboy tmp]# service iptables start
[root@bigboy tmp]# service iptables stop
[root@bigboy tmp]# service iptables restart
&lt;/pre&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
To get iptables configured to start at boot, use the chkconfig command:.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;[root@bigboy tmp]# chkconfig iptables on
&lt;/pre&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;

&lt;span class=&quot;mw-headline&quot; id=&quot;Determining_The_Status_of_iptables&quot;&gt;Determining The Status of iptables&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
You can determine whether iptables is running or not via the service iptables status command. Fedora Core will give a simple status message. For example&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;[root@bigboy tmp]# service iptables status
Firewall is stopped.
[root@bigboy tmp]#
&lt;/pre&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;

&lt;span class=&quot;mw-headline&quot; id=&quot;Packet_Processing_In_iptables&quot;&gt;Packet Processing In iptables&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
All packets inspected by iptables pass through a sequence of built-in tables (queues) for processing. Each of these queues is dedicated to a particular type of packet activity and is controlled by an associated packet transformation/filtering chain.&lt;/div&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
There are three tables in total. The first is the mangle table which is responsible for the alteration of quality of service bits in the TCP header. This is hardly used in a home or SOHO environment.&lt;/div&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The second table is the filter queue which is responsible for packet filtering. It has three built-in chains in which you can place your firewall policy rules. These are the:&lt;/div&gt;
&lt;ul style=&quot;line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Forward chain: Filters packets to servers protected by the firewall.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Input chain: Filters packets destined for the firewall.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Output chain: Filters packets originating from the firewall.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The third table is the nat queue which is responsible for network address translation. It has two built-in chains; these are:&lt;/div&gt;
&lt;ul style=&quot;line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Pre-routing chain: NATs packets when the destination address of the packet needs to be changed.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Post-routing chain: NATs packets when the source address of the packet needs to be changed&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;

&lt;span class=&quot;mw-headline&quot; id=&quot;Table_14-1_Processing_For_Packets_Routed_By_The_Firewall&quot;&gt;Table 14-1 Processing For Packets Routed By The Firewall&lt;/span&gt;&lt;/h2&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; style=&quot;background-color: white; color: black; font-family: sans-serif; font-size: 13px; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Queue Type&lt;/th&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Queue Function&lt;/th&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Packet Transformation Chain in Queue&lt;/th&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Chain Function&lt;/th&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td rowspan=&quot;3&quot;&gt;Filter&lt;/td&gt;&lt;td rowspan=&quot;3&quot;&gt;Packet filtering&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;FORWARD
&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;Filters packets to servers accessible by another NIC on the firewall.&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;INPUT
&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;Filters packets destined to the firewall.&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;OUTPUT
&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;Filters packets originating from the firewall&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td rowspan=&quot;3&quot;&gt;Nat&lt;/td&gt;&lt;td rowspan=&quot;3&quot;&gt;Network Address Translation&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;PREROUTING
&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;Address translation occurs before routing. Facilitates the transformation of the destination IP address to be compatible with the firewall&#39;s routing table. Used with NAT of the destination IP address, also known as&amp;nbsp;&lt;b&gt;destination NAT&lt;/b&gt;&amp;nbsp;or&amp;nbsp;&lt;b&gt;DNAT&lt;/b&gt;.&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;POSTROUTING
&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;Address translation occurs after routing. This implies that there was no need to modify the destination IP address of the packet as in pre-routing. Used with NAT of the source IP address using either one-to-one or many-to-one NAT. This is known as&amp;nbsp;&lt;b&gt;source NAT&lt;/b&gt;, or&amp;nbsp;&lt;b&gt;SNAT&lt;/b&gt;.&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;OUTPUT
&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;Network address translation for packets generated by the firewall. (Rarely used in SOHO environments)&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;Mangle&lt;/td&gt;&lt;td&gt;TCP header modification&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;PREROUTING
POSTROUTING
OUTPUT
INPUT
FORWARD
&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;Modification of the TCP packet quality of service bits before routing occurs. (Rarely used in SOHO environments)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;br /&gt;
You need to specify the table and the chain for each firewall rule you create. There is an exception: Most rules are related to filtering, so iptables assumes that any chain that&#39;s defined without an associated table will be a part of the filter table. The filter table is therefore the default.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
To help make this clearer, take a look at the way packets are handled by iptables. In Figure 14.1 a TCP packet from the Internet arrives at the firewall&#39;s interface on Network A to create a data connection.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The packet is first examined by your rules in the mangle table&#39;s PREROUTING chain, if any. It is then inspected by the rules in the nat table&#39;s PREROUTING chain to see whether the packet requires DNAT. It is then routed.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
If the packet is destined for a protected network, then it is filtered by the rules in the FORWARD chain of the filter table and, if necessary, the packet undergoes SNAT in the POSTROUTING chain before arriving at Network B. When the destination server decides to reply, the packet undergoes the same sequence of steps. Both the FORWARD and POSTROUTING chains may be configured to implement quality of service (QoS) features in their mangle tables, but this is not usually done in SOHO environments.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
If the packet is destined for the firewall itself, then it passes through the mangle table of the INPUT chain, if configured, before being filtered by the rules in the INPUT chain of the filter table before. If it successfully passes these tests then it is processed by the intended application on the firewall.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
At some point, the firewall needs to reply. This reply is routed and inspected by the rules in the OUTPUT chain of the mangle table, if any. Next, the rules in the OUTPUT chain of the nat table determine whether DNAT is required and the rules in the OUTPUT chain of the filter table are then inspected to help restrict unauthorized packets. Finally, before the packet is sent back to the Internet, SNAT and QoS mangling is done by the POSTROUTING chain&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;/div&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;

&lt;span class=&quot;mw-headline&quot; id=&quot;Figure_14-1_Iptables_Packet_Flow_Diagram&quot;&gt;Figure 14-1 Iptables Packet Flow Diagram&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;a class=&quot;image&quot; href=&quot;http://www.linuxhomenetworking.com/wiki/index.php/File:Iptables.gif&quot; style=&quot;background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; color: #5a3696;&quot;&gt;&lt;img alt=&quot;Iptables.gif&quot; height=&quot;760&quot; src=&quot;http://www.linuxhomenetworking.com/wiki/images/f/f0/Iptables.gif&quot; style=&quot;border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;&quot; width=&quot;543&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
It is now time to discuss the ways in which you add rules to these chains.&lt;br /&gt;
&lt;br /&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Targets_And_Jumps&quot;&gt;Targets And Jumps&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Each firewall rule inspects each IP packet and then tries to identify it as the target of some sort of operation. Once a target is identified, the packet needs to jump over to it for further processing. Table 14.2 lists the built-in targets that iptables uses.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Table_14-2_Descriptions_Of_The_Most_Commonly_Used_Targets&quot;&gt;Table 14-2 Descriptions Of The Most Commonly Used Targets&lt;/span&gt;&lt;/h2&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; style=&quot;background-color: white; color: black; font-family: sans-serif; font-size: 13px; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;target&lt;/th&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Desciption&lt;/th&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Most Common Options&lt;/th&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;ACCEPT&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;ul style=&quot;line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;iptables stops further processing.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;The packet is handed over to the end application or the operating system for processing&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;&lt;td&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
N/A&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;DROP&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;ul style=&quot;line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;iptables stops further processing.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;The packet is blocked&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;&lt;td&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
N/A&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;LOG&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;ul style=&quot;line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;The packet information is sent to the syslog daemon for logging&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;iptables continues processing with the next rule in the table&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;As you can&#39;t log and drop at the same time, it is common to have two similar rules in sequence. The first will log the packet, the second will drop it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;--log-prefix &quot;string&quot;
&lt;/pre&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Tells iptables to prefix all log messages with a user defined string. Frequently used to tell why the logged packet was dropped&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;REJECT&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;ul style=&quot;line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Works like the DROP target, but will also return an error message to the host sending the packet that the packet was blocked&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;--reject-with qualifier
&lt;/pre&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The qualifier tells what type of reject message is returned. Qualifiers include:&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;icmp-port-unreachable (default)
icmp-net-unreachable
icmp-host-unreachable
icmp-proto-unreachable
icmp-net-prohibited
icmp-host-prohibited
tcp-reset
echo-reply
&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;DNAT&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;ul style=&quot;line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Used to do&amp;nbsp;&lt;b&gt;destination network address translation&lt;/b&gt;. ie. rewriting the destination IP address of the packet&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;--to-destination ipaddress
&lt;/pre&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Tells iptables what the destination IP address should be&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;SNAT&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;ul style=&quot;line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Used to do&amp;nbsp;&lt;b&gt;source network address translation&lt;/b&gt;&amp;nbsp;rewriting the source IP address of the packet&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;The source IP address is user defined&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;--to-source &amp;lt;address&amp;gt;[-&amp;lt;address&amp;gt;][:&amp;lt;port&amp;gt;-&amp;lt;port&amp;gt;]
&lt;/pre&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Specifies the source IP address and ports to be used by SNAT.&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;MASQUERADE&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;ul style=&quot;line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Used to do Source Network Address Translation.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;By default the source IP address is the same as that used by the firewall&#39;s interface&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;[--to-ports &amp;lt;port&amp;gt;[-&amp;lt;port&amp;gt;]]
&lt;/pre&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Specifies the range of source ports to which the original source port can be mapped.&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Important_Iptables_Command_Switch_Operations&quot;&gt;Important Iptables Command Switch Operations&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Each line of an iptables script not only has a jump, but they also have a number of command line options that are used to append rules to chains that match your defined packet characteristics, such the source IP address and TCP port. There are also options that can be used to just clear a chain so you can start all over again. Tables 14.2 through 14.6 list the most common options.&lt;/div&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Table_14-2_General_Iptables_Match_Criteria&quot;&gt;Table 14-2 General Iptables Match Criteria&lt;/span&gt;&lt;/h2&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; style=&quot;background-color: white; color: black; font-family: sans-serif; font-size: 13px; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;iptables command Switch&lt;/th&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Desciption&lt;/th&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-t &amp;lt;-table-&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;If you don&#39;t specify a table, then the&amp;nbsp;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;filter&lt;/code&gt;&amp;nbsp;table is assumed. As discussed before, the possible built-in tables include: filter, nat, mangle&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-j &amp;lt;target&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Jump to the specified target chain when the packet matches the current rule.&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-A&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Append rule to end of a chain&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-F&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Flush. Deletes all the rules in the selected table&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-p &amp;lt;protocol-type&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Match protocol. Types include, icmp, tcp, udp, and all&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-s &amp;lt;ip-address&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Match source IP address&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-d &amp;lt;ip-address&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Match destination IP address&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-i &amp;lt;interface-name&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Match &quot;input&quot; interface on which the packet enters.&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-o &amp;lt;interface-name&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Match &quot;output&quot; interface on which the packet exits&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;br /&gt;In this command switches example&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;iptables -A INPUT -s 0/0 -i eth0 -d 192.168.1.1  -p TCP -j ACCEPT
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
iptables is being configured to allow the firewall to accept TCP packets coming in on interface eth0 from any IP address destined for the firewall&#39;s IP address of 192.168.1.1. The 0/0 representation of an IP address means any.&lt;/div&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Table_14-4_Common_TCP_and_UDP_Match_Criteria&quot;&gt;Table 14-4 Common TCP and UDP Match Criteria&lt;/span&gt;&lt;/h2&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; style=&quot;background-color: white; color: black; font-family: sans-serif; font-size: 13px; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Switch&lt;/th&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Desciption&lt;/th&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-p tcp --sport &amp;lt;port&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;TCP source port. Can be a single value or a range in the format:&amp;nbsp;&lt;i&gt;start-port-number:end-port-number&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-p tcp --dport &amp;lt;port&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;TCP destination port. Can be a single value or a range in the format:&amp;nbsp;&lt;i&gt;starting-port:ending-port&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-p tcp --syn&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Used to identify a new TCP connection request.&amp;nbsp;! --syn means, not a new connection request&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-p udp --sport &amp;lt;port&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;UDP source port. Can be a single value or a range in the format:&amp;nbsp;&lt;i&gt;starting-port:ending-port&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-p udp --dport &amp;lt;port&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;UDP destination port. Can be a single value or a range in the format:&amp;nbsp;&lt;i&gt;starting-port:ending-port&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;br /&gt;In this example:&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;iptables -A FORWARD -s 0/0 -i eth0 -d 192.168.1.58 -o eth1 -p TCP \
         --sport 1024:65535 --dport 80 -j ACCEPT
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
iptables is being configured to allow the firewall to accept TCP packets for routing when they enter on interface eth0 from any IP address and are destined for an IP address of 192.168.1.58 that is reachable via interface eth1. The source port is in the range 1024 to 65535 and the destination port is port 80 (www/http).&lt;/div&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Table_14-5_Common_ICMP_.28Ping.29_Match_Criteria&quot;&gt;Table 14-5 Common ICMP (Ping) Match Criteria&lt;/span&gt;&lt;/h2&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; style=&quot;background-color: white; color: black; font-family: sans-serif; font-size: 13px; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Matches used with ---icmp-type&lt;/th&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Desciption&lt;/th&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;--icmp-type &amp;lt;type&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;The most commonly used types are echo-reply and echo-request&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;br /&gt;In this example:&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT  -p icmp --icmp-type echo-reply   -j ACCEPT
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
iptables is being configured to allow the firewall to send ICMP echo-requests (pings) and in turn, accept the expected ICMP echo-replies.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Consider another example&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;iptables -A INPUT -p icmp --icmp-type echo-request \
         -m limit --limit 1/s -i eth0 -j ACCEPT
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The limit feature in iptables specifies the maximum average number of matches to allow per second. You can specify time intervals in the format /second, /minute, /hour, or /day, or you can use abbreviations so that 3/second is the same as 3/s.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
In this example, ICMP echo requests are restricted to no more than one per second. When tuned correctly, this feature allows you to filter unusually high volumes of traffic that characterize denial of service (DOS) attacks and Internet worms.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;iptables -A INPUT -p tcp --syn -m limit --limit 5/s -i eth0 -j ACCEPT
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
You can expand on the limit feature of iptables to reduce your vulnerability to certain types of denial of service attack. Here a defense for SYN flood attacks was created by limiting the acceptance of TCP segments with the SYN bit set to no more than five per second.&lt;/div&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Table_14-6_Common_Extended_Match_Criteria&quot;&gt;Table 14-6 Common Extended Match Criteria&lt;/span&gt;&lt;/h2&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; style=&quot;background-color: white; color: black; font-family: sans-serif; font-size: 13px; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Switch&lt;/th&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Desciption&lt;/th&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-m multiport --sports &amp;lt;port, port&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;A variety of TCP/UDP source ports separated by commas. Unlike when&amp;nbsp;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-m&lt;/code&gt;&amp;nbsp;isn&#39;t used, they do not have to be within a range.&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-m multiport --dports &amp;lt;port, port&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;A variety of TCP/UDP destination ports separated by commas. Unlike when&amp;nbsp;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-m&lt;/code&gt;&amp;nbsp;isn&#39;t used, they do not have to be within a range.&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-m multiport --ports &amp;lt;port, port&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;A variety of TCP/UDP ports separated by commas. Source and destination ports are assumed to be the same and they do not have to be within a range.&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;-m --state &amp;lt;state&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The most frequently tested states are:&lt;/div&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;b&gt;ESTABLISHED:&lt;/b&gt;&amp;nbsp;The packet is part of a connection that has seen packets in both directions&lt;/div&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;b&gt;NEW:&lt;/b&gt;&amp;nbsp;The packet is the start of a new connection&lt;/div&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;b&gt;RELATED:&lt;/b&gt;&amp;nbsp;The packet is starting a new secondary connection. This is a common feature of such protocols such as an FTP data transfer, or an ICMP error.&lt;/div&gt;
&lt;div style=&quot;line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;b&gt;INVALID:&lt;/b&gt;&amp;nbsp;The packet couldn&#39;t be identified. Could be due to insufficient system resources, or ICMP errors that don&#39;t match an existing data flow.&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
This is an expansion on the previous example:&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;iptables -A FORWARD -s 0/0 -i eth0 -d 192.168.1.58 -o eth1 -p TCP \
         --sport 1024:65535 -m multiport --dports 80,443 -j ACCEPT
 
iptables -A FORWARD -d 0/0 -o eth0 -s 192.168.1.58 -i eth1 -p TCP \
         -m state --state ESTABLISHED -j ACCEPT
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Here iptables is being configured to allow the firewall to accept TCP packets to be routed when they enter on interface eth0 from any IP address destined for IP address of 192.168.1.58 that is reachable via interface eth1. The source port is in the range 1024 to 65535 and the destination ports are port 80 (www/http) and 443 (https). The return packets from 192.168.1.58 are allowed to be accepted too. Instead of stating the source and destination ports, you can simply allow packets related to established connections using the -m state and --state ESTABLISHED options.&lt;/div&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Using_User_Defined_Chains&quot;&gt;Using User Defined Chains&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
As you may remember, you can configure iptables to have user-defined chains. This feature is frequently used to help streamline the processing of packets. For example, instead of using a single, built-in chain for all protocols, you can use the chain to determine the protocol type for the packet and then hand off the actual final processing to a user-defined, protocol-specific chain in the filter table. In other words, you can replace a long chain with a stubby main chain pointing to multiple stubby chains, thereby shortening the total length of all chains the packet has to pass through. For example&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;iptables -A INPUT -i eth0  -d 206.229.110.2 -j fast-input-queue
iptables -A OUTPUT -o eth0 -s 206.229.110.2 -j fast-output-queue

iptables -A fast-input-queue  -p icmp -j icmp-queue-in
iptables -A fast-output-queue -p icmp -j icmp-queue-out

iptables -A icmp-queue-out -p icmp --icmp-type echo-request \
         -m state --state NEW -j ACCEPT

iptables -A icmp-queue-in -p icmp --icmp-type echo-reply -j ACCEPT
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Here six queues help assist in improving processing speed. Table 14.7 summarizes the function of each.&lt;/div&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Table_14.7_Custom_Queues_Example_Listing&quot;&gt;Table 14.7 Custom Queues Example Listing&lt;/span&gt;&lt;/h2&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; style=&quot;background-color: white; color: black; font-family: sans-serif; font-size: 13px; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Chain&lt;/th&gt;&lt;th style=&quot;background-attachment: initial; background-clip: initial; background-color: green; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: white;&quot;&gt;Desciption&lt;/th&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;INPUT&lt;/code&gt;&lt;/td&gt;&lt;td&gt;The regular built-in INPUT chain in iptables&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;OUTPUT&lt;/code&gt;&lt;/td&gt;&lt;td&gt;The regular built-in OUTPUT chain in&amp;nbsp;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;iptables&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;fast-input-queue&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Input chain dedicated to identifying specific protocols and shunting the packets to protocol specific chains.&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;fast-output-queue&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Output chain dedicated to identifying specific protocols and shunting the packets to protocol specific chains.&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;icmp-queue-out&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Output queue dedicated to ICMP&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;&lt;td&gt;&lt;code style=&quot;background-color: #f9f9f9;&quot;&gt;icmp-queue-in&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Input queue dedicated to ICMP&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Saving_Your_iptables_Scripts&quot;&gt;Saving Your iptables Scripts&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The service iptables save command permanently saves the iptables configuration in the /etc/sysconfig/iptables file. When the system reboots, the iptables-restore program reads the configuration and makes it the active configuration.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The format of the /etc/sysconfig/iptables file is slightly different from that of the scripts shown in this chapter. The initialization of built-in chains is automatic and the string &quot;iptables&quot; is omitted from the rule statements.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Here is a sample /etc/sysconfig/iptables configuration that allows ICMP, IPSec (ESP and AH packets), already established connections, and inbound SSH.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;[root@bigboy tmp]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.2.9 on Mon Nov 8 11:00:07 2004 
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [144:12748]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp -m icmp --icmp-type 255 -j ACCEPT
-A RH-Firewall-1-INPUT -p esp -j ACCEPT
-A RH-Firewall-1-INPUT -p ah -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Mon Nov 8 11:00:07 2004
[root@bigboy tmp]#
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
It is never a good idea to edit this script directly because it is always overwritten by the save command and it doesn&#39;t save any comments at all, which can also make it extremely difficult to follow. For these reasons, you&#39;re better off writing and applying a customized script and then using the service iptables save command to make the changes permanent.&lt;/div&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Fedora.27s_iptables_Rule_Generator&quot;&gt;Fedora&#39;s iptables Rule Generator&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Fedora comes with a program called lokkit that you can use to generate a very rudimentary firewall rule set. It prompts for the level of security and then gives you the option of doing simple customizations. It is a good place for beginners to start on a test system so that they can see a general rule structure.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Like the service iptables save command, lokkit saves the firewall rules in a new /etc/sysconfig/iptables file for use on the next reboot.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Once you have become familiar with the iptables syntax, it&#39;s best to write scripts that you can comment and then save it to /etc/sysconfig/iptables. It makes them much more manageable and readable.&lt;/div&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Recovering_From_A_Lost_Script&quot;&gt;Recovering From A Lost Script&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Sometimes the script you created to generate iptables rules may get corrupted or lost, or you might inherit a new system from an administer and cannot find the original script used to protect it. In these situations, you can use the iptables-save and iptables-restore commands to assist you with the continued management of the server.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Unlike the service iptables save command, which actually saves a permanent copy of the firewall&#39;s active configuration in the /etc/sysconfig/iptables file, iptables-save displays the active configuration to the screen in /etc/sysconfig/iptables format. If you redirect the iptables-save screen output to a file with the &amp;gt; symbol, then you can edit the output and reload the updated rules when they meet your new criteria with the iptables-restore command.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
This example exports the iptables-save output to a text file named firewall-config.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;[root@bigboy tmp]# iptables-save &amp;gt; firewall-config
[root@bigboy tmp]# cat firewall-config
# Generated by iptables-save v1.2.9 on Mon Nov 8 11:00:07 2004
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [144:12748]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp -m icmp --icmp-type 255 -j ACCEPT
-A RH-Firewall-1-INPUT -p esp -j ACCEPT
-A RH-Firewall-1-INPUT -p ah -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Mon Nov 8 11:00:07 2004
[root@bigboy tmp]#
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
After editing the firewall-config file with the commands you need, you can reload it into the active firewall rule set with the iptables-restore command.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;[root@bigboy tmp]# iptables-restore &amp;lt; firewall-config
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Finally, you should permanently save the active configuration so that it will be loaded automatically when the system reboots:&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;[root@bigboy tmp]# service iptables save
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
If desired, you can eventually convert this firewall-config file into a regular iptables script so that it becomes more easily recognizable and manageable.&lt;/div&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Loading_Kernel_Modules_Needed_By_iptables&quot;&gt;Loading Kernel Modules Needed By iptables&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The iptables application requires you to load certain kernel modules to activate some of its functions. Whenever any type of NAT is required, the iptable_nat module needs to be loaded. The ip_conntrack_ftp module needs to be added for FTP support and should always be loaded with the ip_conntrack module which tracks TCP connection states. As most scripts probably will keep track of connection states, the ip_conntrack module will be needed in any case. The ip_nat_ftp module also needs to be loaded for FTP servers behind a NAT firewall.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Unfortunately, the /etc/sysconfig/iptables file doesn&#39;t support the loading of modules, so you&#39;ll have to add the statements to your /etc/rc.local file which is run at the end of every reboot.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The script samples in this chapter include these statements only as a reminder to place them in the /etc/rc.local file&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;# File: /etc/rc.local

# Module to track the state of connections
modprobe ip_conntrack

# Load the iptables active FTP module, requires ip_conntrack
modprobe ip_conntrack_ftp
  
# Load iptables NAT module when required
modprobe iptable_nat
 
# Module required for active an FTP server using NAT
modprobe ip_nat_ftp
&lt;/pre&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Sample_iptables_Scripts&quot;&gt;Sample iptables Scripts&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
This section provides some sample scripts you can use to get iptables working for you. Pay special attention to the logging example at the end.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The basic initialization script snippet should also be included in all your scripts to ensure the correct initialization of your chains should you decide to restart your script after startup. This chapter also includes other snippets that will help you get basic functionality. It should be a good guide to get you started.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;b&gt;Note:&lt;/b&gt;&amp;nbsp;Once you feel more confident, you can use&amp;nbsp;&lt;a class=&quot;external text&quot; href=&quot;http://www.linuxhomenetworking.com/about/buy-docs.html&quot; style=&quot;background-attachment: initial; background-clip: initial; background-color: initial; background-image: url(data:image/png; background-origin: initial; background-position: 100% 50%; background-repeat: no-repeat no-repeat; color: #663366; padding-right: 13px;&quot;&gt;Appendix II &quot;Codes, Scripts, and Configurations&quot;&lt;/a&gt;, to find detailed scripts. The appendix shows you how to allow your firewall to:&lt;/div&gt;
&lt;ul style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Be used as a Linux Web, mail and DNS server&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Be the NAT router for your home network&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Prevent various types of attacks using corrupted TCP, UDP and ICMP packets.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Provide outbound passive FTP access from the firewall&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
There are also simpler code snippets in the&amp;nbsp;&lt;a class=&quot;external text&quot; href=&quot;http://www.linuxhomenetworking.com/about/buy-docs.html&quot; style=&quot;background-attachment: initial; background-clip: initial; background-color: initial; background-image: url(data:image/png; background-origin: initial; background-position: 100% 50%; background-repeat: no-repeat no-repeat; color: #663366; padding-right: 13px;&quot;&gt;Appendix II &quot;Codes, Scripts, and Configurations&quot;&lt;/a&gt;, for Inbound and outbound FTP connections to and from your firewall&lt;/div&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Basic_Operating_System_Defense&quot;&gt;Basic Operating System Defense&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
You can do several things before employing your firewall script to improve the resilience of your firewall to attack. For example, the Linux operating system has a number of built-in protection mechanisms that you should activate by modifying the system kernel parameters in the /proc filesystem via the /etc/sysctl.conf file. Using of /etc/sysctl.conf to modify kernel parameters is explained in more detail in , Appendix I &quot;&lt;a href=&quot;http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ap01_:_Miscellaneous_Linux_Topics&quot; style=&quot;background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; color: #5a3696;&quot; title=&quot;Quick HOWTO : Ap01 : Miscellaneous Linux Topics&quot;&gt;Miscellaneous Linux Topics&lt;/a&gt;&quot;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Here is a sample configuration:&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;# File: /etc/sysctl.conf
 
#---------------------------------------------------------------
# Disable routing triangulation. Respond to queries out
# the same interface, not another. Helps to maintain state
# Also protects against IP spoofing
#---------------------------------------------------------------
 
net/ipv4/conf/all/rp_filter = 1
 
 
#---------------------------------------------------------------
# Enable logging of packets with malformed IP addresses
#---------------------------------------------------------------
 
net/ipv4/conf/all/log_martians = 1
 
 
#---------------------------------------------------------------
# Disable redirects
#---------------------------------------------------------------
 
net/ipv4/conf/all/send_redirects = 0
 
 
#---------------------------------------------------------------
# Disable source routed packets
#---------------------------------------------------------------
 
net/ipv4/conf/all/accept_source_route = 0
 
 
#---------------------------------------------------------------
# Disable acceptance of ICMP redirects
#---------------------------------------------------------------
 
net/ipv4/conf/all/accept_redirects = 0
 
 
#---------------------------------------------------------------
# Turn on protection from Denial of Service (DOS) attacks
#---------------------------------------------------------------
 
net/ipv4/tcp_syncookies = 1
 
 
#---------------------------------------------------------------
# Disable responding to ping broadcasts
#---------------------------------------------------------------
 
net/ipv4/icmp_echo_ignore_broadcasts = 1
 
#---------------------------------------------------------------
# Enable IP routing. Required if your firewall is protecting a
# network, NAT included
#---------------------------------------------------------------

net/ipv4/ip_forward = 1
&lt;/pre&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Advanced_iptables_Initialization&quot;&gt;Advanced iptables Initialization&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
You may also want to add some more advanced initialization steps to your script, including checks for Internet traffic from RFC1918 private addresses. The sample script snippet below outlines how to do this. More complex initializations would include checks for attacks using invalid TCP flags and directed broadcasts which are beyond the scope of this book.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The script also uses multiple user-defined chains to make the script shorter and faster as the chains can be repeatedly accessed. This removes the need to repeat the same statements over and over again.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
You can take even more precautions to further protect your network. The complete firewall script in Appendix II&amp;nbsp;&lt;a class=&quot;external text&quot; href=&quot;http://www.linuxhomenetworking.com/about/appendix-buy-lqfn.htm&quot; style=&quot;background-attachment: initial; background-clip: initial; background-color: initial; background-image: url(data:image/png; background-origin: initial; background-position: 100% 50%; background-repeat: no-repeat no-repeat; color: #663366; padding-right: 13px;&quot;&gt;&quot;Codes, Scripts, and Configurations&quot;&lt;/a&gt;, outlines most of them.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=
#
# Define networks: NOTE!! You may want to put these &quot;EXTERNAL&quot;
# definitions at the top of your script.
#
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=
 
EXTERNAL_INT=&quot;eth0&quot;            # External Internet interface
EXTERNAL_IP=&quot;97.158.253.25&quot;    # Internet Interface IP address
 
#---------------------------------------------------------------
# Initialize our user-defined chains
#---------------------------------------------------------------
 
iptables -N valid-src
iptables -N valid-dst
 
#---------------------------------------------------------------
# Verify valid source and destination addresses for all packets
#---------------------------------------------------------------
 
iptables -A INPUT   -i $EXTERNAL_INT -j valid-src
iptables -A FORWARD -i $EXTERNAL_INT -j valid-src
iptables -A OUTPUT  -o $EXTERNAL_INT -j valid-dst
iptables -A FORWARD -o $EXTERNAL_INT -j valid-dst
 
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
#
# Source and Destination Address Sanity Checks
#
# Drop packets from networks covered in &lt;a class=&quot;external mw-magiclink-rfc&quot; href=&quot;http://tools.ietf.org/html/rfc1918&quot; style=&quot;background-attachment: initial; background-clip: initial; background-color: initial; background-image: url(data:image/png; background-origin: initial; background-position: 100% 50%; background-repeat: no-repeat no-repeat; color: #663366; padding-right: 13px;&quot;&gt;RFC 1918&lt;/a&gt; (private nets)
# Drop packets from external interface IP
#
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
 
iptables -A valid-src -s 10.0.0.0/8     -j DROP
iptables -A valid-src -s 172.16.0.0/12  -j DROP
iptables -A valid-src -s 192.168.0.0/16 -j DROP
iptables -A valid-src -s 224.0.0.0/4    -j DROP
iptables -A valid-src -s 240.0.0.0/5    -j DROP
iptables -A valid-src -s 127.0.0.0/8    -j DROP
iptables -A valid-src -s 0.0.0.0/8       -j DROP
iptables -A valid-src -d 255.255.255.255 -j DROP
iptables -A valid-src -s 169.254.0.0/16  -j DROP
iptables -A valid-src -s $EXTERNAL_IP    -j DROP
iptables -A valid-dst -d 224.0.0.0/4    -j DROP
&lt;/pre&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Allowing_DNS_Access_To_Your_Firewall&quot;&gt;Allowing DNS Access To Your Firewall&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
You&#39;ll almost certainly want your firewall to make DNS queries to the Internet. This is not because it is required for the basic functionality of the firewall, but because of Fedora Linux&#39;s yum RPM updater which will help to keep the server up to date with the latest security patches. The following statements will apply not only for firewalls acting as DNS clients but also for firewalls working in a caching or regular DNS server role.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;#---------------------------------------------------------------
# Allow outbound DNS queries from the FW and the replies too
#
# - Interface eth0 is the internet interface
#
# Zone transfers use TCP and not UDP. Most home networks
# / websites using a single DNS server won&#39;t require TCP statements
#
#---------------------------------------------------------------
 
iptables -A OUTPUT -p udp -o eth0 --dport 53 --sport 1024:65535 \
         -j ACCEPT
 
iptables -A INPUT -p udp -i eth0 --sport 53 --dport 1024:65535 \
         -j ACCEPT
&lt;/pre&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Allowing_WWW_And_SSH_Access_To_Your_Firewall&quot;&gt;Allowing WWW And SSH Access To Your Firewall&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
This sample snippet is for a firewall that doubles as a web server that is managed remotely by its system administrator via secure shell (SSH) sessions. Inbound packets destined for ports 80 and 22 are allowed thereby making the first steps in establishing a connection. It isn&#39;t necessary to specify these ports for the return leg as outbound packets for all established connections are allowed. Connections initiated by persons logged into the Web server will be denied as outbound NEW connection packets aren&#39;t allowed.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;#---------------------------------------------------------------
# Allow previously established connections
# - Interface eth0 is the internet interface
#---------------------------------------------------------------

iptables -A OUTPUT -o eth0 -m state --state ESTABLISHED,RELATED \
  -j ACCEPT

#---------------------------------------------------------------
# Allow port 80 (www) and 22 (SSH) connections to the firewall
#---------------------------------------------------------------

iptables -A INPUT -p tcp -i eth0 --dport 22 --sport 1024:65535 \
  -m state --state NEW -j ACCEPT
iptables -A INPUT -p tcp -i eth0 --dport 80 --sport 1024:65535 \
  -m state --state NEW -j ACCEPT

&lt;/pre&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Allowing_Your_Firewall_To_Access_The_Internet&quot;&gt;Allowing Your Firewall To Access The Internet&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
This iptables script enables a user on the firewall to use a Web browser to surf the Internet. HTTP traffic uses TCP port 80, and HTTPS uses port 443.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;b&gt;Note:&lt;/b&gt;&amp;nbsp;HTTPS (secure HTTP) is used for credit card transactions frequently, as well as by RedHat Linux servers running up2date. FTP and HTTP are frequently used with yum.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;#---------------------------------------------------------------
# Allow port 80 (www) and 443 (https) connections from the firewall
#---------------------------------------------------------------

iptables -A OUTPUT -j ACCEPT -m state \
  --state NEW,ESTABLISHED,RELATED -o eth0 -p tcp \
  -m multiport --dports 80,443 --sport 1024:65535
 
#---------------------------------------------------------------
# Allow previously established connections
# - Interface eth0 is the internet interface
#---------------------------------------------------------------

iptables -A INPUT -j ACCEPT -m state --state ESTABLISHED,RELATED  \
-i eth0 -p tcp


&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
If you want all TCP traffic originating from the firewall to be accepted, then remove the line:&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;-m multiport --dports 80,443 --sport 1024:65535
&lt;/pre&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Allow_Your_Home_Network_To_Access_The_Firewall&quot;&gt;Allow Your Home Network To Access The Firewall&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
In this example, eth1 is directly connected to a home network using IP addresses from the 192.168.1.0 network. All traffic between this network and the firewall is simplistically assumed to be trusted and allowed.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Further rules will be needed for the interface connected to the Internet to allow only specific ports, types of connections and possibly even remote servers to have access to your firewall and home network.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;#---------------------------------------------------------------
# Allow all bidirectional traffic from your firewall to the
# protected network
# - Interface eth1 is the private network interface
#---------------------------------------------------------------

iptables -A INPUT   -j ACCEPT -p all -s 192.168.1.0/24 -i eth1
iptables -A OUTPUT  -j ACCEPT -p all -d 192.168.1.0/24 -o eth1
&lt;/pre&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Masquerading_.28Many_to_One_NAT.29&quot;&gt;Masquerading (Many to One NAT)&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
As explained in Chapter 2, &quot;&lt;a href=&quot;http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch02_:_Introduction_to_Networking&quot; style=&quot;background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; color: #5a3696;&quot; title=&quot;Quick HOWTO : Ch02 : Introduction to Networking&quot;&gt;Introduction to Networking&lt;/a&gt;&quot;, masquerading is another name for what many call many to one NAT. In other words, traffic from all devices on one or more protected networks will appear as if it originated from a single IP address on the Internet side of the firewall.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Note: The masquerade IP address always defaults to the IP address of the firewall&#39;s main interface. The advantage of this is that you never have to specify the NAT IP address. This makes it much easier to configure iptables NAT with DHCP.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
You can configure many to one NAT to an IP alias, using the POSTROUTING and not the MASQUERADE statement. An example of this can be seen in the static NAT section that follows.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Keep in mind that iptables requires the iptables_nat module to be loaded with the modprobe command for the masquerade feature to work. Masquerading also depends on the Linux operating system being configured to support routing between the internet and private network interfaces of the firewall. This is done by enabling IP forwarding or routing by giving the file /proc/sys/net/ipv4/ip_forward the value 1 as opposed to the default disabled value of 0.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Once masquerading has been achieved using the POSTROUTING chain of the nat table, you will have to configure iptables to allow packets to flow between the two interfaces. To do this, use the FORWARD chain of the filter table. More specifically, packets related to NEW and ESTABLISHED connections will be allowed outbound to the Internet, but only packets related to ESTABLISHED connections will be allowed inbound. This helps to protect the home network from anyone trying to initiate connections from the Internet:&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;#---------------------------------------------------------------
# Load the NAT module
#
# Note: It is best to use the /etc/rc.local example in this
#       chapter. This value will not be retained in the
#       /etc/sysconfig/iptables file. Included only as a reminder.
#---------------------------------------------------------------
 
modprobe iptable_nat

#---------------------------------------------------------------
# Enable routing by modifying the ip_forward /proc filesystem file
#
# Note: It is best to use the /etc/sysctl.conf example in this
#       chapter. This value will not be retained in the
#       /etc/sysconfig/iptables file. Included only as a reminder.
#---------------------------------------------------------------
 
echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward
 
#---------------------------------------------------------------
# Allow masquerading
# - Interface eth0 is the internet interface
# - Interface eth1 is the private network interface
#---------------------------------------------------------------
 
iptables -A POSTROUTING -t nat -o eth0 -s 192.168.1.0/24 -d 0/0 \
         -j MASQUERADE
 
#---------------------------------------------------------------
# Prior to masquerading, the packets are routed via the filter
# table&#39;s FORWARD chain.
# Allowed outbound: New, established and related connections
# Allowed inbound&amp;nbsp;: Established and related connections
#---------------------------------------------------------------
 
iptables -A FORWARD -t filter -o eth0 -m state \
         --state NEW,ESTABLISHED,RELATED -j ACCEPT
 
iptables -A FORWARD -t filter -i eth0 -m state \
         --state ESTABLISHED,RELATED -j ACCEPT

&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
&lt;b&gt;Note:&lt;/b&gt;&amp;nbsp;If you configure your firewall to do masquerading, then if should be the used as the default gateway for all your servers on the network.&lt;/div&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Port_Forwarding_Type_NAT_.28DHCP_DSL.29&quot;&gt;Port Forwarding Type NAT (DHCP DSL)&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
In many cases home users may get a single DHCP public IP address from their ISPs. If a Linux firewall is also your interface to the Internet and you want to host a Web site on one of the NAT protected home servers, then you will have to use port forwarding. Here the combination of the firewall&#39;s single IP address, the remote server&#39;s IP address, and the source/destination port of the traffic can be used to uniquely identify a traffic flow. All traffic that matches a particular combination of these factors may then be forwarded to a single server on the private network.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Port forwarding is handled by the PREROUTING chain of the nat table. As in masquerading, the iptables_nat module has to be loaded and routing has to be enabled for port forwarding to work. Routing too must be allowed in iptables with the FORWARD chain, this includes all NEW inbound connections from the Internet matching the port forwarding port plus all future packets related to the ESTABLISHED connection in both directions:&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;#---------------------------------------------------------------
# Load the NAT module
#
# Note: It is best to use the /etc/rc.local example in this
#       chapter. This value will not be retained in the
#       /etc/sysconfig/iptables file. Included only as a reminder.
#---------------------------------------------------------------
 
modprobe iptable_nat

#---------------------------------------------------------------
# Get the IP address of the Internet interface eth0 (linux only)
#
# You&#39;ll have to use a different expression to get the IP address
# for other operating systems which have a different ifconfig output
# or enter the IP address manually in the PREROUTING statement
#
# This is best when your firewall gets its IP address using DHCP.
# The external IP address could just be hard coded (&quot;typed in
# normally&quot;)
#---------------------------------------------------------------
 
external_int=&quot;eth0&quot;
external_ip=&quot;`ifconfig $external_int | grep &#39;inet addr&#39; | \
                       awk &#39;{print $2}&#39; | sed -e &#39;s/.*://&#39;`&quot;
#---------------------------------------------------------------
# Enable routing by modifying the ip_forward /proc filesystem file
#
# Note: It is best to use the /etc/sysctl.conf example in this
#       chapter. This value will not be retained in the
#       /etc/sysconfig/iptables file. Included only as a reminder.
#---------------------------------------------------------------
 
echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward
 
#---------------------------------------------------------------
# Allow port forwarding for traffic destined to port 80 of the
# firewall&#39;s IP address to be forwarded to port 8080 on server
# 192.168.1.200
#
# - Interface eth0 is the internet interface
# - Interface eth1 is the private network interface
#---------------------------------------------------------------
 
iptables -t nat -A PREROUTING -p tcp -i eth0 -d $external_ip \
     --dport 80 --sport 1024:65535 -j DNAT --to 192.168.1.200:8080
 
#---------------------------------------------------------------
# After DNAT, the packets are routed via the filter table&#39;s
# FORWARD chain.
# Connections on port 80 to the target machine on the private
# network must be allowed.
#---------------------------------------------------------------
 
iptables -A FORWARD -p tcp -i eth0 -o eth1 -d 192.168.1.200 \
    --dport 8080 --sport 1024:65535 -m state --state NEW -j ACCEPT
 
iptables -A FORWARD -t filter -o eth0 -m state \
         --state NEW,ESTABLISHED,RELATED -j ACCEPT
 
iptables -A FORWARD -t filter -i eth0 -m state \
         --state ESTABLISHED,RELATED -j ACCEPT
 
&lt;/pre&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Static_NAT&quot;&gt;Static NAT&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
In this example, all traffic to a particular public IP address, not just to a particular port, is translated to a single server on the protected subnet. Because the firewall has more than one IP address, I can&#39;t recommend MASQUERADE; it will force masquerading as the IP address of the primary interface and not as any of the alias IP addresses the firewall may have. Instead, use SNAT to specify the alias IP address to be used for connections initiated by all other servers in the protected network.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Note: Although the nat table NATs all traffic to the target servers (192.168.1.100 to 102), only connections on ports 80,443 and 22 are allowed through by the FORWARD chain. Also notice how you have to specify a separate -m multiport option whenever you need to match multiple non-sequential ports for both source and destination.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
In this example the firewall:&lt;/div&gt;
&lt;ul style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Uses one to one NAT to make the server 192.168.1.100 on your home network appear on the Internet as IP addresses 97.158.253.26.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Creates a many to one NAT for the 192.168.1.0 home network in which all the servers appear on the Internet as IP address 97.158.253.29. This is different from masquerading&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
You will have to create alias IP addresses for each of these Internet IPs for one to one NAT to work.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;#---------------------------------------------------------------
# Load the NAT module
#
# Note: It is best to use the /etc/rc.local example in this
#       chapter. This value will not be retained in the
#       /etc/sysconfig/iptables file. Included only as a reminder.
#---------------------------------------------------------------
 
modprobe iptable_nat
 
#---------------------------------------------------------------
# Enable routing by modifying the ip_forward /proc filesystem file
#
# Note: It is best to use the /etc/sysctl.conf example in this
#       chapter. This value will not be retained in the
#       /etc/sysconfig/iptables file. Included only as a reminder.
#---------------------------------------------------------------
 
echo 1 &amp;gt; /proc/sys/net/ipv4/ip_forward
 
#---------------------------------------------------------------
# NAT ALL traffic:
###########
# REMEMBER to create aliases for all the internet IP addresses below
###########
#
# TO:             FROM:           MAP TO SERVER:
# 97.158.253.26    Anywhere        192.168.1.100 (1:1 NAT - Inbound)
# Anywhere        192.168.1.100   97.158.253.26 (1:1 NAT - Outbound)
# Anywhere        192.168.1.0/24  97.158.253.29 (FW IP)
#
# SNAT is used to NAT all other outbound connections initiated
# from the protected network to appear to come from
# IP address 97.158.253.29
#
# POSTROUTING:
#   NATs source IP addresses. Frequently used to NAT connections from
#   your home network to the Internet
#
# PREROUTING:
#   NATs destination IP addresses. Frequently used to NAT
#   connections from the Internet to your home network
#
# - Interface eth0 is the internet interface
# - Interface eth1 is the private network interface
#---------------------------------------------------------------
 
# PREROUTING statements for 1:1 NAT
# (Connections originating from the Internet)

iptables -t nat -A PREROUTING -d 97.158.253.26 -i eth0 \
        -j DNAT --to-destination 192.168.1.100

# POSTROUTING statements for 1:1 NAT
# (Connections originating from the home network servers)

iptables -t nat -A POSTROUTING -s 192.168.1.100 -o eth0 \
         -j SNAT --to-source 97.158.253.26
 
# POSTROUTING statements for Many:1 NAT
# (Connections originating from the entire home network)

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 \
         -j SNAT -o eth0 --to-source 97.158.253.29

# Allow forwarding to each of the servers configured for 1:1 NAT
# (For connections originating from the Internet. Notice how you
# use the real IP addresses here)
 
iptables -A FORWARD -p tcp -i eth0 -o eth1 -d 192.168.1.100 \
    -m multiport --dports 80,443,22 \
    -m state --state NEW -j ACCEPT
 
 
# Allow forwarding for all New and Established SNAT connections
# originating on the home network AND already established
# DNAT connections
 
iptables -A FORWARD -t filter -o eth0 -m state \
         --state NEW,ESTABLISHED,RELATED -j ACCEPT
 
# Allow forwarding for all 1:1 NAT connections originating on
# the Internet that have already passed through the NEW forwarding
# statements above
 
iptables -A FORWARD -t filter -i eth0 -m state \
         --state ESTABLISHED,RELATED -j ACCEPT
&lt;/pre&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Troubleshooting_iptables&quot;&gt;Troubleshooting iptables&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
A number of tools are at your disposal for troubleshooting iptables firewall scripts. One of the best methods is to log all dropped packets to the /var/log/messages file.&lt;/div&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Checking_The_Firewall_Logs&quot;&gt;Checking The Firewall Logs&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
You track packets passing through the iptables list of rules using the LOG target. You should be aware that the LOG target:&lt;/div&gt;
&lt;ul style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Logs all traffic that matches the iptables rule in which it is located.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Automatically writes an entry to the /var/log/messages file and then executes the next rule.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
If you want to log only unwanted traffic, therefore, you have to add a matching rule with a DROP target immediately after the LOG rule. If you don&#39;t, you&#39;ll find yourself logging both desired and unwanted traffic with no way of discerning between the two, because by default iptables doesn&#39;t state why the packet was logged in its log message.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
This example logs a summary of failed packets to the file /var/log/messages. You can use the contents of this file to determine which TCP/UDP ports you need to open to provide access to specific traffic that is currently stopped.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;#---------------------------------------------------------------
# Log and drop all other packets to file /var/log/messages
# Without this we could be crawling around in the dark
#---------------------------------------------------------------
 
iptables -A OUTPUT -j LOG
iptables -A INPUT -j LOG
iptables -A FORWARD -j LOG
 
iptables -A OUTPUT -j DROP
iptables -A INPUT -j DROP
iptables -A FORWARD -j DROP

&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
Here are some examples of the output of this file:&lt;/div&gt;
&lt;ul style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Firewall denies replies to DNS queries (UDP port 53) destined to server 192.168.1.102 on the home network.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;Feb 23 20:33:50 bigboy kernel: IN=wlan0 OUT= MAC=00:06:25:09:69:80:00:a0:c5:e1:3e:88:08:00 SRC=192.42.93.30 DST=192.168.1.102 LEN=220 TOS=0x00 PREC=0x00 TTL=54 ID=30485 PROTO=UDP SPT=53 DPT=32820 LEN=200
&lt;/pre&gt;
&lt;ul style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Firewall denies Windows NetBIOS traffic (UDP port 138)&lt;/li&gt;
&lt;/ul&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;Feb 23 20:43:08 bigboy kernel: IN=wlan0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:06:25:09:6a:b5:08:00 SRC=192.168.1.100 DST=192.168.1.255 LEN=241 TOS=0x00 PREC=0x00 TTL=64 ID=0 DF PROTO=UDP SPT=138 DPT=138 LEN=221
&lt;/pre&gt;
&lt;ul style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; list-style-image: url(data:image/gif; list-style-type: square; margin-bottom: 0px; margin-left: 1.5em; margin-right: 0px; margin-top: 0.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0.1em;&quot;&gt;Firewall denies Network Time Protocol (NTP UDP port 123)&lt;/li&gt;
&lt;/ul&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;Feb 23 20:58:48 bigboy kernel: IN= OUT=wlan0 SRC=192.168.1.102 DST=207.200.81.113 LEN=76 TOS=0x10 PREC=0x00 TTL=64 ID=0 DF PROTO=UDP SPT=123 DPT=123 LEN=56
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The traffic in all these examples isn&#39;t destined for the firewall; Therefore, you should check your INPUT, OUTPUT, FORWARD, and NAT related statements. If the firewall&#39;s IP address is involved, then you should focus on the INPUT and OUTPUT statements&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
If nothing shows up in the logs, then follow the steps in Chapter 4, &quot;&lt;a href=&quot;http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch04_:_Simple_Network_Troubleshooting&quot; style=&quot;background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; color: #5a3696;&quot; title=&quot;Quick HOWTO : Ch04 : Simple Network Troubleshooting&quot;&gt;Simple Network Troubleshooting&lt;/a&gt;&quot;, to determine whether the data is reaching your firewall at all and, if it is not, the location on your network that could be causing the problem.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
As a general rule, you won&#39;t be able to access the public NAT IP addresses from servers on your home network. Basic NAT testing requires you to ask a friend to try to connect to your home network from the Internet.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
You can then use the logging output in /var/log/messages to make sure that the translations are occurring correctly and iptables isn&#39;t dropping the packets after translation occurs.&lt;/div&gt;
&lt;h2 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; color: teal; font-family: sans-serif; font-size: 19px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;iptables_Won.27t_Start&quot;&gt;iptables Won&#39;t Start&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
The iptables startup script expects to find the /etc/sysconfig/iptables before it starts. If none exists, then symptoms include the firewall status always being stopped and the /etc/init.d/iptables script running without the typical [OK] or [FAILED] messages.&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
If you have just installed iptables and have never applied a policy, then you will face this problem. Unfortunately, running the service iptables save command before restarting won&#39;t help either. You have to create this file.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f9f9f9; border-bottom-color: rgb(47, 111, 171); border-bottom-style: dashed; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(47, 111, 171); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(47, 111, 171); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(47, 111, 171); border-top-style: dashed; border-top-width: 1px; line-height: 1.1em; padding-bottom: 1em; padding-left: 1em; padding-right: 1em; padding-top: 1em;&quot;&gt;[root@bigboy tmp]# service iptables start
[root@bigboy tmp]#

[root@bigboy tmp]# touch /etc/sysconfig/iptables
[root@bigboy tmp]# chmod 600 /etc/sysconfig/iptables

[root@bigboy tmp]# service iptables start
Applying iptables firewall rules: [  OK  ]
[root@bigboy tmp]#
&lt;/pre&gt;
&lt;h1 style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; border-bottom-color: rgb(4, 132, 68); border-bottom-style: solid; border-bottom-width: 2px; color: green; font-family: sans-serif; font-size: 24px; font-weight: normal; line-height: normal; margin-bottom: 0.6em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em;&quot;&gt;
&lt;span class=&quot;mw-headline&quot; id=&quot;Conclusion&quot;&gt;Conclusion&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 1.5em; margin-bottom: 0.5em; margin-top: 0.4em;&quot;&gt;
A firewall is a critical part of any establishment that connects to an unprotected network such as the Internet, but a firewall is never sufficient. Web site security involves not just protection from corrupted packets or maliciously overwhelming volumes of traffic, but also involves daily data backups to help recovery from device failures, regular application patching, enforced password policies, restricted and monitored physical access to your servers, reliable power and cooling, secured cabling, redundant hardware, and, probably most importantly, well trained and motivated employees. Security should be viewed as anything that contributes to the desired risk-free functioning of your site, and it is well worth the money to invest in and learn from a book that specializes in the topic.&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/7114261823604941324/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/linux-firewalls-using-iptables.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7114261823604941324'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7114261823604941324'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/linux-firewalls-using-iptables.html' title='Linux Firewalls Using iptables'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-2263447192803183551</id><published>2012-04-12T02:33:00.002-07:00</published><updated>2012-04-12T21:26:36.528-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Social Service"/><title type='text'>Truth might surprise U</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
A 24 year old boy seeing out from the train&#39;s window shouted...&lt;br /&gt;
&quot;Dad, look the trees are going behind!&quot;&lt;br /&gt;
Dad smiled and a young couple sitting nearby, looked at the 24 year old&#39;s childish behavior with pity, suddenly he again exclaimed...&lt;br /&gt;
&quot;Dad, look the clouds are running with us!&quot;&lt;br /&gt;
The couple couldn&#39;t resist and said to the old man...&lt;br /&gt;
&quot;Why don&#39;t you take your son to a good doctor?&quot;&lt;br /&gt;
The old man smiled and said...&lt;br /&gt;
&quot;I did and we are just coming from the hospital, my son was blind from birth, he just got his eyes today.&quot;&lt;br /&gt;
MORAL..........&lt;br /&gt;
Every single person on the planet has a story. Don&#39;t judge people before you truly know them&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/2263447192803183551/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/truth-might-surprise-u.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/2263447192803183551'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/2263447192803183551'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/truth-might-surprise-u.html' title='Truth might surprise U'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-6361408142151332660</id><published>2012-04-12T02:31:00.005-07:00</published><updated>2012-04-18T02:25:54.070-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="TechNology"/><title type='text'>Getting Started with J2SSH</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;The first thing you will probably want to do is to connect to an SSH server using J2SSH. This is a fairly&lt;br /&gt;straightforward procedure using the SshClient class. This class provides access for connecting, authenticating&lt;br /&gt;and starting a session channel, which enables you to execute commands or start the users shell.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;import com.sshtools.j2ssh.SshClient;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;First of all prepare your application, in this section we will guide you through the basics so for now just a simple&lt;br /&gt;try/catch inside the static main method.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; text-align: -webkit-auto;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;import java.io.BufferedReader;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;import java.io.InputStream;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;import java.io.InputStreamReader;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;import java.io.OutputStream;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;public class SshExample() {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;// A buffered reader so we can request information from the user&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;private static BufferedReader reader =&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;new BufferedReader(new InputStreamReader(System.in));&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;public static void main(String args[]) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp;try {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp; &amp;nbsp;// Further code will be added here&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;} catch(Exception e) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp; &amp;nbsp;e.printStackTrace();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;The next few sections will guide you through making the initial connection, authenticating the user and executing&lt;br /&gt;a command or starting the users shell for a simple console based SSH application.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;em&gt;&lt;strong&gt;Making the initial connection&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;To create an SshClient instance import the class into your implementation class file and use the following code&lt;br /&gt;to connect to an SSH server on the standard port 22.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;SshClient ssh&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;= new SshClient();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;System.out.print(&quot;Host to connect: &quot;);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;String hostname = reader.readLine();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;ssh.connect(hostname);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;When the client connects to the server, the server supplies its public key for the client to verify. You will see&lt;br /&gt;that calling the&amp;nbsp;&lt;code style=&quot;color: #666666;&quot;&gt;connect&lt;/code&gt;&amp;nbsp;method prompts the user within the console to verify the key:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;The host firestar is currently unknown to the system&lt;br /&gt;The host key fingerprint is: 1028: 69 54 9c 49 e5 92 59 40 5 66 c5 2e 9d 86 af ed&lt;br /&gt;Do you want to allow this host key? [Yes|No|Always]:&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;In the default implementation of the&amp;nbsp;&lt;code style=&quot;color: #666666;&quot;&gt;connect&lt;/code&gt;&amp;nbsp;method, J2SSH reads the&amp;nbsp;&lt;em&gt;$HOME/.ssh/known_hosts&lt;/em&gt;&amp;nbsp;file to determines to which hosts connections&lt;br /&gt;may be allowed. This is provided by the class ConsoleKnownHostsKeyVerification and the&lt;br /&gt;default behavior can be emulated by the following code:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;import&amp;nbsp;com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;ssh.connect(&quot;firestar&quot;, new&amp;nbsp;IgnoreHostKeyVerification());&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;When the connect method returns, the protocol has been negotiated and key exchange has taken place, leaving the connection ready for authenticating the user.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;strong&gt;&lt;em&gt;Authenticating the user&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;Once the connection has been completed the user is required to provide a set of credentials for authentication.&lt;br /&gt;All client side authentication methods are implemented using the abstract class:&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;import com.sshtools.j2ssh.authentication.SshAuthenticationClient.&lt;/code&gt;&lt;/blockquote&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;To perform authentication, the SshClient class provides the following method:&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;public int authenticate(SshAuthenticationClient auth);&lt;/code&gt;&lt;/blockquote&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;There are currently five authentication methods implemented by J2SSH, &#39;password&#39;, &#39;publickey&#39;, &#39;keyboard-interactive&#39; and &#39;hostbased&#39;. With an extra agent authentication method that performs public key authentication using the J2SSH key agent.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;em&gt;&lt;strong&gt;Password Authentication&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;Password authentication is ideal for first time users as it requires no additional configuration within the SSH&lt;br /&gt;client or server. The user simply supplies his username and password to the client which is then transmitted over&lt;br /&gt;the encrypted connection to the server. The server then checks that the given password is acceptable to the native&lt;br /&gt;password-authentication mechanism of the host operating system and returns the result to the client.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;J2SSH implements the &#39;password&#39; authentication method with the following class:&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient&lt;/code&gt;&lt;/blockquote&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;Using the password authentication method is straight forward; create an instance of the PasswordAuthentication&lt;br /&gt;class, set the username and password and pass to the SshClient to complete the authentication.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp;/**&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;* Create a PasswordAuthenticationClient instance, set the properties&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;* and pass to the SessionClient to authenticate&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;System.out.print(&quot;Username: &quot;);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;String username = reader.readLine();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;auth.setUsername(username);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;System.out.print(&quot;Password: &quot;);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;String password = reader.readLine();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;auth.setPassword(password);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;int result = ssh.authenticate(pwd);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;strong&gt;&lt;em&gt;The Authentication Result&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;When the authentication method completes it returns the result of the authentication. This integer value can be any&lt;br /&gt;of the following three values defined in the class:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;..&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;..&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;if(result==AuthenticationProtocolState.FAILED)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp; System.out.println(&quot;The authentication failed&quot;);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;if(result==AuthenticationProtocolState.PARTIAL)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp; System.out.println(&quot;The authentication succeeded but another&quot;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &quot;authentication is required&quot;);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;if(result==AuthenticationProtocolState.COMPLETE)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp; System.out.println(&quot;The authentication is complete&quot;);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;strong&gt;&lt;em&gt;Retrieving the available authentication Methods&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;It is possible at any time after the connection has been established and before authentication has been completed to request a list of authentication methods that can be used. The getAvailableAuthMethods method returns a list of authentication method names.&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;public List getAvailableAuthMethods(String username);&lt;/code&gt;&lt;/blockquote&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;em&gt;It should be noted that the SSH specification allows the server to return authentication methods that are not valid&lt;br /&gt;for the user.&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;strong&gt;&lt;em&gt;Prompting the User for Authentication Details?&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;Each SshAuthenticationClient implementation can optionally be set a prompt interface which allows the user to&lt;br /&gt;be prompted for the information once the authenticate method has been invoked.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; text-align: -webkit-auto;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;public interface SshAuthenticationPrompt {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;public boolean showPrompt(SshAuthenticationClient instance)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throws AuthenticationProtocolException;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;The showPrompt method is called if the authentication instance is not ready to authenticate. The methods is called and the developer should verify the instance of the SshAuthenticationClient to make sure that it is compatible with the prompt (for example you cannot perform public key authentication&lt;br /&gt;with a password prompt!). The user should then be duly prompted for the information and the instance set with the user&#39;s information. Once&lt;br /&gt;complete, the prompt returns true to indicate that the user successfully entered correct information.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;There are several prompts provided in the J2SSH common packages that provide useful Swing based dialogs to&lt;br /&gt;prompt the user.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;import com.sshtools.common.authentication.PasswordAuthenticationDialog;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;/***&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;* Create a PasswordAuthenticationDialog instance and call the&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;* showAuthenticationMethod so the user can graphically&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;* enter their username and password&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;PasswordAuthenticationClient pwd =&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new PasswordAuthenticationClient();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;PasswordAuthenticationDialog dialog =&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new PasswordAuthenticationDialog(parent);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;pwd.setAuthenticationPrompt(dialog);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;int result;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;result = ssh.authenticate(pwd);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;strong&gt;&lt;em&gt;Using a session channel to execute a command or start the users shell&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;Once the user is authenticated you will probably want to do something such as execute a command or start the users shell. The SSH protocol provides multiplexed channels over a single connection and the session channel is one of the channels defined by the SSH protocol. The session channel allows the client to execute a single command on the remote host and to communicate with the process by sending and receiving data. The J2SSH SessionChannelClient implements this channel and we will use this to execute a basic &quot;ls&quot; command.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;The session channel provides an inputstream and outputstream for reading/writing, but before we can do this we need to setup the channel for our command. First we open the channel itself by calling the SshClient method:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;background-color: inherit; font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif; font-size: 12px; line-height: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;background-color: white; color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif; font-size: 12px; line-height: 14px;&quot;&gt;SessionChannelClient&amp;nbsp;session&amp;nbsp;=&amp;nbsp;ssh.openSessionChannel();&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif; font-size: 12px; line-height: 14px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;background-color: white; color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif; font-size: 12px; line-height: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;background-color: white; color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif; font-size: 12px; line-height: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;Now that we have a session instance we need to configure it for our command, there are several options that can be set before we invoke one of the methods that will start the session.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;em&gt;Setting Environment Variables&lt;/em&gt;The SSH protocol provides a method to set an environment variable for the processes environment, the protocol also leaves the actual implementation of this down the server implementation, and in our experience most servers do not allow for this for security reasons. However the method is available since its defined in the protocol specification, so your free to try to use it, but beware the variable may not be set!&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;public boolean setEnvironmentVariable(String name, String value);&lt;/code&gt;&lt;/blockquote&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;em&gt;Requesting a Pseudo Terminal&lt;/em&gt;&lt;br /&gt;A pseudo terminal is a device that imitates a terminal. Rather than being connected to an actual terminal, a pseudo-terminal (or pty) is connected to a process. If the command you are executing is expecting a terminal (such as a shell command) you can request that a pseudo terminal be attached to the process by calling the requestPseudoTerminal method.&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;public boolean requestPseudoTerminal(String term, int cols, int rows, int width, int height, String modes);&lt;/code&gt;&lt;/blockquote&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;em&gt;Invoking a command&lt;/em&gt;After the above operations have been performed you can then request that the session either start the user&#39;s shell, execute a specific command or start an SSH subsystem (such as SFTP). You should not invoke a subsystem unless you are able to read/write the subsystem protocol, there are many additional utilities within J2SSH that provide for the available subsystems.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;To start the users default shell use:&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;public boolean startShell();&lt;/code&gt;&lt;/blockquote&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;Or to execute a specific command use:&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;public boolean executeCommand(String command);&lt;/code&gt;&lt;/blockquote&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;em&gt;An important note to remember is that this does not execute a shell command. You cannot for instance issue the command executeCommand(&quot;dir&quot;)&quot; on the Windows Operating system as this is a shell command, instead use &quot;cmd.exe /C dir&quot;. This method executes a binary executable and so should be used to execute any program other than the users shell.&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;strong&gt;&lt;em&gt;Handling Session Data&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;Once the session has been configured and a command or shell has been started, you can begin to&lt;br /&gt;transfer data to and from the remote computer using the sessions IO streams. These streams provide you with a&lt;br /&gt;standardized interface for reading and writing the data.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;em&gt;The Session Channel&#39;s OutputStream&lt;/em&gt;&lt;br /&gt;The format of writing data varies according to how you configured the session, for example if you executed the&lt;br /&gt;users shell then the data should be written as if the user had entered the commands interactively.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;background-color: inherit; font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif; font-size: 12px; line-height: 14px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 14px;&quot;&gt;/** Writing to the session OutputStream */&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 14px;&quot;&gt;OutputStream out = session.getOutputStream();&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 14px;&quot;&gt;String cmd = &quot;ls\n&quot;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 14px;&quot;&gt;out.write(cmd.getBytes());&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: inherit; color: inherit; font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif; font-size: 12px; line-height: 14px;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;background-color: inherit; color: inherit; font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif; font-size: 12px; line-height: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;background-color: inherit; color: inherit; font-family: Consolas, &#39;Courier New&#39;, Courier, mono, serif; font-size: 12px; line-height: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;em&gt;The Session Channel&#39;s InputStream&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;* Reading from the session InputStream&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;InputStream in = session.getInputStream();&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;byte buffer[] = new byte[255];&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;int read;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;while((read = in.read(buffer)) &amp;gt; 0) {&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;String out = new String(buffer, 0, read);&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;System.out.println(out);&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color: #f6f6f6;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;em&gt;Reading from stderr&lt;/em&gt;&lt;br /&gt;The session also provides the stderr data provided by the remote session. Again an InputStream is provided.&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;public InputStream session.getStderrInputStream();&lt;/code&gt;&lt;/blockquote&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;strong&gt;&lt;em&gt;Closing the Session&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;The session can be closed using the following method:&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;public void close();&lt;/code&gt;&lt;/blockquote&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;Disconnecting&lt;/span&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;The connection can be terminated by either side. To terminate the connection call the SshClient method:&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;code style=&quot;color: #666666;&quot;&gt;public void disconnect();&lt;/code&gt;&lt;/blockquote&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;&lt;em&gt;&lt;strong&gt;A word on executing multiple commands&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;So we can now execute a single command on the remote server, but what&#39;s that I hear you say? I want to execute more than one command? Well if you cast your mind back I told you that the SSH protocol provides multiplexed channels over a single connection, so executing another command is as simple as executing the first, just create a new instance of the SessionChannelClient for every command you want to execute. You can execute them simultaneously or one after another, but always create a new session (since the session is closed when the command finishes and the protocol does not allow for re-using of a session to execute another command).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;There is a drawback to this in that the process environment is not passed on from one session to another, so you cannot for example execute a command to change directory and then another to execute a script in that directory, since the change directory is lost when the session closes and the new command starts back in the default working directory. Of course you could always put the cd command into the script? Or use the shell to execute both commands. This subject is certainly a bit more advanced so I will leave it for another day and pencil in a new article to discuss all the alternatives for executing multiple commands.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #f6f6f6; font-family: Verdana, sans-serif; font-size: 13px; text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;&quot;&gt;This concludes our getting started tutorial, you should now have a basic working knowledge of how to connect, authenticate and execute commands using J2SSH.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/6361408142151332660/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/getting-started-with-j2ssh.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/6361408142151332660'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/6361408142151332660'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/getting-started-with-j2ssh.html' title='Getting Started with J2SSH'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-5952394111439682379</id><published>2012-04-08T22:48:00.001-07:00</published><updated>2012-04-08T22:48:38.849-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Social Service"/><title type='text'>Nice reply</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
A reply from CEO of J.P. Morgan to a pretty girl seeking a rich husband&lt;br /&gt;
&lt;br /&gt;
A young and pretty lady posted this on a popular forum:&lt;br /&gt;
&lt;br /&gt;
Title: What should I do to marry a rich guy?&lt;br /&gt;
&lt;br /&gt;
I&#39;m going to be honest of what I&#39;m going to say here.&lt;br /&gt;
&lt;br /&gt;
I&#39;m 25 this year. I&#39;m very pretty, have style and good taste. I wish to marry a guy with $500k annual salary or above.&lt;br /&gt;
&lt;br /&gt;
You might say that I&#39;m greedy, but an annual salary of $1M is considered only as middle class in New York.&lt;br /&gt;
&lt;br /&gt;
My requirement is not high. Is there anyone in this forum who has an income of $500k annual salary? Are you all married?&lt;br /&gt;
&lt;br /&gt;
I wanted to ask: what should I do to marry rich persons like you?&lt;br /&gt;
&lt;br /&gt;
Among those I&#39;ve dated, the richest is $250k annual income, and it seems that this is my upper limit.&lt;br /&gt;
&lt;br /&gt;
If someone is going to move into high cost residential area on the west of New York City Garden(?), $250k annual income is not enough.&lt;br /&gt;
&lt;br /&gt;
I&#39;m here humbly to ask a few questions:&lt;br /&gt;
1) Where do most rich bachelors hang out? (Please list down the names and addresses of bars, restaurant, gym)&lt;br /&gt;
2) Which age group should I target?&lt;br /&gt;
3) Why most wives of the riches are only average-looking? I&#39;ve met a few girls who don&#39;t have looks and are not interesting, but they are able to marry rich guys.&lt;br /&gt;
&lt;br /&gt;
4) How do you decide who can be your wife, and who can only be your girlfriend? (my target now is to get married)&lt;br /&gt;
&lt;br /&gt;
Ms. Pretty&lt;br /&gt;
&lt;br /&gt;
A philosophical reply from CEO of J.P. Morgan:&lt;br /&gt;
&lt;br /&gt;
Dear Ms. Pretty,&lt;br /&gt;
I have read your post with great interest. Guess there are lots of girls out there who have similar questions like yours. Please allow me to analyse your situation as a professional investor.&lt;br /&gt;
&lt;br /&gt;
My annual income is more than $500k, which meets your requirement, so I hope everyone believes that I&#39;m not wasting time here.&lt;br /&gt;
&lt;br /&gt;
From the standpoint of a business person, it is a bad decision to marry you. The answer is very simple, so let me explain.&lt;br /&gt;
&lt;br /&gt;
Put the details aside, what you&#39;re trying to do is an exchange of &quot;beauty&quot; and &quot;money&quot; : Person A provides beauty, and Person B pays for it, fair and square.&lt;br /&gt;
&lt;br /&gt;
However, there&#39;s a deadly problem here, your beauty will fade, but my money will not be gone without any good reason. The fact is, my income might increase from year to year, but you can&#39;t be prettier year after year.&lt;br /&gt;
&lt;br /&gt;
Hence from the viewpoint of economics, I am an appreciation asset, and you are a depreciation asset. It&#39;s not just normal depreciation, but exponential depreciation. If that is your only asset, your value will be much worse 10 years later.&lt;br /&gt;
&lt;br /&gt;
By the terms we use in Wall Street, every trading has a position, dating with you is also a &quot;trading position&quot;.&lt;br /&gt;
If the trade value dropped we will sell it and it is not a good idea to keep it for long term - same goes with the marriage that you wanted. It might be cruel to say this, but in order to make a wiser decision any assets with great depreciation value will be sold or &quot;leased&quot;.&lt;br /&gt;
&lt;br /&gt;
Anyone with over $500k annual income is not a fool; we would only date you, but will not marry you. I would advice that you forget looking for any clues to marry a rich guy. And by the way, you could make yourself to become a rich person with $500k annual income.This has better chance than finding a rich fool.&lt;br /&gt;
&lt;br /&gt;
Hope this reply helps.&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/5952394111439682379/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/nice-reply.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/5952394111439682379'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/5952394111439682379'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/nice-reply.html' title='Nice reply'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-1470136105555547288</id><published>2012-04-04T23:39:00.003-07:00</published><updated>2012-04-04T23:39:59.487-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Social Service"/><title type='text'>Married or not you should read this...</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
“When I got home that night as my wife served dinner, I held her hand and said, I’ve got something to tell you. She sat down and ate quietly. Again I observed the hurt in her eyes.&lt;br /&gt;
&lt;br /&gt;
Suddenly I didn’t know how to open my mouth. But I had to let her know what I was thinking. I want a divorce. I raised the topic calmly. She didn’t seem to be annoyed by my words, instead she asked me softly, why?&lt;br /&gt;
&lt;br /&gt;
I avoided her question. This made her angry. She threw away the chopsticks and shouted at me, you are not a man! That night, we didn’t talk to each other. She was weeping. I knew she wanted to find out what had happened to our marriage. But I could hardly give her a satisfactory answer; she had lost my heart to Jane. I didn’t love her anymore. I just pitied her!&lt;br /&gt;
&lt;br /&gt;
With a deep sense of guilt, I drafted a divorce agreement which stated that she could own our house, our car, and 30% stake of my company. She glanced at it and then tore it into pieces. The woman who had spent ten years of her life with me had become a stranger. I felt sorry for her wasted time, resources and energy but I could not take back what I had said for I loved Jane so dearly. Finally she cried loudly in front of me, which was what I had expected to see. To me her cry was actually a kind of release. The idea of divorce which had obsessed me for several weeks seemed to be firmer and clearer now.&lt;br /&gt;
&lt;br /&gt;
The next day, I came back home very late and found her writing something at the table. I didn’t have supper but went straight to sleep and fell asleep very fast because I was tired after an eventful day with Jane. When I woke up, she was still there at the table writing. I just did not care so I turned over and was asleep again.&lt;br /&gt;
&lt;br /&gt;
In the morning she presented her divorce conditions: she didn’t want anything from me, but needed a month’s notice before the divorce. She requested that in that one month we both struggle to live as normal a life as possible. Her reasons were simple: our son had his exams in a month’s time and she didn’t want to disrupt him with our broken marriage.&lt;br /&gt;
&lt;br /&gt;
This was agreeable to me. But she had something more, she asked me to recall how I had carried her into out bridal room on our wedding day. She requested that every day for the month’s duration I carry her out of our bedroom to the front door ever morning. I thought she was going crazy. Just to make our last days together bearable I accepted her odd request.&lt;br /&gt;
&lt;br /&gt;
I told Jane about my wife’s divorce conditions. . She laughed loudly and thought it was absurd. No matter what tricks she applies, she has to face the divorce, she said scornfully.&lt;br /&gt;
&lt;br /&gt;
My wife and I hadn’t had any body contact since my divorce intention was explicitly expressed. So when I carried her out on the first day, we both appeared clumsy. Our son clapped behind us, daddy is holding mommy in his arms. His words brought me a sense of pain. From the bedroom to the sitting room, then to the door, I walked over ten meters with her in my arms. She closed her eyes and said softly; don’t tell our son about the divorce. I nodded, feeling somewhat upset. I put her down outside the door. She went to wait for the bus to work. I drove alone to the office.&lt;br /&gt;
&lt;br /&gt;
On the second day, both of us acted much more easily. She leaned on my chest. I could smell the fragrance of her blouse. I realized that I hadn’t looked at this woman carefully for a long time. I realized she was not young any more. There were fine wrinkles on her face, her hair was graying! Our marriage had taken its toll on her. For a minute I wondered what I had done to her.&lt;br /&gt;
&lt;br /&gt;
On the fourth day, when I lifted her up, I felt a sense of intimacy returning. This was the woman who had given ten years of her life to me. On the fifth and sixth day, I realized that our sense of intimacy was growing again. I didn’t tell Jane about this. It became easier to carry her as the month slipped by. Perhaps the everyday workout made me stronger.&lt;br /&gt;
&lt;br /&gt;
She was choosing what to wear one morning. She tried on quite a few dresses but could not find a suitable one. Then she sighed, all my dresses have grown bigger. I suddenly realized that she had grown so thin, that was the reason why I could carry her more easily.&lt;br /&gt;
&lt;br /&gt;
Suddenly it hit me… she had buried so much pain and bitterness in her heart. Subconsciously I reached out and touched her head.&lt;br /&gt;
&lt;br /&gt;
Our son came in at the moment and said, Dad, it’s time to carry mom out. To him, seeing his father carrying his mother out had become an essential part of his life. My wife gestured to our son to come closer and hugged him tightly. I turned my face away because I was afraid I might change my mind at this last minute. I then held her in my arms, walking from the bedroom, through the sitting room, to the hallway. Her hand surrounded my neck softly and naturally. I held her body tightly; it was just like our wedding day.&lt;br /&gt;
&lt;br /&gt;
But her much lighter weight made me sad. On the last day, when I held her in my arms I could hardly move a step. Our son had gone to school. I held her tightly and said, I hadn’t noticed that our life lacked intimacy. I drove to office…. jumped out of the car swiftly without locking the door. I was afraid any delay would make me change my mind…I walked upstairs. Jane opened the door and I said to her, Sorry, Jane, I do not want the divorce anymore.&lt;br /&gt;
&lt;br /&gt;
She looked at me, astonished, and then touched my forehead. Do you have a fever? She said. I moved her hand off my head. Sorry, Jane, I said, I won’t divorce. My marriage life was boring probably because she and I didn’t value the details of our lives, not because we didn’t love each other anymore. Now I realize that since I carried her into my home on our wedding day I am supposed to hold her until death do us apart. Jane seemed to suddenly wake up. She gave me a loud slap and then slammed the door and burst into tears. I walked downstairs and drove away. At the floral shop on the way, I ordered a bouquet of flowers for my wife. The salesgirl asked me what to write on the card. I smiled and wrote, I’ll carry you out every morning until death do us apart.&lt;br /&gt;
&lt;br /&gt;
That evening I arrived home, flowers in my hands, a smile on my face, I run up stairs, only to find my wife in the bed -dead. My wife had been fighting CANCER for months and I was so busy with Jane to even notice. She knew that she would die soon and she wanted to save me from the whatever negative reaction from our son, in case we push through with the divorce.— At least, in the eyes of our son—- I’m a loving husband….&lt;br /&gt;
&lt;br /&gt;
The small details of your lives are what really matter in a relationship. It is not the mansion, the car, property, the money in the bank. These create an environment conducive for happiness but cannot give happiness in themselves.&lt;br /&gt;
&lt;br /&gt;
So find time to be your spouse’s friend and do those little things for each other that build intimacy. Do have a real happy marriage!&lt;br /&gt;
&lt;br /&gt;
If you don’t share this, nothing will happen to you.&lt;br /&gt;
&lt;br /&gt;
If you do, you just might save a marriage. Many of life’s failures are people who did not realize how close they were to success when they gave up&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/1470136105555547288/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/married-or-not-you-should-read-this.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/1470136105555547288'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/1470136105555547288'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/married-or-not-you-should-read-this.html' title='Married or not you should read this...'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-1114397340027874219</id><published>2012-04-04T23:19:00.002-07:00</published><updated>2012-04-04T23:40:11.379-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Social Service"/><title type='text'>The Elephant Rope</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;background-color: white; color: grey; font-family: &#39;lucida grande&#39;, tahoma, verdana, arial, sans-serif; font-size: 11px; line-height: 14px;&quot;&gt;As a man was passing the elephants, he suddenly stopped, conf&lt;/span&gt;&lt;span class=&quot;text_exposed_show&quot; style=&quot;background-color: white; color: grey; display: inline; font-family: &#39;lucida grande&#39;, tahoma, verdana, arial, sans-serif; font-size: 11px; line-height: 14px;&quot;&gt;used by the fact that these huge creatures were being held by only a small rope tied to their front leg. No chains, no cages. It was obvious that the elephants could, at anytime, break away from their bonds but for some reason, they did not.&lt;br /&gt;&lt;br /&gt;He saw a trainer nearby and asked why these animals just stood there and made no attempt to get away. “Well,” trainer said, “when they are very young and much smaller we use the same size rope to tie them and, at that age, it’s enough to hold them. As they grow up, they are conditioned to believe they cannot break away. They believe the rope can still hold them, so they never try to break free.”&lt;br /&gt;&lt;br /&gt;The man was amazed. These animals could at any time break free from their bonds but because they believed they couldn’t, they were stuck right where they were.&lt;br /&gt;&lt;br /&gt;Like the elephants, how many of us go through life hanging onto a belief that we cannot do something, simply because we failed at it once before?&lt;br /&gt;&lt;br /&gt;Failure is part of learning; we should never give up the struggle in life.&lt;/span&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/1114397340027874219/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/elephant-rope.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/1114397340027874219'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/1114397340027874219'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/elephant-rope.html' title='The Elephant Rope'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-7091112650409680059</id><published>2012-04-03T23:38:00.001-07:00</published><updated>2012-04-18T02:25:54.072-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="TechNology"/><title type='text'>Java Threads</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
This post is the first of set of posts about&amp;nbsp;&lt;strong&gt;Java Concurrency&lt;/strong&gt;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
The concurrency is the fact to made several things at the same time using several threads.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
A thread, also called&amp;nbsp;&lt;strong&gt;Lightweight Process&lt;/strong&gt;, is treatment unity. Threads executes code in&amp;nbsp;&lt;strong&gt;parralel&lt;/strong&gt;&amp;nbsp;of each other threads currently running. When you’ve only one processor, there is a thread running at the same time of the others, you only have the impression of&amp;nbsp;&lt;strong&gt;concurrency&lt;/strong&gt;&amp;nbsp;(I don’t say it’s not useful, I say it’s different), but when you’ve&amp;nbsp;&lt;strong&gt;multiple processors&lt;/strong&gt;, you’ll see the power of multi threading. In this case, you can have your threads distributed on the processors of the computer.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
&lt;span id=&quot;more-528&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
In Java, a thread is an instance of the class java.lang.Thread. A Thread can be managed in one of these two ways :&lt;/div&gt;
&lt;ol style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Directly mapped to a native thread of the operating system. This is used when the operating system provide a preemptive threading system.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Managed by the virtual machine in a premptive way.&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
A preemptive system, is a system in which the threads are managed by a scheduler and can be interrupted at any time to give processor to an other thread. When you program, you doesn’t have to pay attention of which type of threads you use, the result will normally be the same. But you’ve to know that there can differences between operating systems.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
There is three very important concepts when doing concurrent programming :&lt;/div&gt;
&lt;ol style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Atomicity : An operation is said atomic when it cannot be interrupted. There is almost no atomic operations in Java, the only we’ve is the assignation a = 5, but a = b++ is not atomic. In some cases, you’ll have to make atomic some actions with synchronization, we’ll see later how to do that.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Visibility : This occurs when a thread must watch the actions of an other threads by example the termination of the thread. This also implies some kind of synchronization.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Order of execution : When you have normal program, all you lines of code run in the same order every time you launch the application. This is not the case when you make concurrent programming. You first instruction can followed by an instruction of the thread B ou by the first instruction. And that can change every time you launch the application.&amp;nbsp;&lt;strong&gt;The order of execution is not guaranteed !&lt;/strong&gt;&amp;nbsp;I will certainly repeat that sometimes, but that’s important to know.&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
We’ll see these concepts more deeply in the others parts of the set.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
Lets start introducing the Thread class in Java. You can create threads in two ways :&lt;/div&gt;
&lt;ol style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Extends&amp;nbsp;&lt;strong&gt;Thread&lt;/strong&gt;&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Implements&amp;nbsp;&lt;strong&gt;Runnable&lt;/strong&gt;&amp;nbsp;and pass an instance of your news class to the Thread constructor&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
The first solution isn’t a good solution because what you’re creating is not a new specialized thread, but severals instructions to run in a new Thread, namely a Runnable. Implementing Runnable is also better because Runnable is an interface and so, you can also extends a class and&amp;nbsp;implementing&amp;nbsp;Runnable, that’s useful in some cases.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
In my examples, I’ll always use the first way. So let’s declare our first Runnable :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; MyFirstRunnable &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Runnable&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    @Override
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; run&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;In a thread&quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
And use it to create a new Thread and start it :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt; thread &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; MyFirstRunnable&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
thread.&lt;span style=&quot;color: #006633;&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
The Thread will stopped when the end of the run() will be reached. You cannot force a thread to stop (there is stop() method, but deprecated), we’ll see later how to properly stop a thread.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
And now, what happens if we add a simple line of code to our program :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt; thread &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; MyFirstRunnable&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
thread.&lt;span style=&quot;color: #006633;&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;In the main Thread&quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
Can you predict the result of this code ? Nobody can’t, it’s not predictable, you can have :&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: #222222; font-size: 12px; line-height: 20px; text-align: -webkit-auto;&quot;&gt;In a thread
In the main Thread&lt;/pre&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
or&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: #222222; font-size: 12px; line-height: 20px; text-align: -webkit-auto;&quot;&gt;In the main Thread
In a thread&lt;/pre&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
And we cannot do better than that.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
You can use the Runnable several times :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: #003399;&quot;&gt;Runnable&lt;/span&gt; runnable &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; MyFirstRunnable&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;&amp;amp;&lt;/span&gt;lt&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #339933;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;runnable&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
Now, 25 threads are launched.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
You can also give names to Thread using the setName() method. You can get the name of the current thread using Thread.currentThread().getName(). Let’s do a little example :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; MySecondRunnable &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Runnable&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    @Override
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; run&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;I&#39;m running in thread %s &lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&quot;&lt;/span&gt;, &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentThread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;getName&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: #003399;&quot;&gt;Runnable&lt;/span&gt; runnable &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; MySecondRunnable&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;&amp;amp;&lt;/span&gt;lt&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #339933;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt; thread &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;runnable&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
    thread.&lt;span style=&quot;color: #006633;&quot;&gt;setName&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Thread &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; i&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
    thread.&lt;span style=&quot;color: #006633;&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
This is the best example to see that the other is unpredictable. Here are two executions on my machine :&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
1.&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: #222222; font-size: 12px; line-height: 20px; text-align: -webkit-auto;&quot;&gt;I&#39;m running in thread Thread 0
I&#39;m running in thread Thread 1
I&#39;m running in thread Thread 2
I&#39;m running in thread Thread 3
I&#39;m running in thread Thread 4
I&#39;m running in thread Thread 5
I&#39;m running in thread Thread 7
I&#39;m running in thread Thread 14
I&#39;m running in thread Thread 13
I&#39;m running in thread Thread 12
I&#39;m running in thread Thread 11
I&#39;m running in thread Thread 10
I&#39;m running in thread Thread 9
I&#39;m running in thread Thread 8
I&#39;m running in thread Thread 6
I&#39;m running in thread Thread 15
I&#39;m running in thread Thread 16
I&#39;m running in thread Thread 17
I&#39;m running in thread Thread 18
I&#39;m running in thread Thread 19
I&#39;m running in thread Thread 20
I&#39;m running in thread Thread 21
I&#39;m running in thread Thread 22
I&#39;m running in thread Thread 23
I&#39;m running in thread Thread 24&lt;/pre&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
2.&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: #222222; font-size: 12px; line-height: 20px; text-align: -webkit-auto;&quot;&gt;I&#39;m running in thread Thread 0
I&#39;m running in thread Thread 1
I&#39;m running in thread Thread 2
I&#39;m running in thread Thread 3
I&#39;m running in thread Thread 4
I&#39;m running in thread Thread 5
I&#39;m running in thread Thread 6
I&#39;m running in thread Thread 7
I&#39;m running in thread Thread 8
I&#39;m running in thread Thread 9
I&#39;m running in thread Thread 10
I&#39;m running in thread Thread 11
I&#39;m running in thread Thread 12
I&#39;m running in thread Thread 13
I&#39;m running in thread Thread 14
I&#39;m running in thread Thread 15
I&#39;m running in thread Thread 16
I&#39;m running in thread Thread 17
I&#39;m running in thread Thread 18
I&#39;m running in thread Thread 19
I&#39;m running in thread Thread 20
I&#39;m running in thread Thread 21
I&#39;m running in thread Thread 22
I&#39;m running in thread Thread 23
I&#39;m running in thread Thread 24&lt;/pre&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
Like you can see, the order the threads instructions are executed is not guaranteed at all.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
So here we are with the first part of this suite of articles about Java Concurrency. In the next post, we’ll see the operations you can make directly on threads (stopping, joining, sleeping, …).&lt;/div&gt;
&lt;div style=&quot;background-color: white; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px;&quot;&gt;
I hope you found that post interesting.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px;&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
we’ll see in this article what we can do to&amp;nbsp;&lt;strong&gt;manipulate Threads&lt;/strong&gt;.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
When we’ve Threads, we can make several operations on the Threads :&lt;/div&gt;
&lt;ul style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; list-style-type: square; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Make the current Thread&amp;nbsp;&lt;strong&gt;sleeping&lt;/strong&gt;&amp;nbsp;during x milliseconds&lt;/li&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;&lt;strong&gt;Wait&lt;/strong&gt;&amp;nbsp;for an other thread to complete&lt;/li&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Manage the&amp;nbsp;&lt;strong&gt;priorities&lt;/strong&gt;&amp;nbsp;of Threads and pause a thread to give an other thread the opportunity to run&lt;/li&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;&lt;strong&gt;Interrupt&lt;/strong&gt;&amp;nbsp;a thread&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
We’ll now see how to do all these things.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;span id=&quot;more-571&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
First and easier, we can make a thread sleeping for a certain number of milliseconds. To do that, the Thread class has a method sleep(long millis). But this method is static, so you can only make the current Thread sleeping. You cannot choose the thread you want to make sleeping, your only choice is the current Thread so :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
makes the current Thread sleep during 1000 milliseconds (1 second). But, you have to catch an exception, InterruptedException. This exception occurs if the sleeping thread is interrupted. So you have to do that :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 e.&lt;span style=&quot;color: #006633;&quot;&gt;printStackTrace&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
But this not the good way to manage the InterruptedException. We’ll see in one moment, how to deal with this exception.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
If you want more precision, you can use the overloaded version of sleep that takes the number of millis plus a certain number of nanos to sleep. The precision of this sleep depends on the system timers and clocks.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
For example, if you want to sleep 1000 milliseconds and 1000 nanoseconds (1 microsecond), you can do like that :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1000&lt;/span&gt;, &lt;span style=&quot;color: #cc66cc;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 e.&lt;span style=&quot;color: #006633;&quot;&gt;printStackTrace&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
Here is a little example to test that :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; SleepThread &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; main&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;]&lt;/span&gt; args&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Current time millis : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentTimeMillis&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
            e.&lt;span style=&quot;color: #006633;&quot;&gt;printStackTrace&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Current time millis : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentTimeMillis&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Nano time : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;nanoTime&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: #cc66cc;&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
            e.&lt;span style=&quot;color: #006633;&quot;&gt;printStackTrace&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Nano time : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;nanoTime&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
In my computer, this produce this result :&lt;/div&gt;
&lt;pre style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px;&quot;&gt;Current time millis : 1273959308480
Current time millis : 1273959309480
Nano time : 5878165216075
Nano time : 5878166730976&lt;/pre&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
You can see that the sleep of millis is very precise, but with nanos the result can vary a lot. And of course, the result depends of your computer, your operating system and your configuration.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
An other thing, you can do with Threads, is waiting for an other thread to die. For example, you can create five thread to compute sub result and wait for these 5 threads to finish to compute the final results based on the results of the five threads. To do that, you can use the join() method of the Thread class. This method is not static, so you can use it on any thread to wait for it to die. Like sleep() this method throws InterruptedException in the when the thread is interrupted during waiting for an other thread. So to wait on thread2, you just have to do that :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 thread2.&lt;span style=&quot;color: #006633;&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 e.&lt;span style=&quot;color: #006633;&quot;&gt;printStackTrace&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
That will make the current Thread waiting for thread2 to die. You can also add a timeout in millis, or millis + nanos, with the overloaded versions of join(), join(long millis) and join(long millis, int nanos). Here is little example that shows all that stuff :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; JoinThread &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; main&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;]&lt;/span&gt; args&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
  &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt; thread2 &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; WaitRunnable&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt; thread3 &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; WaitRunnable&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Current time millis : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentTimeMillis&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  thread2.&lt;span style=&quot;color: #006633;&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   thread2.&lt;span style=&quot;color: #006633;&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   e.&lt;span style=&quot;color: #006633;&quot;&gt;printStackTrace&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Current time millis : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentTimeMillis&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  thread3.&lt;span style=&quot;color: #006633;&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   thread3.&lt;span style=&quot;color: #006633;&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   e.&lt;span style=&quot;color: #006633;&quot;&gt;printStackTrace&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Current time millis : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentTimeMillis&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
 &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; WaitRunnable &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Runnable&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
  @Override
  &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; run&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    e.&lt;span style=&quot;color: #006633;&quot;&gt;printStackTrace&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
That produce this result on my computer :&lt;/div&gt;
&lt;pre style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px;&quot;&gt;Current time millis : 1274015478535
Current time millis : 1274015483538
Current time millis : 1274015484538&lt;/pre&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
You can see that the first join() wait 5 seconds for the other thread and when we set a timeout, we wait only 1 seconds and return from join method.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
When working with Threads, it’s also possible to change the priority of a Thread. In the Java Virtual Machine, the Thread scheduler, use a priority-based scheduling. So if a Thread enter in Runnable state with a higher priority than the running Thread, the new Thread will run and the current running thread will return to Runnable state and waits for its turn. But this behaviour is not guaranteed and is completely depending on the virtual machine you are working on. So, do not rely on thread priorities, just use them to improve efficiency of your program.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
Normally, the priority range of Threads is an integer from 0 to 10, but some virtual machine have lower or higher ranges. To know the range of priority, you can use constants of the Thread class :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; ThreadPriorityRange &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; main&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;]&lt;/span&gt; args&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
  &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Minimal priority : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;MIN_PRIORITY&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Maximal priority : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;MAX_PRIORITY&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Norm priority : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;NORM_PRIORITY&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
On my computer, I’ve the most current values :&lt;/div&gt;
&lt;pre style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px;&quot;&gt;Minimal priority : 1
Maximal priority : 10
Norm priority : 5&lt;/pre&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
To set the priority of a Thread, you can use the setPriority(int priority) method of the Thread class. If you enter a value greater than the maximal priority, the maximal value will be used. If you don’t specify a priority, the used priority, will be the priority of the current Thread.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
An other way to works with priority is the yield() method. This method is static, so this works on the current Thread. The purpose of this method is to make the Thread going to Runnable again and to give the opportunity to other threads to get their turn. But in practice, the behaviour of this method is not guaranteed. It can be implemented as a no-op on certain systems. It’s not easy to test that in practice, because the results can truly depends on your computer, virtual machine and operating system. It’s a good things to not use the priorities of Threads in practice.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
The last thing you can do with a Thread, is to interrupt it. In Java, you have no way to force a Thread to stop, if the Thread is not well-done, it can continue its execution infinitely. But you can interrupt a Thread with the interrupt() method. This method interrupt the thread, if the thread is sleeping or joining an other Thread, an InterruptedException is thrown. You have to know that if the thread was sleeping or joining, the interrupted status of the Thread will be cleared. Namely, the method isInterrupted() will return false. A little example to demonstrate that :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; InterruptThread &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; main&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;]&lt;/span&gt; args&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
  &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt; thread1 &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; WaitRunnable&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  thread1.&lt;span style=&quot;color: #006633;&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   e.&lt;span style=&quot;color: #006633;&quot;&gt;printStackTrace&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
  thread1.&lt;span style=&quot;color: #006633;&quot;&gt;interrupt&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
 &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; WaitRunnable &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Runnable&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
  @Override
  &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; run&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Current time millis : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentTimeMillis&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;The thread has been interrupted&quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;The thread is interrupted : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentThread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;isInterrupted&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Current time millis : &quot;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentTimeMillis&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
That produce that kind of result :&lt;/div&gt;
&lt;pre style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px;&quot;&gt;Current time millis : 1274017633151
The thread has been interrupted
The thread is interrupted : false
Current time millis : 1274017634151&lt;/pre&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
You can see that after one second, the second thread is interrupted and that the interrupted status has been set to false. If you are not sleeping, but making a lot of heavy actions, you can test for interrupt like that to make your thread correctly interrupts :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; InterruptableRunnable &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Runnable&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 @Override
 &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; run&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
  &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentThread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;isInterrupted&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;//Heavy operation&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
Now that you know how to interrupt a thread, you can imagine, that simply catch the InterruptedException is not enough to make your thread “interrupt safe”. Imagine that your thread something like that :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; UglyRunnable &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Runnable&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 @Override
 &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; run&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
  &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentThread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;isInterrupted&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;//Heavy operation&lt;/span&gt;
   &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    e.&lt;span style=&quot;color: #006633;&quot;&gt;printStackTrace&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
   &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;//Other operation&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
And now, an other thread want to interrupt your thread while your thread is sleeping. The sleep will be interrupted, but the interrupted status will be cleared so the loop will continue. A solution to make a better thread is to interrupt again the thread after an InterruptException :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; BetterRunnable &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Runnable&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
 @Override
 &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; run&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
  &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentThread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;isInterrupted&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;//Heavy operation&lt;/span&gt;
   &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;currentThread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;interrupt&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
   &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;//Other operation&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
With that code, the interrupted status will be restored and the loop will be stopped after interrupt. Depending on your code, you can also add a continue statement after the interrupt() to not make operations after interrupt. In some cases, you’ll also needs to make several if statements to test the interrupted status to do or not to do some operations.&lt;/div&gt;
&lt;div style=&quot;padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px;&quot;&gt;
So, we’ve now covered the main operations you can do on threads. I hope you found this article interesting.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
After learning how to&amp;nbsp;create threads&amp;nbsp;and&amp;nbsp;manipulate them, it’s time to go to most important things : synchronization.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
Synchronization is a way to make some code thread safe. A code that can be accessed by multiple threads must be made thread safe. Thread Safe describe some code that can be called from multiple threads without corrupting the state of the object or simply doing the thing the code must do in right order.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
For example, we can take this little class :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; Example &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; value &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;    
&amp;nbsp;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; getNextValue&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;return&lt;/span&gt; value&lt;span style=&quot;color: #339933;&quot;&gt;++;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
It’s really simple and works well with one thread, but absolutely not with multiple threads. An incrementation like this is not a simple action, but three actions :&lt;/div&gt;
&lt;ul style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; list-style-type: square; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Read the current value of “value”&lt;/li&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Add one to the current value&lt;/li&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Write that new value to “value”&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
Normally, if you have two threads invoking the getNextValue(), you can think that the first will get 1 and the next will get 2, but it is possible that the two threads get the value 1. Imagine this situation :&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
Thread 1 : read the value, get 0, add 1, so value = 1&lt;br /&gt;
Thread 2 : read the value, get 0, add 1, so value = 1&lt;br /&gt;
Thread 1 : write 1 to the field value and return 1&lt;br /&gt;
Thread 2 : write 1 to the field value and return 1&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
These situations come from what we call interleaving. Interleaving describe the possible situations of several threads executing some statements. Only for three operations and two threads, there is a lot of possible interleavings.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
So we must made the operations atomic to works with multiple threads. In Java, the first way to make that is to use a lock. All Java objects contains an intrinsic locks, we’ll use that lock to make methods or statement atomic. When a thread has a lock, no other thread can acquire it and must wait for the first thread to release the lock. To acquire the lock, you have to use the synchronized keyword to automatically acquire and release a lock for a code. You can add the synchronized keyword to a method to acquire the lock before invoking the method and release it after the method execution. You can refactor the getNextValue() method using the synchronized keyword :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; Example &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; value &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;    
&amp;nbsp;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;synchronized&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; getNextValue&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;return&lt;/span&gt; value&lt;span style=&quot;color: #339933;&quot;&gt;++;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
With that, you have the guarantee that only thread can execute the method at the same time. The used lock is the intrinsic lock of the instance. If the method is static, the used lock is the Class object of Example. If you have two methods with the synchronized keyword, only one method of the two will be executed at the same time because the same lock is used for the two methods. You can also write it using a synchronized block :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; Example &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; value &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; getNextValue&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;synchronized&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;return&lt;/span&gt; value&lt;span style=&quot;color: #339933;&quot;&gt;++;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
This is exactly the same as using the synchronized keyword on the method signature. Using synchronized blocks, you can choose the lock to block on. By example, if you don’t want to use the intrinsic lock of the current object but an other object, you can use an other object just as a lock :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; Example &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; value &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;final&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Object&lt;/span&gt; lock &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Object&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; getNextValue&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;synchronized&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;lock&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;return&lt;/span&gt; value&lt;span style=&quot;color: #339933;&quot;&gt;++;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
The result is the same but has one difference, the lock is internal to the object so no other code can use the lock. With complex classes, it not rare to use several locks to provide thread safety on the class.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
There is an other issue with multiple threads : the visibility of the variables. This seems when a change made by a thread is visible by an other thread. For performance improvements, the Java compiler and virtual machines can made some improvements using registers and cache. By default, you have no guarantee that a change made by a thread is visible to an other thread. To make a change visible to an other thread, you must use synchronized blocks to ensure visibility of the change. You must use synchronized blocks for the read and for the write of the shared values. You must make that for every read/write of a value shared between multiple threads.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
You can also use the volatile keyword on the field to ensure the visibility of read/write between multiple threads. The volatile keyword ensure only visibility, not atomicity. The synchronized blocks ensure visibility and atomicity. So you can use the volatile keyword on fields that doesn’t need atomicity (if you make only read and write to the field without depending on the current value of the field by example).&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
You can also note that this simple example can be solved using AtomicInteger, but that will be covered later in an other part of the posts.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
Pay attention that trying to solve thread safety on a problem can add new issues of deadlock. By example, if thread A owns the lock 1 and are waiting for the lock 2 and if lock 2 is acquired by thread B who waits on lock 1, there is a deadlock. Your program is dead. So you have to pay great attention to the locks.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
There is several rules that we must keep in mind when using locks :&lt;/div&gt;
&lt;ol style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Every mutable fields shared between multiple threads must be guarded with a lock or made volatile, if you only need visibility&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Synchronize only the operations that must synchronized, this improve the performances. But don’t synchronize too few operations. Try to keep the lock only for short operations.&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Always know which locks are acquired and when there are acquired and by which thread&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;An immutable object is always thread safe&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
We continue with the Java Concurrency thema with the semaphores. Semaphores is also a way to synchronize threads.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
Semaphores are a really simple concept, invented by the famous Dutch computer scientist Edsger Dijkstra. Basically a semaphore is a counter (integer) that allows a thread to get into a critical region if the value of the counter is greater than 0. If it’s the case, the counter is decremented by one otherwise, the thread is waiting until it can go. And when the thread go away from the critical region, the counter is incremented by one to allow one more thread to pass the critical region. A semaphore is created with a certain value for its counter.&amp;nbsp;So, you can execute two actions on a semaphore P and V.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
By example, if you have a critical that cannot be executed concurrently, you can use a semaphore :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;sem mutex &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; sem&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;
&amp;nbsp;
P&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;mutex&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;//Critical region&lt;/span&gt;
V&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;mutex&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
So you must always call by yourself the P operation before the critical region and V after it. We call a mutex (mutual exclusion) a semaphore with a value of one. So only one thread can enter the region guarded by the semaphore. This is the most used semaphore. The other use of semaphore is to guard a set of resources like database connections or a data pool.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
In Java, a semaphore is created using the java.util.concurrent.Semaphore class. You can create easily :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;Semaphore mutex &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; Semaphore&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
Semaphore available &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; Semaphore&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
The P and V operations are represented using the acquire and release methods. The method acquire can be interrupted if the thread is interrupted. There is an ininterruptible version with the method acquireUninterruptibly(). There is also a third version with the tryAcquire method. This method acquire a permit only if there is one permit available, otherwise, this method return false directly. All the waiting methods have also an overloaded version with a timeout. You can also acquire several permits at once using the permits argument to the different versions of acquire methods.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
A little example with a mutex using the same example as the previous post on Java concurrency :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; Example &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; value &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;final&lt;/span&gt; Semaphore mutex &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; Semaphore&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; getNextValue&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;InterruptedException&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
            mutex.&lt;span style=&quot;color: #006633;&quot;&gt;acquire&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;return&lt;/span&gt; value&lt;span style=&quot;color: #339933;&quot;&gt;++;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;finally&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
            mutex.&lt;span style=&quot;color: #006633;&quot;&gt;release&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
For more informations about Semaphore in Java, the best is to consult&amp;nbsp;the Javadoc of the Semaphore class.&lt;/div&gt;
&lt;div style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
To conclude, semaphores are a powerful ways to solve concurrency problems, but this is not adapted to all problems. If you need only mutual exclusion,&amp;nbsp;synchronized blocks&amp;nbsp;are a better solutions. The problems with semaphores is that you can forget to call the release method and that can cause deadlock sometimes difficult to find.&lt;/div&gt;
&lt;ol style=&quot;color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/7091112650409680059/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/java-threads.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7091112650409680059'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/7091112650409680059'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/java-threads.html' title='Java Threads'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-2432973658264310506</id><published>2012-04-03T23:32:00.002-07:00</published><updated>2012-04-03T23:33:22.163-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Social Service"/><title type='text'>Love Story 1</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
A boy proposed his girlfriend for marriage,&lt;br /&gt;
&lt;br /&gt;
Girl: Tell me… who do you love most in this world..?&lt;br /&gt;
&lt;br /&gt;
Boy: You, of course!&lt;br /&gt;
Girl: what am I to you?&lt;br /&gt;
&lt;br /&gt;
Boy: The boy thought for a moment and looked intently in her eyes and said&lt;br /&gt;
“u r missing part of my heart”&lt;br /&gt;
&lt;br /&gt;
She smiled,and she accepted his proposal.&lt;br /&gt;
&lt;br /&gt;
After their wedding, the couple had a sweet and happy life for a while.However,t he youthful couple began to drift apart due to the busy schedule of life and the never-ending worries of daily problems…their life became difficult.&lt;br /&gt;
:&lt;br /&gt;
&lt;br /&gt;
All the challenges posed by the harsh realities of life began to drift away deir dreams and love for each other…The couple began to have more quarrels and each quarrel became make their relation more worst.&lt;br /&gt;
&lt;br /&gt;
One day, after the quarrel, the girl ran out of the house…. At the opposite side of the road, she shouted,”You don’t love me…!”&lt;br /&gt;
&lt;br /&gt;
The boy hated her childishness and out of impulse, said,”May be, it was a mistake for us to be together..!&lt;br /&gt;
&lt;br /&gt;
You were never da missing part of my …!”&lt;br /&gt;
&lt;br /&gt;
Suddenly, she turned quiet and stood there for a long while…. He regretted what he said but words spoken cant be taken back.&lt;br /&gt;
&lt;br /&gt;
With tears in her eyes, she went home to pack her things and Before leaving the house,she left a note for him:&lt;br /&gt;
“If I’m really not da missing part of ur love, let me go..n search for sum1 whu is…It is less painful this way… let us go on our separate ways and search for our own partners…”&lt;br /&gt;
Five years went by….&lt;br /&gt;
&lt;br /&gt;
He never remarried but he had tried to find out about her life indirectly….. . She had left the country and was living her dreams.&lt;br /&gt;
&lt;br /&gt;
He use to regret on what he did but never tried to bring her back.. In the dark and lonely night, he lit his cigarette and felt the lingering ache in his heart.He couldn’t bring himself to admit that he was missing her…..&lt;br /&gt;
&lt;br /&gt;
One day, they finally met…. At the airport, He was going away on a business trip .He saw her,She was standing there alone, with just the security door separating them.&lt;br /&gt;
&lt;br /&gt;
She smiled at him gently.&lt;br /&gt;
&lt;br /&gt;
Boy: How are you ?&lt;br /&gt;
&lt;br /&gt;
Girl: I’m fine. How about you… Have you found your heart’s missing part…?&lt;br /&gt;
&lt;br /&gt;
Boy: No.&lt;br /&gt;
&lt;br /&gt;
Girl: I’ll be flying to New York in the next flight.&lt;br /&gt;
&lt;br /&gt;
Boy: I’ll be back in 2 weeks time. Give me a call when you are back…You know my number…. Nothing has changed.&lt;br /&gt;
&lt;br /&gt;
With a smile, she turned around and waved good-bye..&lt;br /&gt;
“Good-bye . . .”&lt;br /&gt;
&lt;br /&gt;
Same evening he heard of a plane crash which was headed to New York.He tried to know about her and found that,She died.&lt;br /&gt;
&lt;br /&gt;
Midnight….. Once again, he lit his cigarette….. And like before, he felt the lingering ache in his heart…. He finally knew, she was that missing part that he had carelessly broken . . .&lt;br /&gt;
&lt;br /&gt;
Sometimes, people say things out of moments of fury…..&lt;br /&gt;
&lt;br /&gt;
We take out our 99% frustrations at our loved ones…. And even though we know that we ought to”think twice and act wisely”, we actually don’t do it. Things happen each day, many of which are beyond our control.&lt;br /&gt;
&lt;br /&gt;
Try not to hurt ur loved once,because a moment of anger could b a lifetime punishment.. ? — with&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/2432973658264310506/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/love-story-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/2432973658264310506'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/2432973658264310506'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/love-story-1.html' title='Love Story 1'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4097251789842337972.post-3336314676417271923</id><published>2012-04-03T23:20:00.002-07:00</published><updated>2012-04-18T02:25:54.067-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="TechNology"/><title type='text'>Java excecutor</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
Let’s start with a new post in the Java concurrency series.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
This time we’ll learn how to start cleanly new threads and to manage thread pools. In Java, if you have a Runnable like this :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: #003399;&quot;&gt;Runnable&lt;/span&gt; runnable &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Runnable&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;void&lt;/span&gt; run&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
      &lt;span style=&quot;color: #003399;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;out&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&quot;Run&quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
You can easily run it in a new thread :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;runnable&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
This is very simple and clean, but what if you’ve several long running tasks that you want to load in parralel and then wait for the completion of all the tasks, it’s a little bit harder to code and if you want to get the return value of all the tasks it becomes really difficult to keep a good code. But like for almost any problems, Java has a solution for you, the Executors. This simple class allows you to create thread pools and thread factories.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
&lt;span id=&quot;more-1063&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
A thread pool is represented by an instance of the class ExecutorService. With an ExecutorService, you can submit task that will be completed in the future. Here are the type of thread pools you can create with the Executors class :&lt;/div&gt;
&lt;ul style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; list-style-type: square; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 15px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;&lt;strong&gt;Single Thread Executor&lt;/strong&gt;&amp;nbsp;: A thread pool with only one thread. So all the submitted task will be executed sequentially. Method :&amp;nbsp;&lt;em&gt;Executors.newSingleThreadExecutor()&lt;/em&gt;&lt;/li&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;&lt;strong&gt;Cached Thread Pool&lt;/strong&gt;&amp;nbsp;: A thread pool that create as many threads it needs to execute the task in parralel. The old available threads will be reused for the new tasks. If a thread is not used during 60 seconds, it will be terminated and removed from the pool. Method :&amp;nbsp;&lt;em&gt;Executors.newCachedThreadPool()&lt;/em&gt;&lt;/li&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;&lt;strong&gt;Fixed Thread Pool&lt;/strong&gt;&amp;nbsp;: A thread pool with a fixed number of threads. If a thread is not available for the task, the task is put in queue waiting for an other task to ends. Method :&amp;nbsp;&lt;em&gt;Executors.newFixedThreadPool()&lt;/em&gt;&lt;/li&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;&lt;strong&gt;Scheduled Thread Pool&lt;/strong&gt;&amp;nbsp;: A thread pool made to schedule future task. Method :&lt;em&gt;Executors.newScheduledThreadPool()&lt;/em&gt;&lt;/li&gt;
&lt;li style=&quot;list-style-type: square; margin-bottom: 0px; margin-left: 20px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;&lt;strong&gt;Single Thread Scheduled Pool&lt;/strong&gt;&amp;nbsp;: A thread pool with only one thread to schedule future task. Method :&lt;em&gt;Executors.newSingleThreadScheduledExecutor()&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
Once you have a thread pool, you can submit task to it using the different submit methods. You can submit a Runnable or a Callableto the thread pool. The method return a Future representing the future state of the task. If you submitted a Runnable, the Future object return null once the task finished.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
By example, if you have this Callable :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;final&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;class&lt;/span&gt; StringTask &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;implements&lt;/span&gt; Callable&lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt;String&lt;span style=&quot;color: #339933;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #003399;&quot;&gt;String&lt;/span&gt; call&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
      &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;//Long operations&lt;/span&gt;
&amp;nbsp;
      &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;&quot;Run&quot;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
If you want to execute that task 10 times using 4 threads, you can use that code :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;ExecutorService pool &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; Executors.&lt;span style=&quot;color: #006633;&quot;&gt;newFixedThreadPool&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #339933;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   pool.&lt;span style=&quot;color: #006633;&quot;&gt;submit&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; StringTask&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
But you must shutdown the thread pool in order to terminate all the threads of the pool :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;pool.&lt;span style=&quot;color: #006633;&quot;&gt;shutdown&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
If you don’t do that, the JVM risk to not shutdown because there is still threads not terminated. You can also force the shutdown of the pool using shutdownNow, with that the currently running tasks will be interrupted and the tasks not started will not be started at all.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
But with that example, you cannot get the result of the task. So let’s get the Future objects of the tasks :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;ExecutorService pool &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; Executors.&lt;span style=&quot;color: #006633;&quot;&gt;newFixedThreadPool&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
List&lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt;Future&lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt;String&lt;span style=&quot;color: #339933;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; futures &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; ArrayList&lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt;Future&lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt;String&lt;span style=&quot;color: #339933;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #339933;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   futures.&lt;span style=&quot;color: #006633;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;pool.&lt;span style=&quot;color: #006633;&quot;&gt;submit&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; StringTask&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;Future&lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt;String&lt;span style=&quot;color: #339933;&quot;&gt;&amp;gt;&lt;/span&gt; future &lt;span style=&quot;color: #339933;&quot;&gt;:&lt;/span&gt; futures&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   &lt;span style=&quot;color: #003399;&quot;&gt;String&lt;/span&gt; result &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; future.&lt;span style=&quot;color: #006633;&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;//Compute the result&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
pool.&lt;span style=&quot;color: #006633;&quot;&gt;shutdown&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
But this code is a bit complicated. And there is a disadvantage. If the first task takes a long time to compute and all the other tasks ends before the first, the current thread cannot compute the result before the first task ends. Once again, Java has the solution for you, CompletionService.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
A CompletionService is a service that make easier to wait for result of submitted task to an executor. The implementation is ExecutorCompletionService who’s based on an ExecutorService to work. So let’s try :&lt;/div&gt;
&lt;div class=&quot;wp_syntax&quot; style=&quot;background-color: white; border-bottom-color: silver; border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: silver; border-left-style: solid; border-left-width: 1px; border-right-color: silver; border-right-style: solid; border-right-width: 1px; border-top-color: silver; border-top-style: solid; border-top-width: 1px; color: #110000; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; margin-bottom: 1.5em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: hidden; text-align: -webkit-auto; width: 610px;&quot;&gt;
&lt;div class=&quot;code&quot; style=&quot;padding-bottom: 2px; padding-left: 4px; padding-right: 4px; padding-top: 2px; vertical-align: top;&quot;&gt;
&lt;pre class=&quot;java&quot; style=&quot;clear: none; float: none; line-height: 1.333; overflow-x: visible; overflow-y: visible; width: auto;&quot;&gt;ExecutorService threadPool &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; Executors.&lt;span style=&quot;color: #006633;&quot;&gt;newFixedThreadPool&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
CompletionService&lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt;String&lt;span style=&quot;color: #339933;&quot;&gt;&amp;gt;&lt;/span&gt; pool &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; ExecutorCompletionService&lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt;String&lt;span style=&quot;color: #339933;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;threadPool&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #339933;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   pool.&lt;span style=&quot;color: #006633;&quot;&gt;submit&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;new&lt;/span&gt; StringTask&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #000066; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #339933;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
   &lt;span style=&quot;color: #003399;&quot;&gt;String&lt;/span&gt; result &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; pool.&lt;span style=&quot;color: #006633;&quot;&gt;take&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;.&lt;span style=&quot;color: #006633;&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;//Compute the result&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
&amp;nbsp;
threadPool.&lt;span style=&quot;color: #006633;&quot;&gt;shutdown&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
With that, you have the result in the order they are completed and you don’t have to keep a collection of Future.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
Here we are, you have the tools in hand to launch tasks in parralel using performing thread pools. Using Executors, ExecutorService and CompletionService you can create complex algorithm using several taks. With that tools, it’s really easy to change the number of threads performing in parralel or adding more tasks without changing a lot of code.&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #222222; font-family: Arial, Tahoma, Verdana; font-size: 12px; line-height: 20px; padding-bottom: 10px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;
I hope that this post will help you to write better concurrent code.&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swamy085.blogspot.com/feeds/3336314676417271923/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swamy085.blogspot.com/2012/04/java-excecutor.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/3336314676417271923'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4097251789842337972/posts/default/3336314676417271923'/><link rel='alternate' type='text/html' href='http://swamy085.blogspot.com/2012/04/java-excecutor.html' title='Java excecutor'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/03507558517487305095</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>