Java geek

Related reading:

Java Concurrent programming (1) Knowledge map Java concurrent programming (3) Visibility Java concurrent programming (4) Sequential Java concurrent programming (5) Introduction to Creating threads Java concurrent programming (6) Synchronized usage Java Concurrency programming Introduction (7) Easy to understand wait and Notify and use scenarios Java concurrency programming Introduction (8) Thread lifecycle Java concurrency programming Introduction (9) Deadlock and deadlock bit Java concurrency programming Introduction (10) lock optimization Introduction to Concurrent Programming in Java (11) Flow limiting scenarios and Spring Flow Limiting scenarios Introduction to Concurrent programming in Java (12) Producer and Consumer Patterns – Introduction to Concurrent programming in Java with code templates (13) Read/write lock and cache templates (14) CountDownLatch application scenarios Introduction to Concurrent Programming (CyclicBarrier) Introduction to Concurrent programming (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier) Introduction to Concurrent programming in Java (CyclicBarrier Java Concurrent programming Introduction (19) Asynchronous task scheduling tool CompleteFeature Java concurrent programming Introduction (20) Common locking scenarios and locking tools


One, a quiz

Are the following two classes thread-safe in concurrent cases?

public class Counter {
    private int count;
    
    public void increase(a) {
        count = count + 1;
    }
    
    public int getCount(a) {
      returncount; }}public class Basket {
    //Vector is a thread-safe collection
    Vector<Fruit> v = new Vector<Fruit>();

    public void put(Fruit fruit) {
        v.add(fruit);
    }

    public Fruit tackOut(a) throws Exception {
        if (v.size() > 0) {
            return v.remove(0);
        } else {
            throw new Exception("There is no fruit in basket."); }}}Copy the code

Answer: Neither of these classes is thread-safe in the case of concurrency.

Thread safety and atomicity

Thread-safe: A class is thread-safe if it consistently behaves correctly when accessed by multiple threads.

Atomicity: The ability of one or more operations to execute without interruption on the CPU is called atomicity.

Neither class is thread-safe because it does not guarantee that the operation will behave correctly due to atomicity.

1. In class Counter, the following code actually executes three instructions:

count = count + 1;
Copy the code

In the case of multi-thread concurrency, the calculation result will be incorrect due to thread switching.

Note: The picture refers to Java concurrent programming practice

2. The following code in the class is not atomic and may cause a null pointer exception to be thrown as expected:

if (v.size() > 0) {
    return v.remove(0);
}
Copy the code

Third, correct

Atomicity can be fixed by locking, as follows:

public class Counter {

    private int count;
    
    public synchronized void increase(a) {
        count = count + 1;
    }
    
    /** Atomic operations without synchronization keyword */
    public int getCount(a) {
      returncount; }}public class Basket {

    Vector<Fruit> v = new Vector<Fruit>();

    /** Atomic operations without synchronization keyword */
    public void put(Fruit fruit) {
        v.add(fruit);
    }

    /** synchronized */
    public synchronized Fruit tackOut(a) throws Exception {
        if (v.size() > 0) {
            return v.remove(0);
        } else {
            throw new Exception("There is no fruit in basket."); }}}Copy the code

end.


<– Read the mark, left like!