java - Java中如何通知特定线程

标签 java multithreading ipc java-threads

如何在线程间通信中调用特定线程?

在下面的程序中,我有两个线程 t1t2

当我调用 t1.notify() 时,它会引发:

Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at Shared.methodTwo(NotifyThread.java:43)
    at Thread2.run(NotifyThread.java:77)
Error 
<小时/>
class Shared {

    Thread1 t1 ;
    Thread2 t2 ;

    void ThreadInit( Thread1 t1 , Thread2 t2 ) {
        this.t1 = t1 ;
        this.t2 = t2 ;
    }

    synchronized void methodOne()
    {
        Thread t = Thread.currentThread();

        System.out.println(t.getName()+" is relasing the lock and going to wait");

        try
        {
            wait();        //releases the lock of this object and waits
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        System.out.println(t.getName()+" got the object lock back and can continue with it's execution");
    }

    synchronized void methodTwo()
    {
        Thread t = Thread.currentThread();

        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        t1.notify();     

        System.out.println("A thread which is waiting for lock of this object is notified by "+t.getName());
    }
    }

    class Thread1 extends Thread 
    {
    Shared s ;
    Thread1( Shared s ) {

        this.s = s ;
    }

    public void run()
            {
                s.methodOne();   //t1 calling methodOne() of 's' object
            }

    } 

    class Thread2 extends Thread {
         Shared s ;
    Thread2( Shared s ) {

        this.s = s ;

    }

    public void run()
            {
                s.methodTwo();   //t1 calling methodOne() of 's' object
            }


    }
    public class NotifyThread 
    {
    public static void main(String[] args)
    {
        final Shared s = new Shared();

        Thread1 t1 = new Thread1(s) ;
        Thread2 t2 = new Thread2(s) ;

        s.ThreadInit(t1,t2) ;

        t1.start();
        t2.start();
    }
}

最佳答案

您不/无法通知特定线程。您对锁对象调用 notify()。这会唤醒正在等待锁的线程1之一。在你的例子中,锁对象是一个Thread ...这相当令人困惑。不过,请参阅下文。

但是您的问题(IllegalMonitorStateException)发生是因为执行通知的线程(即当前线程)没有持有锁。当前线程在通知锁时必须持有锁,这是一个(硬)要求。

有关更多详细信息,请阅读 Object.wait(timeout) 的 javadocs 或(例如):http://howtodoinjava.com/core-java/multi-threading/how-to-work-with-wait-notify-and-notifyall-in-java/

1 - 如果多个线程正在等待您的锁,则调度程序会“随机”选择一个线程。或者notifyAll将唤醒所有等待的线程。

<小时/>

我不会使用Thread对象作为锁对象。它可能会起作用,但也有可能其他东西(可能是运行时系统中的某些东西)也在锁定/等待/通知 Thread 对象。那么事情就会变得非常困惑。

(确实,请阅读 javadocThread.join(long) !)

最好专门为此目的创建锁对象;例如

private final Object lock = new Object();
<小时/>

此外,编写扩展Thread的类通常是一个坏主意。通常最好实现 Runnable 接口(interface),实例化它,并将实例作为参数传递给 Thread 构造函数;例如

Thread t = new Thread(new Runnable() {
    public void run() {
        System.out.println("Hello world");
    }});
t.start();

实现 Runnable 而不是扩展 Thread 的一个优点是,您可以更轻松地使用代码来管理线程生命周期;例如ExecutorService、fork-join 线程池或经典线程池。

第二个是轻量级线程逻辑可以作为匿名类简洁地实现......就像我的示例一样。

关于java - Java中如何通知特定线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55940267/

相关文章:

python - 我的 socket 和 pickle 有问题。值​不保存到 txt 中

php - 如何在 PHP 客户端和 C 守护程序服务器之间进行 IPC?

c# - 查询任务的 Result 属性导致线程池创建一个新线程

java - 如何取消jtextarea事件?

java - ImageView.setImageDrawable 在 LANDSCAPE 模式下不起作用

java - 小程序中的多个代码库

c# - 这是使用 System.Threading.Timer 运行计划的异步/等待方法的正确方法吗?

objective-c - NSMutableArray + NSEnumeration并发内存错误

c++ - 共享内存中的 vector 结构

java - 使用批处理解析多个 XML 文件