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 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载