<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Parolski.com &#187; java</title>
	<atom:link href="http://www.parolski.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.parolski.com</link>
	<description>Faith, Solaris, and Chicken Korma, by Anton Parol</description>
	<lastBuildDate>Fri, 14 Oct 2011 11:16:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Half a half: Part 2</title>
		<link>http://www.parolski.com/2008/12/13/half-a-half-part-2/</link>
		<comments>http://www.parolski.com/2008/12/13/half-a-half-part-2/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 21:12:26 +0000</pubDate>
		<dc:creator>Anton Parol</dc:creator>
				<category><![CDATA[computing]]></category>
		<category><![CDATA[BigDecimal]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[problem]]></category>

		<guid isPermaLink="false">http://www.parolski.com/?p=158</guid>
		<description><![CDATA[Ten days ago we had a look at Java&#8217;s ability to deal with numbers which had alot of decimal places. Liam pointed out that having to loop through a variety of operations is longer than performing a more efficient operation once. With Liams magical formula, we get the following Java, with no loop: 1 2 [...]]]></description>
			<content:encoded><![CDATA[<p>Ten days ago we had a look at Java&#8217;s ability to deal with numbers which had alot of decimal places. Liam pointed out that having to loop through a variety of operations is longer than performing a more efficient operation once.</p>
<p>With Liams magical formula, we get the following Java, with no loop:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.math.BigDecimal</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> The_proper_half <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Integer</span> times_to_loop<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * @param args the number of times you want to run the loop
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        calculate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> times_to_loop<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//The Number we want to half</span>
        <span style="color: #003399;">BigDecimal</span> number_to_half <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;10&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//The result of doing the half</span>
        <span style="color: #003399;">BigDecimal</span> the_half <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//The number we want to divide by</span>
        <span style="color: #003399;">BigDecimal</span> divider <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        the_half <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>number_to_half.<span style="color: #006633;">divide</span><span style="color: #009900;">&#40;</span>divider.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>times_to_loop<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The last number has &quot;</span> <span style="color: #339933;">+</span>
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span> number_to_half.<span style="color: #006633;">subtract</span><span style="color: #009900;">&#40;</span>the_half<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; decimal places: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>number_to_half.<span style="color: #006633;">subtract</span><span style="color: #009900;">&#40;</span>the_half<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, we only perform the opertation once (line 23), but admitadly, to get the correct number at the end, we still do the subtraction at the end!</p>
<p>Regarding optimisation, there are huge benefits. It turns out that the old way if hugely costly for thousands of iterations. These are the results for 10000 iterations, the old way:</p>
<pre>anton@anton-laptop classes $ time java Half_of_a_half 10000 &gt; /dev/null

real    5m22.714s
user    5m21.320s
sys    0m1.052s</pre>
<p>The results for the new way are much better:</p>
<pre>anton@anton-laptop classes $ time java The_proper_half 10000 &gt; /dev/null

real    0m1.019s
user    0m1.248s
sys    0m0.052s</pre>
<p>So it went from over five minutes, down to just a second. That was just by changing from a loop a single statement. Thats pretty cool stuff!</p>
<p>I have to admit here, the difficult bit is probably not the code. Its the abillity to see how a problem may be translated from one way of expressing it to another. Its that bit of genius thats really done the work here. Credit to Liam for the genius!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parolski.com/2008/12/13/half-a-half-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Half a number, half it again and add it to the first half.</title>
		<link>http://www.parolski.com/2008/12/03/half-a-number-half-it-again-and-add-it-to-the-first-half/</link>
		<comments>http://www.parolski.com/2008/12/03/half-a-number-half-it-again-and-add-it-to-the-first-half/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 20:37:17 +0000</pubDate>
		<dc:creator>Anton Parol</dc:creator>
				<category><![CDATA[computing]]></category>
		<category><![CDATA[BigDecimal]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.parolski.com/?p=136</guid>
		<description><![CDATA[This is a common riddle, so if you&#8217;ve heard it, don&#8217;t shout out the answer (people might think your strange for shouting at the internet&#8230;.) Take a number. Then, half that number. Then half that half and add it to the first half. Before that gives people a headache, lets do a little example: Lets [...]]]></description>
			<content:encoded><![CDATA[<p>This is a common riddle, so if you&#8217;ve heard it, don&#8217;t shout out the answer (people might think your strange for shouting at the internet&#8230;.)</p>
<p>Take a number. Then, half that number. Then half that half and add it to the first half. Before that gives people a headache, lets do a little example:</p>
<p>Lets use 10 . Then half of ten gives us 5. Then, half five (2.5) and add it to the first half, 5. This gives us 7.5</p>
<p>Continue to half the last half, adding it to the sum of the previous halves. Phew, I hope thats explained properly.i.e. half two point five, and add it the the 7.5 we already have, which give us 8.25</p>
<p>Okay, so the question is this. If we keeping adding on the halves, will we ever reach 10 again? Well, I like Java, so lets get all geeky and model the problem. For this, the double and float data types won&#8217;t give us enough decimal places. For this, we&#8217;ll need: math.BigDecimal . The Big Decimal. Sounds ominous!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.math.BigDecimal</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Half_of_a_half <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Integer</span> times_to_loop<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * @param args the number of times you want to run the loop
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        calculate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> times_to_loop<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//The Number we want to half</span>
        <span style="color: #003399;">BigDecimal</span> number_to_half <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;10&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//The result of doing the half</span>
        <span style="color: #003399;">BigDecimal</span> the_half <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//The number we want to divide by</span>
        <span style="color: #003399;">BigDecimal</span> divider <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> times_to_loop<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            number_to_half <span style="color: #339933;">=</span> number_to_half.<span style="color: #006633;">divide</span><span style="color: #009900;">&#40;</span>divider<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            the_half <span style="color: #339933;">=</span> the_half.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>number_to_half<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>the_half<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The last number has &quot;</span> <span style="color: #339933;">+</span>
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span> the_half.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; decimal places&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This code takes a single parameter on the command line: the number of times to half by</p>
<p>e.g.<br />
<code><br />
anton@anton-laptop classes $ java Half_of_a_half 10<br />
5<br />
7.5<br />
8.75<br />
9.375<br />
9.6875<br />
9.84375<br />
9.921875<br />
9.9609375<br />
9.98046875<br />
9.990234375<br />
The last number has 9 decimal places<br />
</code><br />
So as we can see, the decimal places just get long and longer. The additions are never big enough to get us close enough to the original number. Problem solved!</p>
<p>The fun bit is realising the significance of the calculation, or rather, the speed with which it was done. Performing 1000 divisions and additions takes less that 0.2 of a second! Maybe thats not so fast by todays computing ability, but by human standards thats pretty extreme.<br />
<code><br />
anton@anton-laptop classes $ time java Half_of_a_half 1000 &gt; /dev/null<br />
</code><br />
<code><br />
real    0m1.387s<br />
user    0m1.916s<br />
sys    0m0.036s<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.parolski.com/2008/12/03/half-a-number-half-it-again-and-add-it-to-the-first-half/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using synchronized methods in java</title>
		<link>http://www.parolski.com/2008/10/04/using-synchronized-methods-in-java/</link>
		<comments>http://www.parolski.com/2008/10/04/using-synchronized-methods-in-java/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 21:14:07 +0000</pubDate>
		<dc:creator>Anton Parol</dc:creator>
				<category><![CDATA[computing]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[synchronized methods]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://www.parolski.com/?p=116</guid>
		<description><![CDATA[This post has long code segments in it, so I&#8217;ll explain what it all does at the top. Then at the end, just copy paste into your favourite editor/IDE to play with it. So the title kind of gives it away, but the concept behind it was quite interesting. Imagine you have some kind of [...]]]></description>
			<content:encoded><![CDATA[<p>This post has long code segments in it, so I&#8217;ll explain what it all does at the top. Then at the end, just copy paste into your favourite editor/IDE to play with it.</p>
<p>So the title kind of gives it away, but the concept behind it was quite interesting. Imagine you have some kind of resource, like a network connection, a file etc, which is needs to be accessed by many threads, but only one thread at a time. Using<a href="http://java.sun.com/docs/books/tutorial/essential/concurrency/syncmeth.html"> synchronized methods</a> achieves this. Its especially handy where you might have hundreds of threads which need to modify the same resource, but should do it, one at a time. This code can do that (although its not perfect by far)</p>
<p>In this example, we have a padlock, which we can open , and close. Of course, we can&#8217;t open it whilst its being closed, and vica versa. You could just run the openLock and closeLock methods one after the other, but using threads gives us some bonus features. For example, we might want to open the padlock, get half way through, and then realise we want to pause, leaving the rest of the work for later. Or maybe we want to get halfway through closing the padlock, and then change our mind and open it (i.e. without fully closing it)</p>
<p>So heres the code, enjoy!<br />
The padlock:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">workingWithSynchronizedThreads</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.logging.Level</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.logging.Logger</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * This class represents a simple padlock. You know, like the kind you use to
 * lock up your bike. I use it as an example of how we can make fields in
 * java , then try and access them through different threads. Using syncronised
 * methods, we will prevent the lock from being opened whilst were still trying
 * to close it.
 * @author anton
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> padlock <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Constructor object to make a new padlock
     * @param isThePadlockOpen Whether the padlock is open or closed
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> padlock<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span> isThePadlockOpen<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">isThePadlockOpen</span> <span style="color: #339933;">=</span> isThePadlockOpen<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * This field tells us whether or not the lock is open or closed (true for
     * open, false for locked)
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isThePadlockOpen<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * This method simply sets the padlock to be closed by setting isOpen to false
     * Closing the lock takes two seconds (hence the sleep statements) ).
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> closeLock<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            isThePadlockOpen <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I'm closing the lock&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tellMeIfThisLockIsOpen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I couldn't close the lock&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>padlock.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>Level.<span style="color: #006633;">SEVERE</span>, <span style="color: #000066; font-weight: bold;">null</span>, ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * This method simply sets the padlock to be open by setting isOpen to true.
     * Opening a lock takes two seconds (hence the sleep statements ).
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> openLock<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            isThePadlockOpen <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I'm opening the lock&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tellMeIfThisLockIsOpen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I couldn't open the lock&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>padlock.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>Level.<span style="color: #006633;">SEVERE</span>, <span style="color: #000066; font-weight: bold;">null</span>, ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Lets us know if our padlock object is open or closed
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> tellMeIfThisLockIsOpen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isThePadlockOpen <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The lock is open!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isThePadlockOpen <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The lock is closed!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Something is seriously wrong!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The closeLockThread:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">workingWithSynchronizedThreads</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 *
 * @author anton
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> closeLockThread <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Thread</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> padlock padlock<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> closeLockThread<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> padlock<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">padlock</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>padlock<span style="color: #009900;">&#41;</span> padlock<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        padlock.<span style="color: #006633;">closeLock</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The openLockThread:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">workingWithSynchronizedThreads</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 *
 * @author anton
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> openLockThread <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Thread</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> padlock padlock<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> openLockThread<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> padlock<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">padlock</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>padlock<span style="color: #009900;">&#41;</span> padlock<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        padlock.<span style="color: #006633;">openLock</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Okay, less mess with some threads!!!:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">workingWithSynchronizedThreads</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 *
 * @author anton
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> messAroundWithSomePadlocks <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * @param args the command line arguments
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        padlock myLock <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> padlock<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        closeLockThread iWannaCloseTheLock <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> closeLockThread<span style="color: #009900;">&#40;</span>myLock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        openLockThread iWannaOpenTheLock <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> openLockThread<span style="color: #009900;">&#40;</span>myLock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        iWannaCloseTheLock.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        iWannaOpenTheLock.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The things that need attention are:</p>
<ol>
<li>Do we need to do that casting from padlock to padlock in the openLock and closeLock thread classes?</li>
<li>Is it ok/understandable to make a field a question? (public boolean isThePadlockOpen;) ?</li>
</ol>
<p>The inspiration for this work came from looking at some code were working on in my department, but the learning was really done looking at the<a href="http://java.sun.com/docs/books/tutorial/index.html"> Java Tutorials</a>, which are ace!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parolski.com/2008/10/04/using-synchronized-methods-in-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>a new kind of extreme programming</title>
		<link>http://www.parolski.com/2007/12/09/a-new-kind-of-extreme-programming/</link>
		<comments>http://www.parolski.com/2007/12/09/a-new-kind-of-extreme-programming/#comments</comments>
		<pubDate>Sun, 09 Dec 2007 05:04:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[university]]></category>
		<category><![CDATA[extreme]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[korma]]></category>

		<guid isPermaLink="false">http://www.parolski.com/2007/12/09/a-new-kind-of-extreme-programming/</guid>
		<description><![CDATA[XP is regarded as a good technique for producing clean effective code. I however think that pulling yet another all nighter is also extreme. I guess the logical conclusion would be to code with a laptop whilst skydiving from a plane&#8230;&#8230;a bit like extreme ironing Once this coursework is handed in, my house mate Darren [...]]]></description>
			<content:encoded><![CDATA[<p>XP is regarded as a good technique for producing clean effective code. I however think that pulling yet another all nighter is also extreme. I guess the logical conclusion would be to code with a laptop whilst skydiving from a plane&#8230;&#8230;a bit like <a href="http://en.wikipedia.org/wiki/Extreme_ironing">extreme ironing</a></p>
<p>Once this coursework is handed in, my house mate Darren will have been  awake, coding, for 51 hours including doing a  part time job. 51 hours!!!!! Thats got to be the new definition of XP!</p>
<p>The upside is that in that time, I managed to implement SMTP in a Java using sockets. nice.</p>
<p>A word of warning: eating chicken korma during a code marathon is likely to knock you out for two hours&#8230;&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parolski.com/2007/12/09/a-new-kind-of-extreme-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Netbeans 6.0 rc2 &#8211; What a joy to use!</title>
		<link>http://www.parolski.com/2007/12/03/netbeans-60-rc2-what-a-joy-to-use/</link>
		<comments>http://www.parolski.com/2007/12/03/netbeans-60-rc2-what-a-joy-to-use/#comments</comments>
		<pubDate>Mon, 03 Dec 2007 16:52:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[university]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[netbeans]]></category>

		<guid isPermaLink="false">http://www.parolski.com/?p=8</guid>
		<description><![CDATA[This post is for the benefit of anyone who had the displeasure of using Netbeans 5.0 that came with OpenSuse 10.3 . The first thing to say is that netbeans does take a little while to load, which is a shame really because it looks like they missed the opportunity to show off javas threading [...]]]></description>
			<content:encoded><![CDATA[<p>This post is for the benefit of anyone who had the displeasure of using Netbeans 5.0 that came with OpenSuse 10.3 .</p>
<p>The first thing to say is that netbeans does take a little while to load, which is a shame really because it looks like they missed the opportunity to show off javas threading abilities. The other downer is that it eats alot of ram <img src='http://www.parolski.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  you&#8217;ll want at least a gig to run netbeans and happily get on with your other desktop tasks (music, word processing etc)</p>
<p>After the load, the fun stuff!</p>
<p>The best thing to say is that the web container integration works really well. It comes with glassfish as a standard, but you can add many different different verions of tomcat ontop if you prefer that. It makes deploying your servlets or jsps as easy as hiting F6 (which runs you main project, remember to select your project as main first!) . You can change where netbeans deploys the servlet by right clicking on the project and going to properties. Once in properties you can go down to run and select your server <img src='http://www.parolski.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   Of course, you will need to have the server running first!</p>
<p>IMO the best update for the editor is the code highlighting, particularly the <a href="http://wiki.netbeans.org/wiki/view/Java_EditorUsersGuide#section-Java_EditorUsersGuide-HowToUseInstantRename">instant rename</a>. You can basically out the caret in the middle of a word and hit &lt;ctrl&gt; + R     The editor then selects every other instance of that word in the file and changes it as you change the currently selected one accordingly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parolski.com/2007/12/03/netbeans-60-rc2-what-a-joy-to-use/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

