- wait()和notify():wait()方法会使线程等待,直到其他线程调用notify()方法或notifyAll()方法唤醒该线程。当线程调用wait()方法时,会释放持有的锁。这两个方法必须在synchronized代码块中调用。
- interrupted():interrupted()方法检查线程是否被中断,并清除中断标志。该方法是一个静态方法。如果线程被中断,则返回true,否则返回false。中断一个线程意味着打断线程的执行,可以通过interrupt()方法中断线程。
代码示例
private static final int MAX_COUNT = 10; // 缓冲区容量
private static final Object LOCK = new Object();//用于线程通信的锁对象
private static int item = 0;
static class Producer implements Runnable {
private final String name;
public Producer(String name) {
this.name = name;
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
synchronized (LOCK) {
while (item == MAX_COUNT) { // 缓冲区满了,生产者线程等待
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
item++;
System.out.println("Producer " + name + " 生产了 1 个产品。");
LOCK.notifyAll();
}
}
}
}
static class Consumer implements Runnable {
private final String name;
public Consumer(String name) {
this.name = name;
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
synchronized (LOCK) {
while (item == 0) { // 缓冲区为空,消费者线程等待
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
item--;
System.out.println("Consumer " + name + " 消费了 1 个产品。");
LOCK.notifyAll();
}
}
}
}
public static void main(String[] args) throws Exception {
Thread producerThread1 = new Thread(new Producer("A"));
Thread producerThread2 = new Thread(new Producer("B"));
Thread consumerThread1 = new Thread(new Consumer("X"));
Thread consumerThread2 = new Thread(new Consumer("Y"));
producerThread1.start();
producerThread2.start();
consumerThread1.start();
consumerThread2.start();
Thread.sleep(300); // 运行 0.3秒钟
producerThread1.interrupt(); // 中断生产者线程1
producerThread2.interrupt(); // 中断生产者线程2
consumerThread1.interrupt(); // 中断消费者线程1
consumerThread2.interrupt(); // 中断消费者线程2
}
- join():join()方法用于等待另一个线程的执行完成。如果线程A在执行过程中调用了线程B的join()方法,那么线程A会被阻塞,直到线程B执行完毕。这种方式通常用于等待某个耗时的任务完成后再继续进行其他操作。
代码示例
public class MyThreadMethodJoin extends Thread {
public MyThreadMethodJoin(String name) {
super(name);
}
@Override
public void run() {
System.out.println("Thread " + getName() + " started.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Thread " + getName() + " interrupted.");
return;
}
System.out.println("Thread " + getName() + " finished.");
}
public static void main(String[] args) throws Exception {
MyThreadMethodJoin t1 = new MyThreadMethodJoin("Thread 1");
MyThreadMethodJoin t2 = new MyThreadMethodJoin("Thread 2");
MyThreadMethodJoin t3 = new MyThreadMethodJoin("Thread 3");
t1.start();
t2.start();
t1.join();
t2.join();
t3.start();
t3.join();
System.out.println("Main thread finished.");
}
}
运行结果
Thread Thread 2 started.
Thread Thread 1 started.
-- 线程1,2启动 ,进入两秒阻塞
Thread Thread 1 finished.
Thread Thread 2 finished.
Thread Thread 3 started.
--线程3启动,进入两秒阻塞
Thread Thread 3 finished.
Main thread finished.
当注掉代码中三个线程的join方法后,主线程不会等待三个线程,先执行完毕输出,输出结果:
Main thread finished.
Thread Thread 2 started.
Thread Thread 3 started.
Thread Thread 1 started.
Thread Thread 2 finished.
Thread Thread 3 finished.
Thread Thread 1 finished.