Questions tagged [thread-synchronization]

In a multi-threaded environment thread synchronization is used to coordinate access to shared resources such as file handles, network connections, and memory

In a multi-threaded environment thread synchronization is used to coordinate access to shared resources such as file handles, network connections, and memory

References

565 questions
55
votes
4 answers

What is progress and bounded waiting in critical section?

I was reading Critical Section Problem from Operating System Concepts by Peter B. Galvin. According to it 1) Progress is : If no process is executing in its critical section and some processes wish to enter their critical sections, then only those…
47
votes
3 answers

Two threads executing synchronized block simultaneously

Below is the code where a Thread enters a synchronized block, waits for 5 seconds and then exits. I have started two Thread instances simultaneously. The expectation was one of the threads will own the lock on the synchronized object, and the other…
39
votes
3 answers

Does Thread.yield() do anything if we have enough processors to service all threads?

If we are in a situation with two running threads on a machine with two processors and we call Thread.yield() within one of those threads, does it stand to reason that nothing will happen (the scheduler will essentially ignore the request) because…
Dave
  • 17,420
  • 96
  • 300
  • 582
38
votes
3 answers

Java memory model: volatile variables and happens-before

I'd like to clarify how happens-before relation works with volatile variables. Let we have the following variables: public static int i, iDst, vDst; public static volatile int v; and thread A: i = 1; v = 2; and thread B: vDst = v; iDst = i; Are…
38
votes
5 answers

What is the point of making the singleton instance volatile while using double lock?

private volatile static Singleton uniqueInstance In a singleton when using double lock method for synchronization why is the single instance declared as volatile ? Can I achieve the same functionality without declaring it as volatile ?
31
votes
3 answers

Spring @Async limit number of threads

My question is very similar to this one : @Async prevent a thread to continue until other thread have finished Basically i need run ~ hundreds of computations in more threads. I want to run only some amount of parallel threads e.g. 5 threads with 5…
Martin V.
  • 2,966
  • 5
  • 28
  • 45
27
votes
2 answers

compare and swap vs test and set

Could someone explain to me the working and differences of above operations in multi-threading?
Tony The Lion
  • 57,181
  • 57
  • 223
  • 390
25
votes
3 answers

example code to show how java synchronized block works

I am learning java multi-threading, I found it's hard to understand how synchronized block works: synchronized(Object o){ // do something } please give some example code that can show me the Object o is blocked. As how I understand…
CaiNiaoCoder
  • 3,089
  • 9
  • 49
  • 78
20
votes
3 answers

Synchronization mechanism for an observable object

Let's imagine we have to synchronize read/write access to shared resources. Multiple threads will access that resource both in read and writing (most of times for reading, sometimes for writing). Let's assume also that each write will always trigger…
Adriano Repetti
  • 60,141
  • 17
  • 127
  • 190
18
votes
8 answers

How Synchronization works in Java?

I have a doubt regarding Java Synchronization . I want to know if I have three Synchronized methods in my class and a thread acquires lock in one synchronized method other two will be locked ? I am asking this question because I am confused with the…
Raj
  • 2,161
  • 10
  • 31
  • 51
16
votes
2 answers

understanding of Volatile.Read/Write

I'm trying to understand the C# Volatile class. As i read: The Volatile.Write method forces the value in location to be written to at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to…
No Idea For Name
  • 10,935
  • 10
  • 37
  • 61
15
votes
6 answers

What is the difference between Thread.join and Synchronized?

I am confused when to use Thread.join() and when to use synchronization in multi threading application. According to me, both of them block or wait for the execution to be done by some other thread. This example has to output 10 A's , 10 B's & 10…
JPG
  • 1,078
  • 5
  • 24
  • 52
12
votes
5 answers

Simultaneous mutable access to arbitrary indices of a large vector that are guaranteed to be disjoint

Context I have a case where multiple threads must update objects stored in a shared vector. However, the vector is very large, and the number of elements to update is relatively small. Problem In a minimal example, the set of elements to update can…
Thierry
  • 889
  • 7
  • 18
12
votes
2 answers

std::timed_mutex::try_lock* fail spuriously

By try_lock*, I take to mean try_lock(), try_lock_for(), and try_lock_until(). According to cppreference, all three methods may just fail spuriously. Following is quoted from the description for try_lock_for() As with try_lock(), this function is…
Lingxi
  • 13,248
  • 1
  • 32
  • 74
11
votes
9 answers

Difference Between Monitor & Lock?

What's the difference between a monitor and a lock? If a lock is simply an implementation of mutual exclusion, then is a monitor simply a way of making use of the waiting time inbetween method executions? A good explanation would be really helpful…
Goober
  • 12,636
  • 49
  • 119
  • 188
1
2 3
37 38