Singleton: Difference between revisions

Content added Content deleted
No edit summary
Line 694: Line 694:
Value = 1
Value = 1
yes</lang>
yes</lang>

=={{header|NetRexx}}==
Uses a static field to avoid synchronization problems and the ''flawed'' &quot;double-checked locking&quot; idiom in JVMs. See [http://www.ibm.com/developerworks/java/library/j-dcl/index.html www.ibm.com/developerworks/java/library/j-dcl/index.html] for a detailed explanation.
<lang NetRexx>/* NetRexx */
options replace format comments java crossref symbols binary

import java.util.random

class RCSingleton public

method main(args = String[]) public static
RCSingleton.Testcase.main(args)
return

-- ---------------------------------------------------------------------------
class RCSingleton.Instance public

properties private static
_instance = Instance()

properties private
_refCount = int
_random = Random

method Instance() private
this._refCount = 0
this._random = Random()
return

method getInstance public static returns RCSingleton.Instance
return _instance

method getRandom public returns Random
return _random

method addRef public protect
_refCount = _refCount + 1
return

method release public protect
if _refCount > 0 then
_refCount = _refCount - 1
return

method getRefCount public protect returns int
return _refCount

-- ---------------------------------------------------------------------------
class RCSingleton.Testcase public implements Runnable

properties private
_instance = RCSingleton.Instance

method run public
say threadInfo'|-'
thud = Thread.currentThread
_instance = RCSingleton.Instance.getInstance
thud.yield
_instance.addRef
say threadInfo'|'_instance.getRefCount
thud.yield
do
thud.sleep(_instance.getRandom.nextInt(1000))
catch ex = InterruptedException
ex.printStackTrace
end
_instance.release
say threadInfo'|'_instance.getRefCount
return

method main(args = String[]) public static
threads = [ Thread -
Thread(Testcase()), Thread(Testcase()), Thread(Testcase()), -
Thread(Testcase()), Thread(Testcase()), Thread(Testcase()) ]
say threadInfo'|-'
mn = Testcase()
mn._instance = RCSingleton.Instance.getInstance
say mn.threadInfo'|'mn._instance.getRefCount
mn._instance.addRef
say mn.threadInfo'|'mn._instance.getRefCount
do
loop tr over threads
(Thread tr).start
end tr
Thread.sleep(400)
catch ex = InterruptedException
ex.printStackTrace
end
mn._instance.release
say mn.threadInfo'|'mn._instance.getRefCount
return

method threadInfo public static returns String
trd = Thread.currentThread
tid = trd.getId
hc = trd.hashCode
info = Rexx(trd.getName).left(16, '_')':' -
|| Rexx(Long.toString(tid)).right(10, 0)':' -
|| '@'Rexx(Integer.toHexString(hc)).right(8, 0)
return info

</lang>

'''output'''
<pre style="height:30ex;overflow:scroll">
main____________:0000000001:@035a8767|-
main____________:0000000001:@035a8767|0
main____________:0000000001:@035a8767|1
Thread-1________:0000000010:@22998b08|-
Thread-1________:0000000010:@22998b08|2
Thread-2________:0000000011:@7a6d084b|-
Thread-2________:0000000011:@7a6d084b|3
Thread-3________:0000000012:@2352544e|-
Thread-4________:0000000013:@457471e0|-
Thread-5________:0000000014:@7ecec0c5|-
Thread-6________:0000000015:@3dac2f9c|-
Thread-3________:0000000012:@2352544e|4
Thread-4________:0000000013:@457471e0|5
Thread-5________:0000000014:@7ecec0c5|6
Thread-6________:0000000015:@3dac2f9c|7
Thread-5________:0000000014:@7ecec0c5|6
main____________:0000000001:@035a8767|5
Thread-3________:0000000012:@2352544e|4
Thread-1________:0000000010:@22998b08|3
Thread-6________:0000000015:@3dac2f9c|2
Thread-2________:0000000011:@7a6d084b|1
Thread-4________:0000000013:@457471e0|0
</pre>


=={{header|Objeck}}==
=={{header|Objeck}}==