如何使用通知和等待

发布于 2024-11-05 17:33:03 字数 966 浏览 0 评论 0原文

wait/notify可以在一个线程中使用吗? 我的意思是我有一个侦听器,当该侦听器被调用时,我想启用一个线程来完成他的工作。我该怎么做?

更新:我的数据写入数据库中......并且每次调用侦听器时都会写入。现在我创建的线程读取该数据并将其发送到某处...... 接下来......我获取一些其他数据并做同样的事情......另一个线程需要知道他读取的最后一个数据是什么,以便他可以从他离开的地方开始阅读......

看看这里: 在一个线程中使用等待和通知 这就是我的问题的样子。谢谢,

我有以下内容:

synchronized (syncToken)
{
    try {
        syncToken.wait();

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
System.out.println("MyThread: " + s);

MyThread 中......所以当我这样做时,

MyThread t = new MyThread(syncToken);
t.start();

我将我的线程置于等待状态......是吗?

当我这样做时:

syncToken.notify();

我让我的线程回到正轨......但是下一行的执行是 wait() 之后的那一行?

我的意思是: System.out.println("MyThread: " + s); ????

当你通知一个线程时,他会在 wait() 之后的行中继续执行吗???Thx

Can wait/notify be used within one thread?
I'm mean I have a listener and in the moment when that listener gets called I wanna enable a thread to do his work.How could I do that?

UPDATE:My data is written in a database...and is written each time the listener is called.Now the thread that I've created reads that data and sends it somewhere....
Next...I get some other data and do the same thing....The other thread needs to know what was the last data he read it so he can start reading from where he left....

Take a look in here:
using wait and notify within one thread
This is how my problem looks like.Thx

I have the following:

synchronized (syncToken)
{
    try {
        syncToken.wait();

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
System.out.println("MyThread: " + s);

in MyThread....so when I do

MyThread t = new MyThread(syncToken);
t.start();

I put my thread on waiting...yes?

And when I do this:

syncToken.notify();

I get my thread back on track....but the execution of the next line is the one after wait()?

I mean this: System.out.println("MyThread: " + s); ????

When u notify a thred does he continues his execution with the line after wait()???Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听,心雨的声音 2024-11-12 17:33:03

下面是两个不同线程之间并发的简单示例。在示例中,主线程启动一个 MyThread 线程,每 3 秒将数据设置到 MyThread 实例,然后 MyThread 打印它。这个想法是拥有一个同步对象,您可以等待它,并在使用结束时通知其他线程可以使用它:

Test.java:

package stack;

public class Test {
    public static void main (String args[])
    {
        Object syncToken = new Object();
        MyThread t = new MyThread(syncToken);
        t.start();
        for (int i = 0; i < 10; i++)
        {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized(syncToken)
            {
                t.setText("Iteration " + i);
                syncToken.notify();
            }
        }
    }
}

MyThread。 java:

package stack;

public class MyThread extends Thread{

    String s;
    Object syncToken;
    public MyThread(Object syncToken)
    {
        this.s = "";
        this.syncToken = syncToken;
    }

    public void run()
    {
        while(true) // you will need to set some condition if you want to stop the thread in a certain time...
        {
            synchronized (syncToken)
            {
                try {
                    syncToken.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("MyThread: " + s);
        }
    }

    public void setText(String s)
    {
        this.s = s;
    }
}

在此示例中,主线程设置一个字符串(每 3 秒),MyThread 线程打印它。

根据您的需要进行调整,应该不会太难。

The following is a simple example of concurrency between two different threads. In the example the main thread start a MyThread thread and every 3 seconds it sets a data to the MyThread instance and then MyThread prints it. The idea is to have a synchronized object that you wait on it and notify in the end of the usage to other threads that they can use it:

Test.java:

package stack;

public class Test {
    public static void main (String args[])
    {
        Object syncToken = new Object();
        MyThread t = new MyThread(syncToken);
        t.start();
        for (int i = 0; i < 10; i++)
        {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized(syncToken)
            {
                t.setText("Iteration " + i);
                syncToken.notify();
            }
        }
    }
}

MyThread.java:

package stack;

public class MyThread extends Thread{

    String s;
    Object syncToken;
    public MyThread(Object syncToken)
    {
        this.s = "";
        this.syncToken = syncToken;
    }

    public void run()
    {
        while(true) // you will need to set some condition if you want to stop the thread in a certain time...
        {
            synchronized (syncToken)
            {
                try {
                    syncToken.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("MyThread: " + s);
        }
    }

    public void setText(String s)
    {
        this.s = s;
    }
}

In this example, the main thread sets a string (every 3 seconds) and the MyThread thread prints it.

Adapt it to your needs, it shouldn't be too hard.

习惯成性 2024-11-12 17:33:03

我有类似的问题。我创建了一个由两个线程使用的仲裁器(在您的情况下,它可以是侦听器线程和您的任务线程):
侦听器:

arbiter.waitConsumer();
// prepare data
arbiter.dataLoaded();

任务线程:

while(true){
  arbiter.waitProducer();
  // consume data
  arbiter.dataConsumed();
}

仲裁器:

public class Arbiter {
private boolean dataLoaded = false;
public synchronized void waitProducer(){
    while(!dataLoaded){
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public synchronized void waitConsumer(){
    while(dataLoaded){
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public synchronized void dataLoaded(){
    dataLoaded = true;
    notify();
}public synchronized void dataConsumed(){
    dataLoaded = false;
    notify();
}}

侦听器和任务将根据仲裁器监视器同步自身。也许您可以调用您的仲裁队列或管道并存储日期以在其中使用?

I had similar problem. I created an arbiter used by two threads (in your case it can be listeners thread and your task thread):
listener:

arbiter.waitConsumer();
// prepare data
arbiter.dataLoaded();

task thread:

while(true){
  arbiter.waitProducer();
  // consume data
  arbiter.dataConsumed();
}

arbiter:

public class Arbiter {
private boolean dataLoaded = false;
public synchronized void waitProducer(){
    while(!dataLoaded){
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public synchronized void waitConsumer(){
    while(dataLoaded){
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public synchronized void dataLoaded(){
    dataLoaded = true;
    notify();
}public synchronized void dataConsumed(){
    dataLoaded = false;
    notify();
}}

Listener and task will synchronize themselfes against arbiters monitor. Probably you can call your arbiter queue or pipe and store date for consuming in it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文