Home  >  Article  >  Java  >  Java underlying technology application: how to implement multi-threaded programming and concurrency safety

Java underlying technology application: how to implement multi-threaded programming and concurrency safety

王林
王林Original
2023-11-08 16:24:31468browse

Java underlying technology application: how to implement multi-threaded programming and concurrency safety

"Java underlying technology application: how to implement multi-threaded programming and concurrency security"

In today's software development field, multi-thread programming and concurrency security are very important topic of. Especially in Java development, we often need to deal with multi-thread concurrency. However, achieving multi-threaded programming and concurrency safety is not an easy task. This article will introduce the application of Java's underlying technology and explore how to use specific code examples to achieve multi-threaded programming and concurrency safety.

First, let us understand multi-threaded programming in Java. In Java, we can create threads by inheriting the Thread class or implementing the Runnable interface. The following is an example of using the inherited Thread class:

class MyThread extends Thread {
    public void run() {
        System.out.println("This is a thread created by extending Thread class.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
    }
}

In addition, we can also create threads by implementing the Runnable interface, as shown below:

class MyRunnable implements Runnable {
    public void run() {
        System.out.println("This is a thread created by implementing Runnable interface.");
    }
}

public class Main {
    public static void main(String[] args) {
        Thread thread = new Thread(new MyRunnable());
        thread.start();
    }
}

Both of the above methods can be created. Threads, but the way to implement the Runnable interface is more flexible, because Java only supports single inheritance. If a class already has a parent class, it can no longer inherit the Thread class, and implementing the Runnable interface will not be subject to such restrictions.

Next, let’s talk about how to achieve concurrency safety. In multi-threaded programming, race conditions are prone to occur due to multiple threads accessing shared resources at the same time. In order to ensure the safety of multi-threaded access to shared resources, we can usually use the synchronized keyword or Lock interface to achieve this. The following is an example of using the synchronized keyword:

class Counter {
    private int count = 0;

    public synchronized void increment() {
        count++;
    }

    public synchronized void decrement() {
        count--;
    }

    public synchronized int getCount() {
        return count;
    }
}

public class Main {
    public static void main(String[] args) {
        Counter counter = new Counter();

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    counter.increment();
                }
            }).start();
        }

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    counter.decrement();
                }
            }).start();
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Final count: " + counter.getCount());
    }
}

In the above example, the Counter class ensures the atomicity of the increment(), decrement() and getCount() methods through the synchronized keyword, thereby avoiding Inconsistency caused by concurrent access by multiple threads.

In addition to using the synchronized keyword, we can also use the Lock interface to achieve concurrency safety. The following is an example of using the Lock interface:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Counter {
    private int count = 0;
    private Lock lock = new ReentrantLock();

    public void increment() {
        lock.lock();
        try {
            count++;
        } finally {
            lock.unlock();
        }
    }

    public void decrement() {
        lock.lock();
        try {
            count--;
        } finally {
            lock.unlock();
        }
    }

    public int getCount() {
        lock.lock();
        try {
            return count;
        } finally {
            lock.unlock();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Counter counter = new Counter();

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    counter.increment();
                }
            }).start();
        }

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    counter.decrement();
                }
            }).start();
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Final count: " + counter.getCount());
    }
}

In the above example, we use ReentrantLock to create a reentrant mutex, ensuring the concurrency safety of the count variable.

Through the above example, we can see how to implement multi-threaded programming and concurrency safety in Java. At the same time, we also learned how to use the synchronized keyword and Lock interface to ensure the security of multi-threaded access to shared resources. Of course, in actual development, appropriate methods must be selected to achieve multi-thread programming and concurrency safety based on specific business needs.

In short, multi-threaded programming and concurrency safety are important issues in Java development. I hope the content of this article will be helpful to you. It is hoped that readers can flexibly use Java's underlying technology in actual development to write efficient and safe multi-threaded programs.

The above is the detailed content of Java underlying technology application: how to implement multi-threaded programming and concurrency safety. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn