在多核CPU的時代,使用多線程編程已經是Java開發中的常態,對於並發編程的概念和實現方式有深入的了解是非常必要的。在Java中,實現並發編程有多種方式,包括使用Thread類、Runnable介面、線程池、鎖等。本文將從幾個方面介紹Java開發中的並發編程。
一、Thread類的使用
Java中的Thread類是實現多線程的基礎,在使用時需要繼承Thread類並重寫run()方法,定義線程執行的代碼。
public class MyThread extends Thread {
@Override
public void run() {
// 線程執行的代碼
System.out.println("MyThread is running");
}
}
public class Demo {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start(); // 啟動線程
}
}
上述代碼中,定義了一個MyThread類繼承自Thread類,重寫了run()方法,線程啟動時執行run()方法中的代碼。
二、Runnable介面的使用
Runnable介面也是實現多線程的常用方式,在使用時需要實現run()方法,Runnable介面的實現類對象可以作為Thread類構造方法的參數,用於啟動線程。
public class MyRunnable implements Runnable {
@Override
public void run() {
// 線程執行的代碼
System.out.println("MyRunnable is running");
}
}
public class Demo {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start(); // 啟動線程
}
}
上述代碼中,定義了一個MyRunnable類實現了Runnable介面並重寫了run()方法,Runnable介面的實現類對象作為Thread類的構造方法參數,用於啟動線程。
三、線程池的使用
線程池是一種同時管理多個線程的方式,將線程的創建和管理放在池中,可以有效減輕系統創建和刪除線程的壓力。Java中的線程池實現在java.util.concurrent包中,可以使用ThreadPoolExecutor類來創建線程池,此外還可以使用Executors類提供的方法簡單創建線程池。
public class MyRunnable implements Runnable {
@Override
public void run() {
// 線程執行的代碼
System.out.println("MyRunnable is running");
}
}
public class Demo {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
MyRunnable myRunnable = new MyRunnable();
executorService.submit(myRunnable);
}
executorService.shutdown(); // 關閉線程池
}
}
上述代碼中,使用Executors類提供的方法創建一個固定線程數為5的線程池,並提交10個MyRunnable實例作為線程池的任務,最後關閉線程池。
四、鎖的使用
在使用多線程時,需要注意線程安全的問題。Java中提供了多種鎖機制來實現線程安全,常用的是synchronized關鍵字和ReentrantLock類。
使用synchronized關鍵字可以實現方法級別的鎖,或者在代碼塊中使用synchronized(this)來實現對象級別的鎖。
public class MyThread {
private int count = 0;
public synchronized void addCount() {
count++;
}
public synchronized int getCount() {
return count;
}
}
public class Demo {
public static void main(String[] args) {
MyThread myThread = new MyThread();
for (int i = 0; i {
for (int j = 0; j < 1000; j++) {
myThread.addCount();
}
}).start();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("count = " + myThread.getCount());
}
}
上述代碼中,使用synchronized關鍵字實現了對count屬性的訪問控制,確保了多線程環境下對count屬性的數據同步。
使用ReentrantLock類也可以實現對共享資源的鎖控制,ReentrantLock類實例在加鎖後需要在finally中釋放鎖。
public class MyThread {
private int count = 0;
private ReentrantLock lock = new ReentrantLock();
public void addCount() {
try {
lock.lock();
count++;
} finally {
lock.unlock();
}
}
public int getCount() {
return count;
}
}
public class Demo {
public static void main(String[] args) {
MyThread myThread = new MyThread();
for (int i = 0; i {
for (int j = 0; j < 1000; j++) {
myThread.addCount();
}
}).start();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("count = " + myThread.getCount());
}
}
上述代碼中,使用ReentrantLock類實現對count屬性的訪問控制,並且在finally塊中釋放鎖。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/195535.html
微信掃一掃
支付寶掃一掃