java线程间通讯
Java provide benefits of avoiding thread pooling using inter-thread communication. The wait()
, notify()
, and notifyAll()
methods of Object class are used for this purpose. These method are implemented as final methods in Object, so that all classes have them. All the three method can be called only from within a synchronized context
Java提供了避免使用线程间通信进行线程池的好处。 为此,使用Object类的wait()
, notify()
和notifyAll()
方法。 这些方法在Object中作为最终方法实现,因此所有类都具有它们。 只能从同步上下文中调用所有这三种方法
wait()
tells calling thread to give up monitor and go to sleep until some other thread enters the same monitor and call notify.wait()
告诉调用线程放弃监视器,然后进入睡眠状态,直到其他线程进入同一监视器并调用notify。notify()
wakes up a thread that called wait() on same object.notify()
唤醒在同一对象上调用wait()的线程。notifyAll()
wakes up all the thread that called wait() on same object.notifyAll()
唤醒同一对象上所有调用wait()的线程。
wait()
和sleep()
之间的区别 (Difference between wait()
and sleep()
)
wait() | sleep() |
---|---|
called from synchronised block | no such requirement |
monitor is released | monitor is not released |
gets awake when notify() or notifyAll() method is called. | does not get awake when notify() or notifyAll() method is called |
not a static method | static method |
wait() is generaly used on condition | sleep() method is simply used to put your thread on sleep. |
等待() | 睡觉() |
---|---|
从同步块调用 | 没有这样的要求 |
监视器被释放 | 监视器未释放 |
当调用notify()或notifyAll()方法时醒来。 | 调用notify()或notifyAll()方法时不会唤醒 |
不是静态方法 | 静态方法 |
通常在条件上使用wait() | sleep()方法仅用于使线程进入睡眠状态。 |
线程池 (Thread Pooling)
Pooling is usually implemented by loop i.e to check some condition repeatedly. Once condition is true appropriate action is taken. This waste CPU time.
池化通常是通过循环实现的,即反复检查某些条件。 一旦条件成立,就会采取适当的措施。 这浪费了CPU时间。
Java中的线程死锁 (Thread Deadlock in Java)
![Deadlock condition in Multithreading](https://i-blog.csdnimg.cn/blog_migrate/c3d64559a4c1d39bc7c97bba83853fea.gif)
Deadlock is a situation of complete Lock, when no thread can complete its execution because lack of resources. In the above picture, Thread 1 is holding a resource R1, and need another resource R2 to finish execution, but R2 is locked by Thread 2, which needs R3, which in turn is locked by Thread 3. Hence none of them can finish and are stuck in a deadlock.
死锁是完全锁定的情况,因为没有资源,没有线程可以完成其执行。 在上图中,线程1拥有一个资源R1,并且需要另一个资源R2来完成执行,但是R2被线程2锁定了,而线程2需要R3,而后者又被线程3锁定了。陷入僵局。
例 (Example)
In this example, multiple threads are accessing same method that leads to deadlock condition. When a thread holds the resource and does not release it then other thread will wait and in deadlock condition wait time is never ending.
在此示例中,多个线程正在访问导致死锁条件的相同方法。 当一个线程持有该资源并且不释放它时,其他线程将等待,并且在死锁条件下,等待时间永远不会结束。
class Pen{}
class Paper{}
public class Write {
public static void main(String[] args)
{
final Pen pn =new Pen();
final Paper pr =new Paper();
Thread t1 = new Thread() {
public void run()
{
synchronized(pn)
{
System.out.println("Thread1 is holding Pen");
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
// do something
}
synchronized(pr)
{
System.out.println("Requesting for Paper");
}
}
}
};
Thread t2 = new Thread() {
public void run()
{
synchronized(pr)
{
System.out.println("Thread2 is holding Paper");
try {
Thread.sleep(1000);
}
catch(InterruptedException e){
// do something
}
synchronized(pn)
{
System.out.println("requesting for Pen");
}
}
}
};
t1.start();
t2.start();
}
}
Thread1 is holding Pen Thread2 is holding Paper
线程1握住笔线程2握住纸
翻译自: https://www.studytonight.com/java/interthread-communication.php
java线程间通讯