Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Volatile Keyword:</strong></p> <p>If you are working with multi-threaded programming, the volatile keyword will be more useful. When multiple threads using the same variable, each thread will have its own copy of the local cache for that variable. </p> <p>So, when it’s updating the value, it is actually updated in the local cache not in the main variable memory. The other thread which is using the same variable doesn’t know anything about the values changed by the another thread. </p> <p>To avoid this problem, if you declare a variable as volatile, then it will not be stored in the local cache. Whenever thread are updating the values, it is updated to the main memory. So, other threads can access the updated value. </p> <p><strong>Volatile Example:</strong></p> <pre><code>class ExampleThread extends Thread { private volatile int testValue; public ExampleThread(String str){ super(str); } public void run() { for (int i = 0; i &amp;lt; 3; i++) { try { System.out.println(getName() + " : "+i); if (getName().equals("Thread 1")) { testValue = 10; } if (getName().equals("Thread 2")) { System.out.println( "Test Value : " + testValue); } Thread.sleep(1000); } catch (InterruptedException exception) { exception.printStackTrace(); } } } } public class VolatileExample { public static void main(String args[]) { new ExampleThread("Thread 1").start(); new ExampleThread("Thread 2").start(); } } </code></pre> <p><strong>Synchronized Keyword:</strong></p> <p>The synchronized keyword may be applied to a statement block or to a method. The synchronized keyword provides the protection for the crucial sections that are required only to be executed by a single thread once at a time. The synchronized keyword avoids a critical code from being executed by more than one thread at a time. </p> <p>It restricts other threads to concurrently access a resource. If the synchronized keyword is applied to a static method, as we will show it with a class having a method SyncStaticMethod through an example below, the entire class get locked while the method under execution and control of a one thread at a time.</p> <p>When the synchronized keyword is applied to an instance method, as we have done with SyncMethod in the example given below, the instance get locked while being accessed and under execution and control of a one thread at a time. </p> <p>When the synchronized keyword is applied to an object, then that object is locked though the code block associated with it get executed by one thread at at time.</p> <p><strong>Synchronized Example:</strong></p> <pre><code>public class Class1{ public synchronized static String SyncStaticMethod(){ } public synchronized String SyncMethod(){ } { public class Class2{ Object Obj; public String Method2(){ &lt;statements&gt; synchronized (Obj){ &lt;statements affecting Obj&gt; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload