本文介绍了在Object类中放置wait(),notify()方法的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是很难理解在 Object 类中放置 wait()背后的概念。为了这个问题,请考虑 wait() notifyAll()是否在 Thread class。

I am just having hard time to understand concept behind putting wait() in Object class. For this questions sake consider if wait() and notifyAll() are in Thread class.

class Reader extends Thread {
    Calculator c;
    public Reader(Calculator calc) {
        c = calc;
    }

    public void run() {
        synchronized(c) {                              //line 9
        try {
            System.out.println("Waiting for calculation...");
            c.wait();
        } catch (InterruptedException e) {}
            System.out.println("Total is: " + c.total);
        }
    }

    public static void main(String [] args) {
        Calculator calculator = new Calculator();
        new Reader(calculator).start();
        new Reader(calculator).start();
        new Reader(calculator).start();
        calculator.start();
    }
}

class Calculator extends Thread {
    int total;
    public void run() {
        synchronized(this) {                     //Line 31
            for(int i=0;i<100;i++) {
                total += i;
            }
             notifyAll();
        }
    }
}

我的问题是,有什么区别它本来可以吗?在第9行中,我们获取对象c上的锁定,然后执行等待,以满足我们需要在使用等待之前获取对象的锁定的等待条件,因此对于notifyAll我们已经在第31行获取了对象的对象的锁定。

My Question is that what difference it could have made? In line 9 we are acquiring lock on object c and then performing wait which satisfy the condition for wait that we need to acquire lock on the object before we use wait and so is case for notifyAll we have acquired lock on object of Calculator at line 31.

推荐答案

在Java语言中,你 wait() Object 的特定实例上 - 分配给该对象的监视器是精确的。如果要向正在该特定对象实例上等待的一个线程发送信号,则在该对象上调用 notify()。如果要向正在该对象实例上等待的所有线程发送信号,则在该对象上使用 notifyAll()

In the Java language, you wait() on a particular instance of an Object – a monitor assigned to that object to be precise. If you want to send a signal to one thread that is waiting on that specific object instance then you call notify() on that object. If you want to send a signal to all threads that are waiting on that object instance, you use notifyAll() on that object.

如果 wait() notify() Thread 而不是每个线程必须知道每个其他线程的状态。 thread1如何知道thread2正在等待访问特定资源?如果thread1需要调用 thread2.notify(),则必须以某种方式发现 thread2 正在等待。需要有一些机制让线程注册他们需要的资源或操作,以便其他人可以在内容准备好或可用时发出信号。

If wait() and notify() were on the Thread instead then each thread would have to know the status of every other thread. How would thread1 know that thread2 was waiting for access to a particular resource? If thread1 needed to call thread2.notify() it would have to somehow find out that thread2 was waiting. There would need to be some mechanism for threads to register the resources or actions that they need so others could signal them when stuff was ready or available.

在Java中,对象本身是线程之间共享的实体,允许它们相互通信。线程彼此之间没有特定的知识,它们可以异步运行。他们运行并锁定,等待并通知他们想要访问的对象。他们不了解其他线程,也不需要知道他们的状态。他们不需要知道正在等待资源的是thread2 - 他们只是通知资源以及通知等待的任何人(如果有人)将被通知。

In Java, the object itself is the entity that is shared between threads which allows them to communicate with each other. The threads have no specific knowledge of each other and they can run asynchronously. They run and they lock, wait, and notify on the object that they want to get access to. They have no knowledge of other threads and don't need to know their status. They don't need to know that it is thread2 which is waiting for the resource – they just notify on the resource and whomever it is that is waiting (if anyone) will be notified.

在Java中,我们然后使用锁定对象作为线程之间的同步,互斥和通信点。我们在锁定对象上进行同步,以获取对重要代码块的互斥锁访问并同步内存。如果我们等待一些条件改变,我们会等待一个对象 - 一些资源变得可用。如果我们想要唤醒睡眠线程,我们会通知一个对象。

In Java, we then use lock objects as synchronization, mutex, and communication points between threads. We synchronize on a lock object to get mutex access to an important code block and to synchronize memory. We wait on an object if we are waiting for some condition to change – some resource to become available. We notify on an object if we want to awaken sleeping threads.

// locks should be final objects so the object instance we are synchronizing on,
// never changes
private final Object lock = new Object();
...
// ensure that the thread has a mutex lock on some key code
synchronized (lock) {
    ...
    // i need to wait for other threads to finish with some resource
    // this releases the lock and waits on the associated monitor
    lock.wait();
    ...
    // i need to signal another thread that some state has changed and they can
    // awake and continue to run
    lock.notify();
}

程序中可以有任意数量的锁定对象 - 每个锁定一个特定的锁定对象资源或代码段。您可能有100个锁定对象,只有4个线程。当线程运行程序的各个部分时,它们可以独占访问其中一个锁定对象。同样,他们不必知道其他线程的运行状态。

There can be any number of lock objects in your program – each locking a particular resource or code segment. You might have 100 lock objects and only 4 threads. As the threads run the various parts of the program, they get exclusive access to one of the lock objects. Again, they don't have to know the running status of the other threads.

这允许您扩展或减少软件中运行的线程数量如你所愿。您发现4个线程在外部资源上阻塞太多,然后您可以增加数量。推动受攻击的服务器太难,然后减少运行线程的数量。锁定对象确保线程之间的互斥和通信,与运行的线程数无关。

This allows you to scale up or down the number of threads running in your software as much as you want. You find that the 4 threads is blocking too much on outside resources, then you can increase the number. Pushing your battered server too hard then reduce the number of running threads. The lock objects ensure mutex and communication between the threads independent on how many threads are running.

这篇关于在Object类中放置wait(),notify()方法的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 18:54
查看更多