为什么这个 Thread 表现得如此奇怪? [英] Why does this Thread behave so strange?

查看:54
本文介绍了为什么这个 Thread 表现得如此奇怪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑以下代码片段,它的行为符合预期.线程运行,然后暂停,然后取消暂停并完成执行:

Let's think of following code fragment, that behaves as expected. Thread runs, then it's paused and then it's unpaused and finishes it's execution:

public static void main(final String[] args) throws InterruptedException {
        Executor exec = Executors.newSingleThreadExecutor();
        MyThread thread = new MyThread();
        exec.execute(thread);
        thread.pause();
        thread.pause(); // unpause
    }

现在让我们向线程添加一些睡眠,使其暂停一段时间:

Now let's add add some sleeping to thread so it's paused for a while:

public static void main(final String[] args) throws InterruptedException {
        Executor exec = Executors.newSingleThreadExecutor();
        MyThread thread = new MyThread();
        exec.execute(thread);
        thread.pause();
        Thread.sleep(500); 
        thread.pause(); // unpause
    }

但该代码永远不会完成.为什么?

这里是 pause 方法的实现,它检查私有布尔字段是否暂停:

Here's implementation of pause method, it checks private boolean field for pausing:

public synchronized void pause() {
        paused = (paused) ? false : true;
    }

这是覆盖运行方法的实现:

And here is implementation of overriden run method:

@Override
    public void run() {
        // don't worry, I just need som dummy data to take some cpu time ;)
        PriorityQueue<Double> queue = new PriorityQueue<Double>();
        Random random = new Random(System.currentTimeMillis());
        System.out.println("I stared");
        checkPause();
        // let's do some big computation
        for (int i=0; i<10000000; i++) { // 10 mio
            System.out.println(i);
            queue.add(random.nextDouble());
            if (i % 3 == 0) {
                queue.poll(); // more complex operation
            }
        }
        System.out.println("I'm done");
    }

private void checkPause() {
        synchronized (this) {
            if (paused) {
                while (paused != false) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

当我尝试调试时,我将以 wait() 方法结束.然后它只是等待:/

When I tried debugging, I'll end on wait() method. Then it just waits :/

推荐答案

当你调用 wait() 时,你的线程会一直等到另一个线程调用它的 notify() 方法.

When you call wait(), your thread waits until another thread calls its notify() method.

您没有在主线程的线程上调用 notify().

You're not calling notify() on the thread from your main thread.

还要注意synchronize(this)和同步方法是一回事;它使用对象本身作为锁.一旦您的线程命中 wait(),您的主线程将在 thread.unpause() 上阻塞,因为 checkPause() 方法有锁.

Also note that synchronize(this) is the same thing as synchronizing the method; it's using the object itself as the lock. Once your thread hits wait() your main thread will block on thread.unpause() because the checkPause() method has the lock.

这篇关于为什么这个 Thread 表现得如此奇怪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆