让线程按顺序执行8种方法("8种高效方法确保线程按顺序执行")
原创
1. 使用 join() 方法
在Java中,可以使用Thread类的join()方法确保线程按顺序执行。join()方法会令当前线程等待调用join()方法的线程终结后再继续执行。
public class ThreadJoinExample {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
System.out.println("线程1执行");
});
Thread t2 = new Thread(() -> {
System.out.println("线程2执行");
});
t1.start();
t1.join();
t2.start();
}
}
2. 使用 CountDownLatch
CountDownLatch是一个线程同步的辅助类,允许一个或多个线程等待其他线程完成操作。CountDownLatch中包含一个计数器,当计数器值为0时,所有等待的线程将被唤醒。
public class CountDownLatchExample {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
Thread t1 = new Thread(() -> {
System.out.println("线程1执行");
latch.countDown();
});
Thread t2 = new Thread(() -> {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程2执行");
});
t1.start();
t2.start();
}
}
3. 使用 CyclicBarrier
CyclicBarrier是一个线程同步的辅助类,允许一组线程互相等待,直到所有线程都约为某个屏障点后才继续执行。CyclicBarrier可以用于确保线程按顺序执行。
public class CyclicBarrierExample {
public static void main(String[] args) throws InterruptedException {
CyclicBarrier barrier = new CyclicBarrier(2);
Thread t1 = new Thread(() -> {
System.out.println("线程1执行");
try {
barrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
});
Thread t2 = new Thread(() -> {
System.out.println("线程2执行");
try {
barrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
});
t1.start();
t2.start();
}
}
4. 使用 Semaphore
Semaphore是一个计数信号量,重点用于束缚可以同时访问某个特定资源的线程数量。Semaphore也可以用于确保线程按顺序执行。
public class SemaphoreExample {
private static final Semaphore semaphore = new Semaphore(1);
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
try {
semaphore.acquire();
System.out.println("线程1执行");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
});
Thread t2 = new Thread(() -> {
try {
semaphore.acquire();
System.out.println("线程2执行");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
});
t1.start();
t2.start();
}
}
5. 使用 ReentrantLock
ReentrantLock是Java提供的一个显式锁实现,可以用于确保线程按顺序执行。通过使用锁的tryLock()方法,可以实现对线程执行顺序的控制。
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockExample {
private static final Lock lock = new ReentrantLock();
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
lock.lock();
try {
System.out.println("线程1执行");
} finally {
lock.unlock();
}
});
Thread t2 = new Thread(() -> {
lock.lock();
try {
System.out.println("线程2执行");
} finally {
lock.unlock();
}
});
t1.start();
t2.start();
}
}
6. 使用 Condition
Condition是Java提供的一个线程同步辅助类,与ReentrantLock结合使用,可以用于确保线程按顺序执行。
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ConditionExample {
private static final ReentrantLock lock = new ReentrantLock();
private static final Condition condition = lock.newCondition();
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
lock.lock();
try {
System.out.println("线程1执行");
condition.signal();
} finally {
lock.unlock();
}
});
Thread t2 = new Thread(() -> {
lock.lock();
try {
condition.await();
System.out.println("线程2执行");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
t1.start();
t2.start();
}
}
7. 使用屏障栅栏(Phaser)
Phaser是Java 7引入的一个新的线程同步辅助类,类似于CyclicBarrier,但它更灵活。Phaser可以动态地注册和注销屏障点,适用于动态线程数的场景。
import java.util.concurrent.Phaser;
public class PhaserExample {
public static void main(String[] args) {
Phaser phaser = new Phaser(2);
Thread t1 = new Thread(() -> {
System.out.println("线程1执行");
phaser.arriveAndDeregister();
});
Thread t2 = new Thread(() -> {
phaser.register();
System.out.println("线程2执行");
phaser.arriveAndDeregister();
});
t1.start();
t2.start();
}
}
8. 使用状态变量
使用状态变量是一种易懂而有效的方法来确保线程按顺序执行。通过在状态变量上使用wait()和notifyAll()方法,可以实现对线程执行顺序的控制。
public class StateVariableExample {
private static boolean t1Executed = false;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
System.out.println("线程1执行");
t1Executed = true;
synchronized (StateVariableExample.class) {
StateVariableExample.class.notifyAll();
}
});
Thread t2 = new Thread(() -> {
synchronized (StateVariableExample.class) {
while (!t1Executed) {
try {
StateVariableExample.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("线程2执行");
});
t1.start();
t2.start();
}
}
以上8种方法都是确保线程按顺序执行的有效手段。通过具体场景和需求,可以选择合适的方法来实现线程的有序执行。