Thread Synchronization

Discussion in 'Programming General' started by Prism, Nov 19, 2014.

Thread Synchronization
  1. Unread #1 - Nov 19, 2014 at 11:20 PM
  2. Prism
    Joined:
    Nov 19, 2014
    Posts:
    6
    Referrals:
    0
    Sythe Gold:
    0

    Prism Newcomer

    Thread Synchronization

    Thread Synchronization

    Problem with running concurrent tasks

    Lets start from the very beginning, suppose you have 2 threads (thread A and B) that both perform a similar function, changing a previously declared integer's value. Now both threads run continuously, changing the same integer's value repeatedly. Now here is what's likely to happen:

    - Thread A retrieves the integer
    - Thread A applies the effect (increases the integer by 5)
    - Before thread A returns the new value back to the integer variable, thread B acquires the original value (which is 0)
    - Thread A returns the new value (which is 5)
    - Thread B applies the effect (increases the integer by 10)
    - Thread B returns the new value (which again is 10)

    Notice something? Thread A's effect got overplaced, therefore the resulting integer's value is actually 10 rather than 15. This is known as thread interference, also known as a more general topic, memory consistency errors. Memory consistency errors occur when different threads have inconsistent views of what should be the same data. Meaning when 2 or more threads access the same method at the same time which changes the value in an existing variable,

    Example:
    Code:
    private int i;
    
    public void incrementBy(int i) {
        this.i += i;
    }
    Code:
    Thread a = new Thread(() -> { incrementBy(5); });
    
    
    Thread b = new Thread(() -> { incrementBy(10); });
    Solution

    Java's ability to synchronize methods is extremely powerful and useful. It allows threads to wait for each other to complete a specfic task. It states that a thread must first retrieve the objects monitor lock (also know as intrinsic lock). So before a thread can access a specific object's method, it must first obtain its monitor lock. A thread is said to own the object's lock while it has it.

    Example:

    Code:
    /**
     *
     * Before any thread can access this method, it must first obtain this object's monitor lock.
     * Once it has applied its effects, it will return the lock and call the object's notify method for the waiting thread to wake up.
     * It may call the object's notifyAll() method if multiple threads are waiting.
     *
     */
    public synchronized void incrementBy(int i) {
        this.i += i;
    }
    Please note

    Synchronization can lead to thread starvation, also known as a more general topic, thread liveness. Thread starvation is basically.... TODO

    Terminology
    Concurrent software: multiple computations occurring anonymously.

    Additional classes provided by the API regarding this:
    - https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html
    - https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/package-summary.html

    If you have any questions, feel free to ask me! :)
     
  3. Unread #2 - Dec 12, 2014 at 1:16 PM
  4. VirusRS
    Joined:
    Dec 9, 2014
    Posts:
    73
    Referrals:
    0
    Sythe Gold:
    0

    VirusRS Member
    Banned

    Thread Synchronization

    interesting.

    Where did you start this?
     
< Why do my components revert to storyboard sizes in iOS8.1? | I have an great idea for APP, but no skills to put in use! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site