I am trying to learn Volatile keyword in Multi Threading and I came across this statement: *Volatile is preferred in cases when one thread reads and writes a shared variable and other threads just read the same. Whereas if there are more than 2 threads performing read and write both on the shared variable then only volatile is not enough, you need to have synchronisation as well*. I am aware of the volatility concept and synchronisation, so can someone please provide an example relating to the above statement?

Member Avatar
Member Avatar
+0 forum 1

I am trying to understand the SynchronizedMap and I ran the below code. I get the below Output with an exception. According to my understanding the exception is caused when the get() methods are trying to access the syncmap when a thread is still executing a write on the map or Thread is in the Sleep state. Is my understanding correct or am I missing something ? class MapHelper1 implements Runnable { Map<String, Integer> map; public MapHelper1(Map<String, Integer> map) { this.map = map; new Thread(this, "MapHelper1").start(); } public void run() { map.put("One", 1); try { System.out.println("MapHelper1 sleeping"); Thread.sleep(100); } catch …

Member Avatar
Member Avatar
+0 forum 1

I am trying to run the below concurrency code: import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class TestThread { public static void main(final String[] arguments) throws InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); try { executor.submit(new Task()); System.out.println("Shutdown executor"); executor.shutdown(); executor.awaitTermination(5, TimeUnit.SECONDS); } catch (InterruptedException e) { System.err.println("tasks interrupted"); } finally { if (!executor.isTerminated()) { System.err.println("cancel non-finished tasks"); } executor.shutdownNow(); System.out.println("shutdown finished"); } } static class Task implements Runnable { public void run() { try { int duration = 6; System.out.println("Running Task!"); TimeUnit.SECONDS.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } } } } and the output is : Shutdown executor Running …

Member Avatar
Member Avatar
+0 forum 3

Hello, I am running a software on a Linux machine, I am synchronizing time via default ntpd configurations available in Linux (/etc/ntp.conf). I am connecting to the customer NTP server. Now i was requested to tell which synchronization type is used on my machine (Frequency Synchronization/ Time Synchronization or Time-of-day Synchronization) Now after some googling it seems there is two types for NTP synchronization: 1- Phase Synchronization. 2- Frequency Synchronization. But how can I decide which of them is used by Linux? I am already connected the customer NTP server and they told that their NTP doesn't support Time Synchronization, …

Member Avatar
Member Avatar
+0 forum 4

Hey, I have been reading Java oracle tutorials. I read this thing. Suppose, for example, class MsLunch has two instance fields, c1 and c2, that are never used together. All updates of these fields must be synchronized, but there's no reason to prevent an update of c1 from being interleaved with an update of c2 — and doing so reduces concurrency by creating unnecessary blocking. Instead of using synchronized methods or otherwise using the lock associated with this, we create two objects solely to provide locks. public class MsLunch { private long c1 = 0; private long c2 = 0; …

Member Avatar
Member Avatar
+0 forum 1

Hello to all first look this code class Square1 implements Runnable{ int a; public void run(){ a=10; } } class Square2 implements Runnable{ Square1 ob=new Square1(); public void run(){ System.out.println("Square Of this number is = "+(ob.a*ob.a)); } } class SquareThread{ public static void main(String args[]){ Thread t1=new Thread(new Square1()); Thread t2=new Thread(new Square2()); t1.start(); try{ t1.join(); }catch(InterruptedException e){ e.getStackTrace(); } t2.start(); } } i need to find the square but answer is 0 so tell me how to find the square

Member Avatar
Member Avatar
+0 forum 16

Hello all, My master master synchronization working properly but from yesterday after 4pm my sinking get stopped. My uplink working properly but downlink not working.I failed to found any issues. Please tell me why such master master get stopped and any solution for it.

Member Avatar
Member Avatar
+0 forum 1

In the following example two threads share int counter. The increment_thread thread increases counter by one . The decrement_thread thread decreases counter by one . I use semaphore to control access to counter instead of making the two methods synchronized. Is this the proper way of using semaphore? When should we use synchronized methods and when a semaphore?(we don't want more than one thread to access counter at the same time). If both are proper , which is preferable? import java.util.concurrent.Semaphore; public class SemaphoreMultithreadingExample { static int counter = 0; static Semaphore semaphore = new Semaphore(1); public static void incrementCounter(){ …

Member Avatar
Member Avatar
+0 forum 2

Hi freinds i have developed a sync service by using MS sync SDK. It was working fine few days before but its not working. It does not showing any error in log. Please friends let me know if you are having useful suggestios to make the thing working. Thanks in advance.

Member Avatar
Member Avatar
+0 forum 1

Hi I am new to this forum and i was just wondering if the following scenario is implementable. Suppose a thread is reading a file and dividing it into chunks of data(lets say in the form of arraylist, where each line corresponds to a value in arraylist). Each chunk of data is associated with one thread and that thread is writing that data into a file. i want that as soon as ane line has been written,the lock should open and any thread can occupy the lock. is it possible? if yes can you provide a brief overview on how …

Member Avatar
Member Avatar
+0 forum 1

I was looking at the possible solution to synchronise a bounded buffer ( like queue) using condition variables and think that we probably need two condition variables to correctly implement the synchronisation, one condition varaible will have a queue where the consumer thread(s) will be waiting to see if the buffer is not empty and another will producer (threads) will be waiting to check if the buffers are not full?

Member Avatar
Member Avatar
+0 forum 1

Hi, I'm trying to synchronize two processes but I get a deadlock that I can't manage to solve. To be more specific, my program has to do the following: 1. Parent creates a process and waits using sigsuspend. 2. Child creates a process. 3. Grandchild performs an execvp and gets some information using snmp and sends this information to child using a pipe. 4. Child gets grandchild's output and parses the useful information, placing it in a shared memory space with parent. 5. Child signals parent to wake up. Child repeats from 2. 6. When parent wakes up, writes the …

Member Avatar
Member Avatar
+0 forum 2

Here when I execute the program, the output I'm getting on all the tries is 342 (naturally first 34 prints together then 2 seconds later 2 prints). My doubts is that is the order of start() [x.start() followed by y.start()] important in order of execution? Shouldn't the output be 234? P.S. If I alter the start() to y.start() followed by x.start(), then also the output is 342. Help please. Thanks.

Member Avatar
Member Avatar
+0 forum 3

The End.