java线程等待(java线程等待超时取消)
Java线程等待
简介:
在多线程编程中,有时需要等待某个线程的执行结果,然后再进行下一步操作。Java提供了多种机制来实现线程等待,包括使用join()方法、使用wait()和notify()方法、使用CountDownLatch类等。
一级标题:使用join()方法实现线程等待
通过调用线程对象的join()方法,可以使当前线程等待该线程执行完毕。下面是示例代码:
```
public class ThreadJoinExample {
public static void main(String[] args) {
Thread thread1 = new Thread(new MyRunnable(), "Thread1");
Thread thread2 = new Thread(new MyRunnable(), "Thread2");
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("All threads have finished execution.");
}
static class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Thread " + Thread.currentThread().getName() + " is running.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread " + Thread.currentThread().getName() + " has finished execution.");
}
}
```
上面的代码创建了两个线程,并分别启动它们。然后主线程调用了thread1.join()和thread2.join()方法,使主线程等待线程1和线程2的执行完毕。最后输出"All threads have finished execution."。
二级标题:使用wait()和notify()方法实现线程等待
wait()和notify()方法是Object类的两个方法,可以实现线程之间的等待和通知。以下是示例代码:
```
public class ThreadWaitNotifyExample {
public static void main(String[] args) {
final Object lock = new Object();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("Thread1 is running.");
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread1 has been notified.");
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("Thread2 is running.");
lock.notify();
System.out.println("Thread2 has notified Thread1.");
}
}
});
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
```
上面的代码创建了两个线程并启动它们。线程1在运行时获取到锁,并调用了lock.wait()方法,使线程1等待。线程2在运行时获取到锁,并调用了lock.notify()方法,通知线程1可以继续执行。最后,主线程等待线程1和线程2的执行完毕。
三级标题:使用CountDownLatch类实现线程等待
CountDownLatch类是java.util.concurrent包中的一个类,可以用来实现线程间的等待。以下是示例代码:
```
import java.util.concurrent.CountDownLatch;
public class ThreadCountDownLatchExample {
public static void main(String[] args) {
CountDownLatch latch = new CountDownLatch(3);
Thread thread1 = new Thread(new MyRunnable(latch), "Thread1");
Thread thread2 = new Thread(new MyRunnable(latch), "Thread2");
Thread thread3 = new Thread(new MyRunnable(latch), "Thread3");
thread1.start();
thread2.start();
thread3.start();
try {
latch.await();
System.out.println("All threads have finished execution.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class MyRunnable implements Runnable {
private CountDownLatch latch;
public MyRunnable(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
try {
Thread.sleep((long) (Math.random() * 1000));
System.out.println("Thread " + Thread.currentThread().getName() + " has finished execution.");
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
```
上面的代码创建了一个CountDownLatch对象,并指定计数为3。在每个线程的run()方法中,线程会随机休眠一段时间,然后调用latch.countDown()方法减少计数器的值。主线程调用latch.await()方法,使主线程等待计数器值为0。当所有的线程都调用了countDown()方法后,计数器的值变为0,主线程继续执行。
内容详细说明:
以上介绍了Java中实现线程等待的三种方式。使用join()方法可以使当前线程等待指定的线程执行完毕。wait()和notify()方法可以实现线程之间的等待和通知。CountDownLatch类可以用来实现线程间的等待,通过设置计数器的值和调用countDown()方法来控制等待的线程数量。
其中,使用join()方法的方式比较简单,适用于两个或少量线程之间的等待。wait()和notify()方法需要先获取锁,并且只能在synchronized块中使用,适用于多个线程之间的等待和通知。CountDownLatch类适用于多个线程之间的复杂等待场景,可以灵活地控制等待的线程数量。
在实际应用中,根据具体的需求选择合适的线程等待机制来实现线程间的同步和协作。