Java多线程同步问题实例及解决方案
在Java中,多线程的同步问题主要体现在以下几种情况:
- 互斥锁(Mutex):
当一个线程访问资源时,其他线程必须等待。这时就需要互斥锁。
import java.util.concurrent.Lock;
import java.util.concurrent.Runnable;
import java.util.concurrent.locks.ReentrantLock;
public class MutexExample implements Runnable {
private Lock lock = new ReentrantLock();
@Override
public void run() {
try {
// 为资源获取锁
lock.acquire();
System.out.println("Thread " + Thread.currentThread().getName() + " is accessing the resource...");
// 使用完资源后释放锁
lock.release();
System.out.println("Thread " + Thread.currentThread().getName() + " has released the resource.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MutexExample example = new MutexExample();
Thread thread1 = new Thread(example, "Thread 1"));
Thread thread2 = new Thread(example, "Thread 2"));
thread1.start();
thread2.start();
// 等待所有线程完成
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
- 条件变量(Condition):
条件变量用于在多线程环境中实现基于特定条件的同步。
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ConditionVariableExample implements Runnable {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
private int count = 0;
@Override
public void run() {
try {
// 获取锁
lock.acquire();
// 设置条件
condition.signalAll();
System.out.println("Thread " + Thread.currentThread().getName() + " is waiting for a signal.");
// 等待信号
while (!condition.await(500, TimeUnit.MILLISECONDS)))) {
System.out.println("Thread " + Thread.currentThread().getName() + " still waiting for a signal.");
}
System.out.println("Thread " + Thread.currentThread().getName() + " has received the signal and will continue its execution.");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 无论是否发生中断,都需要释放锁
lock.release();
}
}
public static void main(String[] args) {
ConditionVariableExample example = new ConditionVariableExample();
Thread thread1 = new Thread(example, "Thread 1"));
Thread thread2 = new Thread(example, "Thread 2"));
thread1.start();
thread2.start();
// 等待所有线程完成
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized
块:synchronized
关键字用于修饰代码块,确保同一时刻只有一个线程访问该部分代码。
public class SynchronizedExample {
private Object lock = new Object();
public void doSomething() {
synchronized (lock) { // 使用锁对象
System.out.println("Thread " + Thread.currentThread().getName() + " is accessing the method.");
try {
// 这里是你要执行的代码
Thread.sleep(500); // 等待500毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SynchronizedExample example = new SynchronizedExample();
Thread thread1 = new Thread(example, "Thread 1"));
Thread thread2 = new Thread(example, "Thread 2"));
thread1.start();
thread2.start();
// 等待所有线程完成
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
通过以上例子,你可以了解到如何在Java多线程中使用互斥锁、条件变量和synchronized
块来实现同步。
还没有评论,来说两句吧...