tag:blogger.com,1999:blog-32922877418510079502024-03-13T07:38:01.035-07:00Russian Programmer's BlogAnonymoushttp://www.blogger.com/profile/16405950889183577138noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-3292287741851007950.post-28416799938084385142015-09-05T04:45:00.000-07:002015-09-05T04:45:26.713-07:00Sublime Text regex detecting multiline tagsIf you want to make a multiline search in sublime, for example for the following text:
</br>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDlyWPxjYP1w2eoZAookNNLIRK-wcaqg3NNkblrHfBe_QTCZz4BVDdZkCoLIgaiJs5fWhDA79czzNLGM-LHXOaBg8YLVMa0KGW1i4_2-S9rfsWtcDqfvXWWkXtKPbu0F4ACVT851KPxo8V/s320/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> [sometag] here is more text
it spans more than one line [/sometag]
[sometag]this is a bit of text[/sometag]
</code></pre>
</br>
Use following regexp pattern:
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDlyWPxjYP1w2eoZAookNNLIRK-wcaqg3NNkblrHfBe_QTCZz4BVDdZkCoLIgaiJs5fWhDA79czzNLGM-LHXOaBg8YLVMa0KGW1i4_2-S9rfsWtcDqfvXWWkXtKPbu0F4ACVT851KPxo8V/s320/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> (?s)\[sometag\](.*?)\[\/sometag\]
</code></pre>
<a href="https://regex101.com/r/gI0xC3/1">example</a>Anonymoushttp://www.blogger.com/profile/16405950889183577138noreply@blogger.com0tag:blogger.com,1999:blog-3292287741851007950.post-25186260691329006492015-08-24T04:29:00.002-07:002015-08-24T05:21:32.520-07:00DCL (Double Checked Locking) is not working in Java<span style="font-family: Arial, Helvetica, sans-serif;">It was confusing for me, but today I learned that DCL Pattern is not working in Java.<br />Take a look:</span><br />
<h5 id="code4" style="background-color: white; border: 0px; font-family: Arial, sans-serif; font-size: 1.166em !important; margin: 5px 0px 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
Double-checked locking example</h5>
<div>
<div>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDlyWPxjYP1w2eoZAookNNLIRK-wcaqg3NNkblrHfBe_QTCZz4BVDdZkCoLIgaiJs5fWhDA79czzNLGM-LHXOaBg8YLVMa0KGW1i4_2-S9rfsWtcDqfvXWWkXtKPbu0F4ACVT851KPxo8V/s320/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> public static Singleton
getInstance() {
if (instance == null) {
synchronized(Singleton.class) {
//1
if (instance == null)
//2 instance = new Singleton();
//3
}
}
return instance;
}
</code></pre>
<span style="background-color: white; color: #222222; font-family: Arial, sans-serif; line-height: 1.5em;">The theory behind double-checked locking is that the second check at //2 makes it impossible for two different </span><code style="border-image-outset: initial; border-image-repeat: initial; border-image-slice: initial; border-image-source: initial; border-image-width: initial; border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Singleton</code><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; line-height: 1.5em;"> objects to be created. Consider the following sequence of events:</span></div>
</div>
<ol style="background-color: white; border: 0px; color: #333333; font-family: Arial, sans-serif; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px 0px 0px 20px; vertical-align: baseline;">
<li style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Thread 1 enters the <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">getInstance()</code> method.</li>
<li style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Thread 1 enters the <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">synchronized</code> block at //1 because <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">instance</code> is <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">null</code>.</li>
<li style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Thread 1 is preempted by thread 2.</li>
<li style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Thread 2 enters the <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">getInstance()</code> method.</li>
<li style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Thread 2 attempts to acquire the lock at //1 because <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">instance</code> is still <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">null</code>. However, because thread 1 holds the lock, thread 2 blocks at //1.</li>
<li style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Thread 2 is preempted by thread 1.</li>
<li style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Thread 1 executes and because instance is still <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">null</code> at //2, creates a <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Singleton</code> object and assigns its reference to <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">instance</code>.</li>
<li style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Thread 1 exits the <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">synchronized</code> block and returns instance from the <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">getInstance()</code> method.</li>
<li style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Thread 1 is preempted by thread 2.</li>
<li style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Thread 2 acquires the lock at //1 and checks to see if <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">instance</code> is <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">null</code>.</li>
<li style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Because <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">instance</code> is non-<code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">null</code>, a second <code style="border: 0px; color: rgb(0, 0, 0) !important; font-family: 'Andale Mono', 'Lucida Console', Monaco, Liberation, fixed, monospace; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Singleton</code> object is not created and the one created by thread 1 is returned.</li>
</ol>
<div style="background-color: white; border: 0px; color: #222222; font-family: Arial, sans-serif; line-height: 1.5em; outline: 0px; padding: 6px 0px; vertical-align: baseline;">
The theory behind double-checked locking is perfect. Unfortunately, reality is entirely different. The problem with double-checked locking is that there is no guarantee it will work on single or multi-processor machines.</div>
<div style="background-color: white; border: 0px; outline: 0px; padding: 6px 0px; vertical-align: baseline;">
<div style="color: #222222; font-family: Arial, sans-serif; line-height: 1.5em;">
The issue of the failure of double-checked locking is not due to implementation bugs in JVMs but to the current Java platform memory model. The memory model allows what is known as "out-of-order writes" and is a prime reason why this idiom fails. The problem with this code is that the variable instance can become non-null before the body of the Singleton constructor executes.</div>
<span style="color: #222222; font-family: Arial, sans-serif;">Source: <span style="line-height: 24px;"><a href="http://www.ibm.com/developerworks/java/library/j-dcl/index.html">http://www.ibm.com/developerworks/java/library/j-dcl/index.html</a></span></span><br />
<span style="color: #222222; font-family: Arial, sans-serif;"><br /></span>
<h3><span style="color: #222222; font-family: Arial, sans-serif;">Instead of DCL you can use:</span></h3></br>
<h4>
<span style="color: #222222; font-family: Arial, sans-serif;">1. You don't need lazy initializing</span></h4>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDlyWPxjYP1w2eoZAookNNLIRK-wcaqg3NNkblrHfBe_QTCZz4BVDdZkCoLIgaiJs5fWhDA79czzNLGM-LHXOaBg8YLVMa0KGW1i4_2-S9rfsWtcDqfvXWWkXtKPbu0F4ACVT851KPxo8V/s320/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> public class Singleton1 {
private static final Singleton1 instance = new Singleton1();
public static Singleton1 getInstance() {
return instance;
}
}
</code></pre>
<br />
<h4>
<span style="color: #222222; font-family: Arial, sans-serif;">2. You need lazy initializing and you can ignore synchronization expenses</span></h4>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDlyWPxjYP1w2eoZAookNNLIRK-wcaqg3NNkblrHfBe_QTCZz4BVDdZkCoLIgaiJs5fWhDA79czzNLGM-LHXOaBg8YLVMa0KGW1i4_2-S9rfsWtcDqfvXWWkXtKPbu0F4ACVT851KPxo8V/s320/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> public class Singleton2 {
private static final Singleton2 instance;
public synchronized static Singleton2 getInstance() {
if (instance == null)
instance = new Singleton2();
return instance;
}
}
</code></pre>
<span style="color: #222222; font-family: Arial, Helvetica, sans-serif;">In modern JVM these expenses are minimal.</span><br />
<h4>
</br>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: #222222;">3. </span><span style="color: #222222;">You need lazy initializing and you can't ignore synchronization expenses</span><span style="color: #222222;"> </span></span></h4>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDlyWPxjYP1w2eoZAookNNLIRK-wcaqg3NNkblrHfBe_QTCZz4BVDdZkCoLIgaiJs5fWhDA79czzNLGM-LHXOaBg8YLVMa0KGW1i4_2-S9rfsWtcDqfvXWWkXtKPbu0F4ACVT851KPxo8V/s320/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> public class Singleton3 {
private static class Instance {
static final Singleton3 value = new Singleton3();
}
public static Singleton3 getInstance() {
return Instance.value;
}
}
</code></pre>
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-sizing: content-box !important; color: #333333; float: none !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; height: auto !important; left: auto !important; line-height: 20px; margin: 0px !important; min-height: auto !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: nowrap !important; width: auto !important;">
<br /></div>
<div class="line number9 index8 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-sizing: content-box !important; color: #333333; float: none !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 14px; height: auto !important; left: auto !important; line-height: 20px; margin: 0px !important; min-height: auto !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: nowrap !important; width: auto !important;">
<code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-sizing: content-box !important; color: rgb(0, 0, 0) !important; float: none !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; height: auto !important; left: auto !important; margin: 0px !important; min-height: auto !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">In that case static inner class will be loaded when getInstance() method will be called.</code></div>
</div>Anonymoushttp://www.blogger.com/profile/16405950889183577138noreply@blogger.com0tag:blogger.com,1999:blog-3292287741851007950.post-83685279452504720692015-08-24T04:22:00.002-07:002015-08-24T04:22:39.389-07:00Hello!Hello!
My name is Boris. I'm from Saint-Petersburg, Russia. And I'm a programmer. Welcome to my blog!Anonymoushttp://www.blogger.com/profile/16405950889183577138noreply@blogger.com0