boy002 2010-09-07 09:49
浏览 264
已采纳

java多线程疑惑

我对java多线程进行了一个测试,编写了一下代码,但产生的结果我个人却很难解释,代码如下:
Semaphore.java

class Semaphore
{
    private int count;

    public Semaphore(int n)
    {
        this.count = n;
    }

    public synchronized void acquire()
    {
        while (count == 0)
        {
            try
            {
                System.out.println(Thread.currentThread().getName()+"我来了。");
                wait();
                System.out.println(Thread.currentThread().getName()+"我醒了。");
            }
            catch (InterruptedException e)
            {
                // keep trying
                System.err.println(e);
            }
        }
        count--;
    }

    public synchronized void release()
    {
        count++;
        notify(); // alert a thread that's blocking on this semaphore
    }
}
public class TestMain
{
public void semaphoreTest() throws Exception
    {
        Semaphore se = new Semaphore(0);
        class Producer extends Thread
        {
            private Semaphore se = null;

            public Semaphore getSe()
            {
                return se;
            }

            public void setSe(Semaphore se)
            {
                this.se = se;
            }

            public void run()
            {
                for (int y =0 ; y <10;y++)
                {
                    try
                    {
                        Thread.sleep(1000L);
                    }
                    catch (Exception e)
                    {
                        System.out.println(e);
                    }
                    System.out.println(this.getName() + ":又产生了三个资源。");
                    se.release();
                    se.release();
                    se.release();
                }
            }
        }

        class Consumer extends Thread
        {
            private Semaphore se = null;

            public Semaphore getSe()
            {
                return se;
            }

            public void setSe(Semaphore se)
            {
                this.se = se;
            }

            public void run()
            {
                while (true)
                {
                    se.acquire();
                    System.out.println(this.getName() + ":我获取到资源了。");
                }
            }
        }

        Producer pd = new Producer();
        pd.setName("Productor");
        pd.setSe(se);
        for (int x = 0; x < 1; x++)
        {
            Consumer cs = new Consumer();
            cs.setSe(se);
            cs.setName(String.valueOf(x));
            cs.start();
        }
        pd.start();
    }
     public static void main(String[] args) throws Exception
    {
        // TODO 自动生成方法存根
        TestMain tm = new TestMain();
        tm.semaphoreTest();
    }
} 

   产生的结果:

  

0我来了。
Productor:又产生了三个资源。
0我醒了。
0:我获取到资源了。
0我来了。
0我醒了。
0:我获取到资源了。
0:我获取到资源了。
0我来了。
Productor:又产生了三个资源。
0我醒了。

    我醒了和我获取到了资源数目不一致,我只有一个thread却似乎有多个thread在运行,对于这个结果不知有哪位可以给以解惑。

 

  • 写回答

1条回答 默认 最新

  • qinglangee 2010-09-07 11:34
    关注

    "我来;wait();我醒了;"这段代码在count==0时才会执行
    release()方法中的notify();只是发出一个通知,并不一定会切换线程
    所以Producer的se.release();连续执行了三遍,完全有可能在一个时间片内完成,这时count=3,就会只醒来一次,取到3个资源.

    把Producer中的Thread.sleep(1000L);去掉可以让结果看起来更容易理解

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog