简体   繁体   English

Java /操作系统中的信号量

[英]Semaphore in Java/Operating Systems

I am new in the forum and in programming in general.At the moment i study "Operation Systems" and Java. 我是论坛和编程的新手。目前我正在研究“操作系统”和Java。 This week we started the topic "Semaphores" and it seems to me that it's a bit confusing. 本周我们开始讨论“信号量”这个主题,在我看来这有点令人困惑。 I have this exercise: 我有这个练习:

shared variable numberofworks = 0 共享变量numberofworks = 0

EmployeeA 
    {
        while (True) {
            A_works_outside();
            A_works_inside();
            numberofworks =numberofworks + 1;
        }
    }

    EmployeeB
    {
        while (True) {
            B_works_outside();
            B_works_inside();
            numberofworks =numberofworks + 1;
        }
    }

well,the exercise wants to use semaphores (P and V) so 好吧,练习想要使用信号量(P和V)

  1. In i repetition of EmployeeA the A_works_inside() runs only when B_works_outside() is finished 在重复EmployeeA时, A_works_inside()仅在B_works_outside()完成时运行
  2. In i repetition of EmployeeB the Β_works_inside() runs only when Α_works_outside() is finished 在重复EmployeeB时, Β_works_inside()仅在Α_works_outside()完成时运行

  3. Variable numberofworks must be the sum of numberofworksA + numberofworksB 变量numberofworks必须是numberofworksA + numberofworksB的总和

I must write the code in Java and in "operation system" before helping me in this exercise (if you can of course) General questions in semaphores: What's the basic difference between mutex and a sempahore? 在帮助我进行本练习之前,我必须用Java和“操作系统”编写代码(如果可以的话)信号量中的一般问题:互斥体和sempahore之间的基本区别是什么? and how should i understand when to use mutex or not? 我应该如何理解何时使用互斥?

Trying to use a resource protected by a Mutex: 尝试使用受互斥锁保护的资源:

- Hi! Can I ?
*If free*
- Yup go on.
*Else*
- No, go away!

Trying to use a resource protected by a Semaphore: 尝试使用信号量保护的资源:

- Hi! Can I ?
*If free*
- Yup go on.
*Else*
- No, wait in the queue!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么操作系统不是用java编写的? - Why operating systems are not written in java? 有没有办法用Java获取远程主机的操作系统 - Is there a way to get operating systems of remote host in java Java-自定义游标在不同的操作系统中有所不同 - Java - custom cursor is different in different operating systems java swing如何从不同的操作系统吸引到屏幕上? - How java swing draws to screen from different operating systems? Java 中的多线程:不同的操作系统提供不同的性能吗? - Multithreading in Java: Do different Operating Systems offer different performance? 是否可以使用Java为不同的操作系统更改桌面背景? - Is it possible to change the desktop background with Java for different operating systems? 如何处理文件到Java中不同操作系统的路径? - How to treat the path of a file to the different operating systems in Java? 为什么Java GUI JFrame大小在不同的操作系统上会有所不同? - Why is Java GUI JFrame size different sizes on different operating systems? 如何在java中捕获各种操作系统的屏幕截图? - How to capture screen shots of various operating systems in java? Java Swing定位元素在不同的操作系统中有所不同 - Java Swing positioning elements differently in different operating systems
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM