Java development: How to perform multi-threaded programming and thread safety, specific code examples are needed
In Java development, multi-threaded programming is a very important and common task . Multi-threading can take full advantage of multi-core CPUs and improve program execution efficiency. However, multi-threaded programming also brings some challenges, one of which is thread safety. This article explains how to do multithreaded programming and thread safety, and provides specific code examples.
1. Multi-threaded programming
First, let’s look at the way to inherit the Thread class:
public class MyThread extends Thread { public void run() { // 线程执行的代码 } } // 在主线程中创建并启动线程 public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); }
Next, let’s look at the way to implement the Runnable interface:
public class MyRunnable implements Runnable { public void run() { // 线程执行的代码 } } // 在主线程中创建并启动线程 public static void main(String[] args) { MyRunnable runnable = new MyRunnable(); Thread thread = new Thread(runnable); thread.start(); }
First, let’s look at the use of the synchronized keyword:
public class MyThread extends Thread { private static int count = 0; public synchronized void run() { for (int i = 0; i < 1000; i++) { count++; } } } // 在主线程中创建并启动多个线程 public static void main(String[] args) { MyThread thread1 = new MyThread(); MyThread thread2 = new MyThread(); thread1.start(); thread2.start(); // 等待两个线程执行完毕 try { thread1.join(); thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Count: " + MyThread.count); }
Next, let’s look at the use of the Lock interface:
public class MyThread implements Runnable { private Lock lock = new ReentrantLock(); private static int count = 0; public void run() { lock.lock(); try { for (int i = 0; i < 1000; i++) { count++; } } finally { lock.unlock(); } } } // 在主线程中创建并启动多个线程 public static void main(String[] args) { MyThread runnable = new MyThread(); Thread thread1 = new Thread(runnable); Thread thread2 = new Thread(runnable); thread1.start(); thread2.start(); // 等待两个线程执行完毕 try { thread1.join(); thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Count: " + MyThread.count); }
2. Thread safety
In multi-threaded programming, thread safety is an important concept. Thread safety means that when multiple threads access shared resources, there will be no data errors or inconsistencies. Common thread safety issues include race conditions and resource contention.
In order to achieve thread safety, you can take the following methods:
public synchronized void increment() { // 代码块 }
Summary:
The above is a brief introduction and sample code about Java multi-threaded programming and thread safety. In actual development, multi-threaded programming and thread safety are very important topics. It is necessary to use threads and synchronization mechanisms reasonably and follow some best practices to ensure the correctness and performance of the program.
However, multi-threaded programming and thread safety are complex topics that require in-depth study and practice. This article only provides some basic concepts and sample code, hoping to provide readers with some reference and inspiration.
The above is the detailed content of Java development: How to do multi-threaded programming and thread safety. For more information, please follow other related articles on the PHP Chinese website!